Skip to content

Commit d59301b

Browse files
authored
fix: migrate to new waitForEventInfo handling (#824)
1 parent bc7a802 commit d59301b

File tree

9 files changed

+113
-143
lines changed

9 files changed

+113
-143
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Install browsers
3131
run: python -m playwright install
3232
- name: Lint
33-
uses: pre-commit/action@v2.0.0
33+
uses: pre-commit/action@v2.0.3
3434
- name: Generate APIs
3535
run: bash scripts/update_api.sh
3636
- name: Verify generated API is up to date
@@ -126,10 +126,6 @@ jobs:
126126
browser-channel: msedge
127127
runs-on: ${{ matrix.os }}
128128
steps:
129-
- name: Install Media Pack
130-
if: matrix.os == 'windows-latest'
131-
shell: powershell
132-
run: Install-WindowsFeature Server-Media-Foundation
133129
- uses: actions/checkout@v2
134130
- name: Set up Python
135131
uses: actions/setup-python@v2
@@ -142,8 +138,8 @@ jobs:
142138
pip install -e .
143139
python setup.py bdist_wheel
144140
python -m playwright install-deps
145-
- name: Install ffmpeg
146-
run: python -m playwright install ffmpeg
141+
- name: Install browsers
142+
run: python -m playwright install ${{ matrix.browser-channel }}
147143
- name: Common Tests
148144
run: pytest tests/common --browser=chromium --browser-channel=${{ matrix.browser-channel }} --timeout 90
149145
- name: Test Sync API

playwright/_impl/_connection.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,6 @@ def __init__(
9999
if self._parent:
100100
self._parent._objects[guid] = self
101101

102-
def _wait_for_event_info_before(self, wait_id: str, api_name: str) -> None:
103-
self._connection._send_message_to_server(
104-
self._guid,
105-
"waitForEventInfo",
106-
{
107-
"info": {
108-
"apiName": api_name,
109-
"waitId": wait_id,
110-
"phase": "before",
111-
}
112-
},
113-
)
114-
115-
def _wait_for_event_info_after(
116-
self, wait_id: str, exception: Exception = None
117-
) -> None:
118-
info = {"waitId": wait_id, "phase": "after"}
119-
if exception:
120-
info["error"] = str(exception)
121-
self._connection._send_message_to_server(
122-
self._guid,
123-
"waitForEventInfo",
124-
{"info": info},
125-
)
126-
127102
def _dispose(self) -> None:
128103
# Clean up from parent and connection.
129104
if self._parent:

playwright/_impl/_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async def goto(
122122
def _setup_navigation_wait_helper(
123123
self, wait_name: str, timeout: float = None
124124
) -> WaitHelper:
125-
wait_helper = WaitHelper(self, f"frame.{wait_name}")
125+
wait_helper = WaitHelper(self._page, f"frame.{wait_name}")
126126
wait_helper.reject_on_event(
127127
self._page, "close", Error("Navigation failed because page was closed!")
128128
)

playwright/_impl/_wait_helper.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,41 @@
2424

2525

2626
class WaitHelper:
27-
def __init__(self, channel_owner: ChannelOwner, api_name: str) -> None:
27+
def __init__(self, channel_owner: ChannelOwner, event: str) -> None:
2828
self._result: asyncio.Future = asyncio.Future()
2929
self._wait_id = uuid.uuid4().hex
3030
self._loop = channel_owner._loop
3131
self._pending_tasks: List[Task] = []
32-
self._channel_owner = channel_owner
32+
self._channel = channel_owner._channel
3333
self._registered_listeners: List[Tuple[EventEmitter, str, Callable]] = []
34-
channel_owner._wait_for_event_info_before(self._wait_id, api_name)
34+
self._wait_for_event_info_before(self._wait_id, event)
35+
36+
def _wait_for_event_info_before(self, wait_id: str, event: str) -> None:
37+
self._channel.send_no_reply(
38+
"waitForEventInfo",
39+
{
40+
"info": {
41+
"waitId": wait_id,
42+
"phase": "before",
43+
"event": event,
44+
}
45+
},
46+
)
47+
48+
def _wait_for_event_info_after(self, wait_id: str, error: Exception = None) -> None:
49+
try:
50+
self._channel.send_no_reply(
51+
"waitForEventInfo",
52+
{
53+
"info": {
54+
"waitId": wait_id,
55+
"phase": "after",
56+
"error": str(error) if error else None,
57+
}
58+
},
59+
)
60+
except Exception:
61+
pass
3562

3663
def reject_on_event(
3764
self,
@@ -68,13 +95,13 @@ def _fulfill(self, result: Any) -> None:
6895
self._cleanup()
6996
if not self._result.done():
7097
self._result.set_result(result)
71-
self._channel_owner._wait_for_event_info_after(self._wait_id)
98+
self._wait_for_event_info_after(self._wait_id)
7299

73100
def _reject(self, exception: Exception) -> None:
74101
self._cleanup()
75102
if not self._result.done():
76103
self._result.set_exception(exception)
77-
self._channel_owner._wait_for_event_info_after(self._wait_id, exception)
104+
self._wait_for_event_info_after(self._wait_id, exception)
78105

79106
def wait_for_event(
80107
self,

tests/assets/networkidle.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src='networkidle.js'></script>

tests/assets/networkidle.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
async function main() {
2+
window.ws = new WebSocket('ws://localhost:' + window.location.port + '/ws');
3+
window.ws.addEventListener('message', message => {});
4+
5+
fetch('fetch-request-a.js');
6+
window.top.fetchSecond = () => {
7+
// Do not return the promise here.
8+
fetch('fetch-request-b.js');
9+
};
10+
}
11+
12+
main();

tests/async/test_accessibility.py

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -221,29 +221,17 @@ async def test_accessibility_filtering_children_of_leaf_nodes_rich_text_editable
221221
"children": [{"role": "text", "name": "my fake image"}],
222222
}
223223
else:
224-
if browser_channel:
225-
golden = {
226-
"role": "textbox",
227-
"name": "",
228-
"value": "Edit this image: ",
229-
"children": [
230-
{"role": "text", "name": "Edit this image:"},
231-
{"role": "img", "name": "my fake image"},
232-
],
233-
"value": "Edit this image: ",
234-
}
235-
else:
236-
golden = {
237-
"role": "textbox",
238-
"name": "",
239-
"multiline": True,
240-
"value": "Edit this image: ",
241-
"children": [
242-
{"role": "text", "name": "Edit this image:"},
243-
{"role": "img", "name": "my fake image"},
244-
],
245-
"value": "Edit this image: ",
246-
}
224+
golden = {
225+
"role": "textbox",
226+
"name": "",
227+
"multiline": True,
228+
"value": "Edit this image: ",
229+
"children": [
230+
{"role": "text", "name": "Edit this image:"},
231+
{"role": "img", "name": "my fake image"},
232+
],
233+
"value": "Edit this image: ",
234+
}
247235
snapshot = await page.accessibility.snapshot()
248236
assert snapshot["children"][0] == golden
249237

@@ -259,19 +247,12 @@ async def test_accessibility_plain_text_field_with_role_should_not_have_children
259247
<div contenteditable="plaintext-only" role='textbox'>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
260248
)
261249
snapshot = await page.accessibility.snapshot()
262-
if browser_channel:
263-
assert snapshot["children"][0] == {
264-
"name": "",
265-
"role": "textbox",
266-
"value": "Edit this image:",
267-
}
268-
else:
269-
assert snapshot["children"][0] == {
270-
"multiline": True,
271-
"name": "",
272-
"role": "textbox",
273-
"value": "Edit this image:",
274-
}
250+
assert snapshot["children"][0] == {
251+
"multiline": True,
252+
"name": "",
253+
"role": "textbox",
254+
"value": "Edit this image:",
255+
}
275256

276257

277258
@pytest.mark.only_browser("chromium")
@@ -283,14 +264,11 @@ async def test_accessibility_plain_text_field_without_role_should_not_have_conte
283264
<div contenteditable="plaintext-only">Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
284265
)
285266
snapshot = await page.accessibility.snapshot()
286-
if browser_channel:
287-
assert snapshot["children"][0] == {"name": "", "role": "generic"}
288-
else:
289-
assert snapshot["children"][0] == {
290-
"name": "",
291-
"role": "generic",
292-
"value": "Edit this image:",
293-
}
267+
assert snapshot["children"][0] == {
268+
"name": "",
269+
"role": "generic",
270+
"value": "Edit this image:",
271+
}
294272

295273

296274
@pytest.mark.only_browser("chromium")
@@ -302,14 +280,11 @@ async def test_accessibility_plain_text_field_with_tabindex_and_without_role_sho
302280
<div contenteditable="plaintext-only" tabIndex=0>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
303281
)
304282
snapshot = await page.accessibility.snapshot()
305-
if browser_channel:
306-
assert snapshot["children"][0] == {"name": "", "role": "generic"}
307-
else:
308-
assert snapshot["children"][0] == {
309-
"name": "",
310-
"role": "generic",
311-
"value": "Edit this image:",
312-
}
283+
assert snapshot["children"][0] == {
284+
"name": "",
285+
"role": "generic",
286+
"value": "Edit this image:",
287+
}
313288

314289

315290
async def test_accessibility_non_editable_textbox_with_role_and_tabIndex_and_label_should_not_have_children(

tests/async/test_navigation.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
import pytest
2121

22-
from playwright.async_api import Error, Request, TimeoutError
22+
from playwright.async_api import Error, Page, Request, TimeoutError
23+
from tests.server import Server
2324

2425

2526
async def test_goto_should_work(page, server):
@@ -676,6 +677,14 @@ def handler(request: Any):
676677
await page.wait_for_load_state("domcontentloaded")
677678

678679

680+
async def test_wait_for_load_state_networkidle(page: Page, server: Server):
681+
wait_for_network_idle_future = asyncio.create_task(
682+
page.wait_for_load_state("networkidle")
683+
)
684+
await page.goto(server.PREFIX + "/networkidle.html")
685+
await wait_for_network_idle_future
686+
687+
679688
async def test_wait_for_load_state_should_work_with_pages_that_have_loaded_before_being_connected_to(
680689
page, context, server
681690
):

tests/sync/test_accessibility.py

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -215,29 +215,17 @@ def test_accessibility_filtering_children_of_leaf_nodes_rich_text_editable_field
215215
"children": [{"role": "text", "name": "my fake image"}],
216216
}
217217
else:
218-
if browser_channel:
219-
golden = {
220-
"role": "textbox",
221-
"name": "",
222-
"value": "Edit this image: ",
223-
"children": [
224-
{"role": "text", "name": "Edit this image:"},
225-
{"role": "img", "name": "my fake image"},
226-
],
227-
"value": "Edit this image: ",
228-
}
229-
else:
230-
golden = {
231-
"role": "textbox",
232-
"name": "",
233-
"multiline": True,
234-
"value": "Edit this image: ",
235-
"children": [
236-
{"role": "text", "name": "Edit this image:"},
237-
{"role": "img", "name": "my fake image"},
238-
],
239-
"value": "Edit this image: ",
240-
}
218+
golden = {
219+
"role": "textbox",
220+
"name": "",
221+
"multiline": True,
222+
"value": "Edit this image: ",
223+
"children": [
224+
{"role": "text", "name": "Edit this image:"},
225+
{"role": "img", "name": "my fake image"},
226+
],
227+
"value": "Edit this image: ",
228+
}
241229
snapshot = page.accessibility.snapshot()
242230
assert snapshot["children"][0] == golden
243231

@@ -253,19 +241,12 @@ def test_accessibility_plain_text_field_with_role_should_not_have_children(
253241
<div contenteditable="plaintext-only" role='textbox'>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
254242
)
255243
snapshot = page.accessibility.snapshot()
256-
if browser_channel:
257-
assert snapshot["children"][0] == {
258-
"name": "",
259-
"role": "textbox",
260-
"value": "Edit this image:",
261-
}
262-
else:
263-
assert snapshot["children"][0] == {
264-
"multiline": True,
265-
"name": "",
266-
"role": "textbox",
267-
"value": "Edit this image:",
268-
}
244+
assert snapshot["children"][0] == {
245+
"multiline": True,
246+
"name": "",
247+
"role": "textbox",
248+
"value": "Edit this image:",
249+
}
269250

270251

271252
@pytest.mark.only_browser("chromium")
@@ -277,14 +258,11 @@ def test_accessibility_plain_text_field_without_role_should_not_have_content(
277258
<div contenteditable="plaintext-only">Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
278259
)
279260
snapshot = page.accessibility.snapshot()
280-
if browser_channel:
281-
assert snapshot["children"][0] == {"name": "", "role": "generic"}
282-
else:
283-
assert snapshot["children"][0] == {
284-
"name": "",
285-
"role": "generic",
286-
"value": "Edit this image:",
287-
}
261+
assert snapshot["children"][0] == {
262+
"name": "",
263+
"role": "generic",
264+
"value": "Edit this image:",
265+
}
288266

289267

290268
@pytest.mark.only_browser("chromium")
@@ -296,14 +274,11 @@ def test_accessibility_plain_text_field_with_tabindex_and_without_role_should_no
296274
<div contenteditable="plaintext-only" tabIndex=0>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
297275
)
298276
snapshot = page.accessibility.snapshot()
299-
if browser_channel:
300-
assert snapshot["children"][0] == {"name": "", "role": "generic"}
301-
else:
302-
assert snapshot["children"][0] == {
303-
"name": "",
304-
"role": "generic",
305-
"value": "Edit this image:",
306-
}
277+
assert snapshot["children"][0] == {
278+
"name": "",
279+
"role": "generic",
280+
"value": "Edit this image:",
281+
}
307282

308283

309284
def test_accessibility_non_editable_textbox_with_role_and_tabIndex_and_label_should_not_have_children(

0 commit comments

Comments
 (0)