11import random
22
33import pytest
4+ import pytest_asyncio
45from webdriver .bidi .modules .script import ContextTarget
56from webdriver .error import TimeoutException
67
910
1011pytestmark = pytest .mark .asyncio
1112
13+ DOWNLOAD_END = "browsingContext.downloadEnd"
1214DOWNLOAD_WILL_BEGIN = "browsingContext.downloadWillBegin"
1315NAVIGATION_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
5180async 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
103145async 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-
153205async 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