-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
microsoft/playwright
#22155Labels
Description
Context:
- Playwright Version: 1.32.1
- Operating System: Linux
- Python version: 3.11
- Browser: Chromium
WebSocket
's framereceived
event data is either a str
or bytes
, but the docs suggest it's a dict
with property payload
that's either a str
or bytes
. I think the relevant code is here:
playwright-python/playwright/_impl/_network.py
Lines 631 to 635 in 56a40a5
def _on_frame_received(self, opcode: int, data: str) -> None: | |
if opcode == 2: | |
self.emit(WebSocket.Events.FrameReceived, base64.b64decode(data)) | |
elif opcode == 1: | |
self.emit(WebSocket.Events.FrameReceived, data) |
Here's a proof-of-concept demonstrating this inconsistency:
Proof-of-concept
from anyio import create_memory_object_stream, run
import math
from playwright.async_api import async_playwright
async def events(browser):
context = await browser.new_context()
page = await context.new_page()
try:
await page.goto("https://www.phoenixframework.org", wait_until="commit")
send_stream, receive_stream = create_memory_object_stream(
max_buffer_size=math.inf
)
page.on(
"websocket",
lambda websocket: websocket.on(
"framereceived", lambda frame: send_stream.send_nowait(frame)
),
)
async with receive_stream:
async for frame in receive_stream:
# ignoring the case where `frame` is `bytes`, according to
# the docs, this assert should fail
assert isinstance(frame, str)
yield frame
finally:
await context.close()
async def main():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch()
async for event in events(browser):
print(event)
run(main)
Please let me know if you have any questions!