Skip to content

Commit 3d8fdcd

Browse files
juliandescottesmoz-wptsync-bot
authored andcommitted
[wdspec] Wait for download_end events in bidi download tests
Differential Revision: https://phabricator.services.mozilla.com/D272423 bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1999473 gecko-commit: 5643a89045395263f5cec1ac8b60373740af14ee gecko-reviewers: whimboo
1 parent 8293902 commit 3d8fdcd

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

lint.ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ PRINT STATEMENT: dom/nodes/Document-createElement-namespace-tests/generate.py
109109
PRINT STATEMENT: encrypted-media/polyfill/make-polyfill-tests.py
110110
PRINT STATEMENT: resources/test/conftest.py
111111
PRINT STATEMENT: webdriver/tests/support/helpers.py
112+
PRINT STATEMENT: webdriver/tests/bidi/browser/set_download_behavior/conftest.py
112113

113114
# semi-legitimate use of console.*
114115
CONSOLE: console/*

webdriver/tests/bidi/browser/set_download_behavior/conftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async def trigger_download(context):
7070

7171
await subscribe_events(events=[DOWNLOAD_END])
7272

73-
on_download_will_begin = wait_for_event(DOWNLOAD_END)
73+
on_download_end = wait_for_event(DOWNLOAD_END)
7474
# Trigger download by clicking the link.
7575
await bidi_session.script.evaluate(
7676
expression="download_link.click()",
@@ -80,10 +80,11 @@ async def trigger_download(context):
8080
)
8181

8282
try:
83+
print("Wait for browsingContext.downloadEnd event")
8384
return await wait_for_future_safe(
84-
on_download_will_begin, timeout=0.5)
85+
on_download_end, timeout=2.0)
8586
except TimeoutException:
86-
# User Agent showed file save dialog.
87+
print("User Agent showed file save dialog")
8788
return None
8889

8990
return trigger_download

webdriver/tests/bidi/browsing_context/download_will_begin/download_will_begin.py

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import random
22

33
import pytest
4+
import pytest_asyncio
45
from webdriver.bidi.modules.script import ContextTarget
56
from webdriver.error import TimeoutException
67

@@ -9,11 +10,36 @@
910

1011
pytestmark = pytest.mark.asyncio
1112

13+
DOWNLOAD_END = "browsingContext.downloadEnd"
1214
DOWNLOAD_WILL_BEGIN = "browsingContext.downloadWillBegin"
1315
NAVIGATION_STARTED = "browsingContext.navigationStarted"
1416

1517

16-
async def test_unsubscribe(bidi_session, inline, new_tab):
18+
# This fixture is a workaround until we can cancel downloads.
19+
# https://github.com/w3c/webdriver-bidi/issues/1031
20+
@pytest_asyncio.fixture
21+
async def expect_download_end(bidi_session, subscribe_events):
22+
await subscribe_events(events=[DOWNLOAD_END])
23+
24+
download_end_events = []
25+
26+
async def on_event(method, data):
27+
download_end_events.append(data)
28+
29+
remove_listener = bidi_session.add_event_listener(DOWNLOAD_END, on_event)
30+
31+
expected_events = 0
32+
def _expect_download_end(count):
33+
nonlocal expected_events
34+
expected_events = count
35+
36+
yield _expect_download_end
37+
38+
await wait_for_bidi_events(bidi_session, download_end_events, expected_events, timeout=2)
39+
remove_listener()
40+
41+
42+
async def test_unsubscribe(bidi_session, inline, new_tab, expect_download_end):
1743
filename = f"some_file_name{random.random()}.txt"
1844
download_link = "data:text/plain;charset=utf-8,"
1945
url = inline(
@@ -35,6 +61,9 @@ async def on_event(method, data):
3561

3662
remove_listener = bidi_session.add_event_listener(DOWNLOAD_WILL_BEGIN, on_event)
3763

64+
# Expect one downloadEnd event for this test.
65+
expect_download_end(1)
66+
3867
await bidi_session.script.evaluate(
3968
expression="download_link.click()",
4069
target=ContextTarget(new_tab["context"]),
@@ -49,7 +78,13 @@ async def on_event(method, data):
4978

5079

5180
async def test_download_attribute(
52-
bidi_session, subscribe_events, new_tab, inline, wait_for_event, wait_for_future_safe
81+
bidi_session,
82+
subscribe_events,
83+
new_tab,
84+
inline,
85+
wait_for_event,
86+
wait_for_future_safe,
87+
expect_download_end,
5388
):
5489
download_filename = f"download_filename{random.random()}.txt"
5590
download_link = "data:text/plain;charset=utf-8,"
@@ -65,12 +100,17 @@ async def test_download_attribute(
65100

66101
# Track all received events in the events array
67102
navigation_started_events = []
103+
68104
async def on_event(method, data):
69105
navigation_started_events.append(data)
70106

71107
remove_listener = bidi_session.add_event_listener(NAVIGATION_STARTED, on_event)
72108

73109
on_download_will_begin = wait_for_event(DOWNLOAD_WILL_BEGIN)
110+
111+
# Expect one downloadEnd event for this test.
112+
expect_download_end(1)
113+
74114
# Test clicking on a link with a "download" attribute.
75115
await bidi_session.script.evaluate(
76116
expression="download_link.click()",
@@ -95,13 +135,22 @@ async def on_event(method, data):
95135

96136
# Check that no browsingContext.navigationStarted event was emitted
97137
with pytest.raises(TimeoutException):
98-
await wait_for_bidi_events(bidi_session, navigation_started_events, 1, timeout=0.5)
138+
await wait_for_bidi_events(
139+
bidi_session, navigation_started_events, 1, timeout=0.5
140+
)
99141

100142
remove_listener()
101143

102144

103145
async def test_content_disposition_header(
104-
bidi_session, subscribe_events, new_tab, inline, wait_for_event, wait_for_future_safe, url
146+
bidi_session,
147+
subscribe_events,
148+
new_tab,
149+
inline,
150+
wait_for_event,
151+
wait_for_future_safe,
152+
url,
153+
expect_download_end,
105154
):
106155
content_disposition_filename = f"content_disposition_filename{random.random()}.txt"
107156
content_disposition_link = url(
@@ -122,6 +171,10 @@ async def test_content_disposition_header(
122171
# Content-Disposition header.
123172
on_navigation_started = wait_for_event(NAVIGATION_STARTED)
124173
on_download_will_begin = wait_for_event(DOWNLOAD_WILL_BEGIN)
174+
175+
# Expect one downloadEnd event for this test.
176+
expect_download_end(1)
177+
125178
await bidi_session.script.evaluate(
126179
expression="content_disposition_link.click()",
127180
target=ContextTarget(new_tab["context"]),
@@ -149,9 +202,15 @@ async def test_content_disposition_header(
149202
assert download_event["url"] == navigation_event["url"]
150203

151204

152-
153205
async def test_redirect_to_content_disposition_header(
154-
bidi_session, subscribe_events, new_tab, inline, wait_for_event, wait_for_future_safe, url
206+
bidi_session,
207+
subscribe_events,
208+
new_tab,
209+
inline,
210+
wait_for_event,
211+
wait_for_future_safe,
212+
url,
213+
expect_download_end,
155214
):
156215
redirect_filename = f"redirect_filename{random.random()}.txt"
157216
content_disposition_link = url(
@@ -176,6 +235,10 @@ async def test_redirect_to_content_disposition_header(
176235
# Content-Disposition header.
177236
on_navigation_started = wait_for_event(NAVIGATION_STARTED)
178237
on_download_will_begin = wait_for_event(DOWNLOAD_WILL_BEGIN)
238+
239+
# Expect one downloadEnd event for this test.
240+
expect_download_end(1)
241+
179242
await bidi_session.script.evaluate(
180243
expression="redirect_link.click()",
181244
target=ContextTarget(new_tab["context"]),

0 commit comments

Comments
 (0)