How to use test_browser_type_connect_should_forward_close_events_to_pages 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_browsertype_connect.py

Source: test_browsertype_connect.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 time
16from typing import Callable
17
18import pytest
19
20from playwright.sync_api import BrowserType, Error, Playwright, Route
21from tests.conftest import RemoteServer
22from tests.server import Server
23
24
25def test_browser_type_connect_slow_mo(
26    server: Server, browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
27) -> None:
28    remote_server = launch_server()
29    browser = browser_type.connect(remote_server.ws_endpoint, slow_mo=100)
30    browser_context = browser.new_context()
31    t1 = time.monotonic()
32    page = browser_context.new_page()
33    assert page.evaluate("11 * 11") == 121
34    assert (time.monotonic() - t1) >= 0.2
35    page.goto(server.EMPTY_PAGE)
36    browser.close()
37
38
39def test_browser_type_connect_should_be_able_to_reconnect_to_a_browser(
40    server: Server, browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
41) -> None:
42    remote_server = launch_server()
43    browser = browser_type.connect(remote_server.ws_endpoint)
44    browser_context = browser.new_context()
45    assert len(browser_context.pages) == 0
46    page = browser_context.new_page()
47    assert len(browser_context.pages) == 1
48    assert page.evaluate("11 * 11") == 121
49    page.goto(server.EMPTY_PAGE)
50    browser.close()
51
52    browser = browser_type.connect(remote_server.ws_endpoint)
53    browser_context = browser.new_context()
54    page = browser_context.new_page()
55    page.goto(server.EMPTY_PAGE)
56    browser.close()
57
58
59def test_browser_type_connect_should_be_able_to_connect_two_browsers_at_the_same_time(
60    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
61) -> None:
62    remote_server = launch_server()
63    browser1 = browser_type.connect(remote_server.ws_endpoint)
64    assert len(browser1.contexts) == 0
65    browser1.new_context()
66    assert len(browser1.contexts) == 1
67
68    browser2 = browser_type.connect(remote_server.ws_endpoint)
69    assert len(browser2.contexts) == 0
70    browser2.new_context()
71    assert len(browser2.contexts) == 1
72    assert len(browser1.contexts) == 1
73
74    browser1.close()
75    page2 = browser2.new_page()
76    # original browser should still work
77    assert page2.evaluate("7 * 6") == 42
78
79    browser2.close()
80
81
82def test_browser_type_connect_disconnected_event_should_be_emitted_when_browser_is_closed_or_server_is_closed(
83    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
84) -> None:
85    # Launch another server to not affect other tests.
86    remote = launch_server()
87
88    browser1 = browser_type.connect(remote.ws_endpoint)
89    browser2 = browser_type.connect(remote.ws_endpoint)
90
91    disconnected1 = []
92    disconnected2 = []
93    browser1.on("disconnected", lambda browser: disconnected1.append(True))
94    browser2.on("disconnected", lambda browser: disconnected2.append(True))
95
96    page2 = browser2.new_page()
97
98    browser1.close()
99    assert len(disconnected1) == 1
100    assert len(disconnected2) == 0
101
102    remote.kill()
103    assert len(disconnected1) == 1
104
105    with pytest.raises(Error):
106        # Tickle connection so that it gets a chance to dispatch disconnect event.
107        page2.title()
108    assert len(disconnected2) == 1
109
110
111def test_browser_type_disconnected_event_should_have_browser_as_argument(
112    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
113) -> None:
114    remote_server = launch_server()
115    browser = browser_type.connect(remote_server.ws_endpoint)
116    event_payloads = []
117    browser.on("disconnected", lambda b: event_payloads.append(b))
118    browser.close()
119    assert event_payloads[0] == browser
120
121
122def test_browser_type_connect_set_browser_connected_state(
123    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
124) -> None:
125    remote_server = launch_server()
126    browser = browser_type.connect(remote_server.ws_endpoint)
127    assert browser.is_connected()
128    browser.close()
129    assert browser.is_connected() is False
130
131
132def test_browser_type_connect_should_throw_when_used_after_is_connected_returns_false(
133    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
134) -> None:
135    remote_server = launch_server()
136    browser = browser_type.connect(remote_server.ws_endpoint)
137    page = browser.new_page()
138
139    remote_server.kill()
140
141    with pytest.raises(Error) as exc_info:
142        page.evaluate("1 + 1")
143    assert "Playwright connection closed" == exc_info.value.message
144    assert browser.is_connected() is False
145
146
147def test_browser_type_connect_should_forward_close_events_to_pages(
148    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
149) -> None:
150    # Launch another server to not affect other tests.
151    remote = launch_server()
152
153    browser = browser_type.connect(remote.ws_endpoint)
154    context = browser.new_context()
155    page = context.new_page()
156
157    events = []
158    browser.on("disconnected", lambda browser: events.append("browser::disconnected"))
159    context.on("close", lambda context: events.append("context::close"))
160    page.on("close", lambda page: events.append("page::close"))
161
162    browser.close()
163    assert events == ["page::close", "context::close", "browser::disconnected"]
164    remote.kill()
165    assert events == ["page::close", "context::close", "browser::disconnected"]
166
167
168def test_browser_type_connect_should_forward_close_events_on_remote_kill(
169    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
170) -> None:
171    # Launch another server to not affect other tests.
172    remote = launch_server()
173
174    browser = browser_type.connect(remote.ws_endpoint)
175    context = browser.new_context()
176    page = context.new_page()
177
178    events = []
179    browser.on("disconnected", lambda context: events.append("browser::disconnected"))
180    context.on("close", lambda context: events.append("context::close"))
181    page.on("close", lambda page: events.append("page::close"))
182    remote.kill()
183    with pytest.raises(Error):
184        page.title()
185    assert events == ["page::close", "context::close", "browser::disconnected"]
186
187
188def test_connect_to_closed_server_without_hangs(
189    browser_type: BrowserType, launch_server: Callable[[], RemoteServer]
190) -> None:
191    remote_server = launch_server()
192    remote_server.kill()
193    with pytest.raises(Error) as exc:
194        browser_type.connect(remote_server.ws_endpoint)
195    assert "websocket.connect: " in exc.value.message
196
197
198def test_browser_type_connect_should_fulfill_with_global_fetch_result(
199    browser_type: BrowserType,
200    launch_server: Callable[[], RemoteServer],
201    playwright: Playwright,
202    server: Server,
203) -> None:
204    # Launch another server to not affect other tests.
205    remote = launch_server()
206
207    browser = browser_type.connect(remote.ws_endpoint)
208    context = browser.new_context()
209    page = context.new_page()
210
211    def handle_request(route: Route) -> None:
212        request = playwright.request.new_context()
213        response = request.get(server.PREFIX + "/simple.json")
214        return route.fulfill(response=response)
215
216    page.route("**/*", handle_request)
217
218    response = page.goto(server.EMPTY_PAGE)
219    assert response
220    assert response.status == 200
221    assert response.json() == {"foo": "bar"}
222
223    remote.kill()
224
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)