1010pytestmark = pytest .mark .asyncio
1111
1212DOWNLOAD_WILL_BEGIN = "browsingContext.downloadWillBegin"
13+ NAVIGATION_STARTED = "browsingContext.navigationStarted"
1314
1415
1516async def test_unsubscribe (bidi_session , inline , new_tab ):
@@ -47,8 +48,8 @@ async def on_event(method, data):
4748 remove_listener ()
4849
4950
50- async def test_subscribe (
51- bidi_session , new_tab , inline , wait_for_event , wait_for_future_safe
51+ async def test_download_attribute (
52+ bidi_session , subscribe_events , new_tab , inline , wait_for_event , wait_for_future_safe
5253):
5354 download_filename = f"download_filename{ random .random ()} .txt"
5455 download_link = "data:text/plain;charset=utf-8,"
@@ -60,9 +61,16 @@ async def test_subscribe(
6061 context = new_tab ["context" ], url = page_url , wait = "complete"
6162 )
6263
63- await bidi_session .session .subscribe (events = [DOWNLOAD_WILL_BEGIN ])
64- on_entry = wait_for_event (DOWNLOAD_WILL_BEGIN )
64+ await subscribe_events (events = [DOWNLOAD_WILL_BEGIN , NAVIGATION_STARTED ])
65+
66+ # Track all received events in the events array
67+ navigation_started_events = []
68+ async def on_event (method , data ):
69+ navigation_started_events .append (data )
70+
71+ remove_listener = bidi_session .add_event_listener (NAVIGATION_STARTED , on_event )
6572
73+ on_download_will_begin = wait_for_event (DOWNLOAD_WILL_BEGIN )
6674 # Test clicking on a link with a "download" attribute.
6775 await bidi_session .script .evaluate (
6876 expression = "download_link.click()" ,
@@ -71,21 +79,29 @@ async def test_subscribe(
7179 user_activation = True ,
7280 )
7381
74- event = await wait_for_future_safe (on_entry )
82+ event = await wait_for_future_safe (on_download_will_begin )
7583 recursive_compare (
7684 {
7785 "context" : new_tab ["context" ],
78- "navigation" : any_string ,
86+ # downloadWillBegin events created via a link with a download
87+ # attribute should have a `null` navigation id.
88+ "navigation" : None ,
7989 "suggestedFilename" : download_filename ,
8090 "timestamp" : any_int ,
8191 "url" : download_link ,
8292 },
8393 event ,
8494 )
8595
96+ # Check that no browsingContext.navigationStarted event was emitted
97+ with pytest .raises (TimeoutException ):
98+ await wait_for_bidi_events (bidi_session , navigation_started_events , 1 , timeout = 0.5 )
99+
100+ remove_listener ()
101+
86102
87103async def test_content_disposition_header (
88- bidi_session , new_tab , inline , wait_for_event , wait_for_future_safe , url
104+ bidi_session , subscribe_events , new_tab , inline , wait_for_event , wait_for_future_safe , url
89105):
90106 content_disposition_filename = f"content_disposition_filename{ random .random ()} .txt"
91107 content_disposition_link = url (
@@ -100,17 +116,20 @@ async def test_content_disposition_header(
100116 context = new_tab ["context" ], url = page_url , wait = "complete"
101117 )
102118
119+ await subscribe_events (events = [DOWNLOAD_WILL_BEGIN , NAVIGATION_STARTED ])
120+
103121 # Test clicking on a link which returns a response with a
104122 # Content-Disposition header.
105- on_entry = wait_for_event (DOWNLOAD_WILL_BEGIN )
123+ on_navigation_started = wait_for_event (NAVIGATION_STARTED )
124+ on_download_will_begin = wait_for_event (DOWNLOAD_WILL_BEGIN )
106125 await bidi_session .script .evaluate (
107126 expression = "content_disposition_link.click()" ,
108127 target = ContextTarget (new_tab ["context" ]),
109128 await_promise = True ,
110129 user_activation = True ,
111130 )
112131
113- event = await wait_for_future_safe (on_entry )
132+ download_event = await wait_for_future_safe (on_download_will_begin )
114133 recursive_compare (
115134 {
116135 "context" : new_tab ["context" ],
@@ -119,11 +138,20 @@ async def test_content_disposition_header(
119138 "timestamp" : any_int ,
120139 "url" : content_disposition_link ,
121140 },
122- event ,
141+ download_event ,
123142 )
124143
144+ navigation_event = await wait_for_future_safe (on_navigation_started )
145+
146+ # Check that the navigation id and url are identical for navigationStarted
147+ # and downloadWillBegin.
148+ assert download_event ["navigation" ] == navigation_event ["navigation" ]
149+ assert download_event ["url" ] == navigation_event ["url" ]
150+
151+
152+
125153async def test_redirect_to_content_disposition_header (
126- bidi_session , new_tab , inline , wait_for_event , wait_for_future_safe , url
154+ bidi_session , subscribe_events , new_tab , inline , wait_for_event , wait_for_future_safe , url
127155):
128156 redirect_filename = f"redirect_filename{ random .random ()} .txt"
129157 content_disposition_link = url (
@@ -142,17 +170,20 @@ async def test_redirect_to_content_disposition_header(
142170 context = new_tab ["context" ], url = page_url , wait = "complete"
143171 )
144172
173+ await subscribe_events (events = [DOWNLOAD_WILL_BEGIN , NAVIGATION_STARTED ])
174+
145175 # Test clicking on a link which redirects to a response with a
146176 # Content-Disposition header.
147- on_entry = wait_for_event (DOWNLOAD_WILL_BEGIN )
177+ on_navigation_started = wait_for_event (NAVIGATION_STARTED )
178+ on_download_will_begin = wait_for_event (DOWNLOAD_WILL_BEGIN )
148179 await bidi_session .script .evaluate (
149180 expression = "redirect_link.click()" ,
150181 target = ContextTarget (new_tab ["context" ]),
151182 await_promise = True ,
152183 user_activation = True ,
153184 )
154185
155- event = await wait_for_future_safe (on_entry )
186+ download_event = await wait_for_future_safe (on_download_will_begin )
156187 recursive_compare (
157188 {
158189 "context" : new_tab ["context" ],
@@ -161,5 +192,14 @@ async def test_redirect_to_content_disposition_header(
161192 "timestamp" : any_int ,
162193 "url" : content_disposition_link ,
163194 },
164- event ,
195+ download_event ,
165196 )
197+
198+ navigation_event = await wait_for_future_safe (on_navigation_started )
199+
200+ # Check that the navigation id is identical for navigationStarted and
201+ # downloadWillBegin.
202+ assert download_event ["navigation" ] == navigation_event ["navigation" ]
203+ # The url property will be different, the navigation event will have a url
204+ # set to the initial redirect url.
205+ assert navigation_event ["url" ] == redirect_link
0 commit comments