Best Python code snippet using playwright-python
test_websocket.py
Source:test_websocket.py
...45 assert ws.is_closed()46async def test_should_emit_frame_events(page, ws_server):47 sent = []48 received = []49 def on_web_socket(ws):50 ws.on("framesent", lambda payload: sent.append(payload))51 ws.on("framereceived", lambda payload: received.append(payload))52 page.on("websocket", on_web_socket)53 async with page.expect_event("websocket") as ws_info:54 await page.evaluate(55 """port => {56 const ws = new WebSocket('ws://localhost:' + port + '/ws');57 ws.addEventListener('open', () => {58 ws.send('echo-text');59 });60 }""",61 ws_server.PORT,62 )63 ws = await ws_info.value64 if not ws.is_closed():65 await ws.wait_for_event("close")66 assert sent == ["echo-text"]67 assert received == ["incoming", "text"]68async def test_should_emit_binary_frame_events(page, ws_server):69 sent = []70 received = []71 def on_web_socket(ws):72 ws.on("framesent", lambda payload: sent.append(payload))73 ws.on("framereceived", lambda payload: received.append(payload))74 page.on("websocket", on_web_socket)75 async with page.expect_event("websocket") as ws_info:76 await page.evaluate(77 """port => {78 const ws = new WebSocket('ws://localhost:' + port + '/ws');79 ws.addEventListener('open', () => {80 const binary = new Uint8Array(5);81 for (let i = 0; i < 5; ++i)82 binary[i] = i;83 ws.send(binary);84 ws.send('echo-bin');85 });...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!