How to use test_should_receive_events method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_cdp_session.py

Source: test_cdp_session.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import pytest
16
17from playwright.sync_api import Browser, Error, Page
18from tests.server import Server
19
20
21@pytest.mark.only_browser("chromium")
22def test_should_work(page: Page) -> None:
23    client = page.context.new_cdp_session(page)
24    events = []
25    client.on("Runtime.consoleAPICalled", lambda params: events.append(params))
26    client.send("Runtime.enable")
27    result = client.send(
28        "Runtime.evaluate",
29        {"expression": "window.foo = 'bar'; console.log('log'); 'result'"},
30    )
31    assert result == {"result": {"type": "string", "value": "result"}}
32    foo = page.evaluate("() => window.foo")
33    assert foo == "bar"
34    assert events[0]["args"][0]["value"] == "log"
35
36
37@pytest.mark.only_browser("chromium")
38def test_should_receive_events(page: Page, server: Server) -> None:
39    client = page.context.new_cdp_session(page)
40    client.send("Network.enable")
41    events = []
42    client.on("Network.requestWillBeSent", lambda event: events.append(event))
43    page.goto(server.EMPTY_PAGE)
44    assert len(events) == 1
45
46
47@pytest.mark.only_browser("chromium")
48def test_should_be_able_to_detach_session(page: Page) -> None:
49    client = page.context.new_cdp_session(page)
50    client.send("Runtime.enable")
51    eval_response = client.send(
52        "Runtime.evaluate", {"expression": "1 + 2", "returnByValue": True}
53    )
54    assert eval_response["result"]["value"] == 3
55    client.detach()
56    with pytest.raises(Error) as exc_info:
57        client.send("Runtime.evaluate", {"expression": "3 + 1", "returnByValue": True})
58    assert "Target page, context or browser has been closed" in exc_info.value.message
59
60
61@pytest.mark.only_browser("chromium")
62def test_should_not_break_page_close(browser: Browser) -> None:
63    context = browser.new_context()
64    page = context.new_page()
65    session = page.context.new_cdp_session(page)
66    session.detach()
67    page.close()
68    context.close()
69
70
71@pytest.mark.only_browser("chromium")
72def test_should_detach_when_page_closes(browser: Browser) -> None:
73    context = browser.new_context()
74    page = context.new_page()
75    session = context.new_cdp_session(page)
76    page.close()
77    with pytest.raises(Error):
78        session.detach()
79    context.close()
80
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)