11import asyncio
22
33import pytest
4+ import webdriver .bidi .error as error
45from webdriver .bidi .modules .script import ContextTarget
56
67from . import navigate_and_assert
78from ... import any_string
89
910pytestmark = pytest .mark .asyncio
1011
12+ USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"
13+
1114
1215async def test_payload (bidi_session , inline , new_tab ):
1316 url = inline ("<div>foo</div>" )
@@ -81,12 +84,10 @@ async def test_relative_url(bidi_session, new_tab, url):
8184 "/webdriver/tests/bidi/browsing_context/support/empty.html"
8285 )
8386
84- # Navigate to page1 with wait=interactive to make sure the document's base URI
85- # was updated.
86- await navigate_and_assert (bidi_session , new_tab , url_before , "interactive" )
87+ await navigate_and_assert (bidi_session , new_tab , url_before , "none" )
8788
8889 url_after = url_before .replace ("empty.html" , "other.html" )
89- await navigate_and_assert (bidi_session , new_tab , url_after , "interactive " )
90+ await navigate_and_assert (bidi_session , new_tab , url_after , "none " )
9091
9192
9293async def test_same_document_navigation_in_before_unload (bidi_session , new_tab , url ):
@@ -110,25 +111,44 @@ async def test_same_document_navigation_in_before_unload(bidi_session, new_tab,
110111
111112
112113@pytest .mark .capabilities ({"unhandledPromptBehavior" : {'beforeUnload' : 'ignore' }})
113- async def test_wait_none_with_beforeunload_prompt (
114- bidi_session , new_tab , setup_beforeunload_page , inline
115- ):
114+ @pytest .mark .parametrize ("value" , ["none" , "interactive" , "complete" ])
115+ @pytest .mark .parametrize ("accept" , [True , False ])
116+ async def test_navigate_with_beforeunload_prompt (bidi_session , new_tab ,
117+ setup_beforeunload_page , inline , subscribe_events , wait_for_event ,
118+ wait_for_future_safe , value , accept ):
116119 await setup_beforeunload_page (new_tab )
117120
121+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
122+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
123+
118124 url_after = inline ("<div>foo</div>" )
119125
120- result = await bidi_session .browsing_context .navigate (
121- context = new_tab ["context" ], url = url_after , wait = "none"
126+ navigated_future = asyncio .create_task (
127+ bidi_session .browsing_context .navigate (context = new_tab ["context" ],
128+ url = url_after , wait = value ))
129+
130+ # Wait for the prompt to open.
131+ await wait_for_future_safe (on_prompt_opened )
132+ # Make sure the navigation is not finished.
133+ assert not navigated_future .done ()
134+
135+ await bidi_session .browsing_context .handle_user_prompt (
136+ context = new_tab ["context" ], accept = accept
122137 )
123138
124- assert result ["url" ] == url_after
125- any_string (result ["navigation" ])
139+ if accept :
140+ await navigated_future
141+ else :
142+ with pytest .raises (error .UnknownErrorException ):
143+ await wait_for_future_safe (navigated_future )
126144
127145
128146@pytest .mark .capabilities ({"unhandledPromptBehavior" : {'beforeUnload' : 'ignore' }})
129- async def test_wait_none_with_beforeunload_prompt_in_iframe (
130- bidi_session , new_tab , setup_beforeunload_page , inline
131- ):
147+ @pytest .mark .parametrize ("value" , ["none" , "interactive" , "complete" ])
148+ @pytest .mark .parametrize ("accept" , [True , False ])
149+ async def test_navigate_with_beforeunload_prompt_in_iframe (bidi_session ,
150+ new_tab , setup_beforeunload_page , inline , subscribe_events ,
151+ wait_for_event , wait_for_future_safe , value , accept ):
132152 page = inline (f"""<iframe src={ inline ("foo" )} ></iframe>""" )
133153 await bidi_session .browsing_context .navigate (
134154 context = new_tab ["context" ], url = page , wait = "complete"
@@ -139,35 +159,69 @@ async def test_wait_none_with_beforeunload_prompt_in_iframe(
139159
140160 await setup_beforeunload_page (iframe_context )
141161
162+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
163+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
164+
142165 url_after = inline ("<div>foo</div>" )
143166
144- result = await bidi_session .browsing_context .navigate (
145- context = iframe_context ["context" ], url = url_after , wait = "none"
167+ navigated_future = asyncio .create_task (
168+ bidi_session .browsing_context .navigate (
169+ context = iframe_context ["context" ], url = url_after , wait = value ))
170+
171+ # Wait for the prompt to open.
172+ await wait_for_future_safe (on_prompt_opened )
173+ # Make sure the navigation is not finished.
174+ assert not navigated_future .done ()
175+
176+ await bidi_session .browsing_context .handle_user_prompt (
177+ context = new_tab ["context" ], accept = accept
146178 )
147179
148- assert result ["url" ] == url_after
149- any_string (result ["navigation" ])
180+ if accept :
181+ await navigated_future
182+ else :
183+ with pytest .raises (error .UnknownErrorException ):
184+ await wait_for_future_safe (navigated_future )
150185
151186
152187@pytest .mark .capabilities ({"unhandledPromptBehavior" : {'beforeUnload' : 'ignore' }})
153- async def test_wait_none_with_beforeunload_prompt_in_iframe_navigate_in_top_context (
154- bidi_session , new_tab , setup_beforeunload_page , inline
155- ):
188+ @pytest .mark .parametrize ("value" , ["none" , "interactive" , "complete" ])
189+ @pytest .mark .parametrize ("accept" , [True , False ])
190+ async def test_navigate_with_beforeunload_prompt_in_iframe_navigate_in_top_context (
191+ bidi_session , new_tab , setup_beforeunload_page , inline ,
192+ subscribe_events , wait_for_event , wait_for_future_safe , value , accept ):
156193 page = inline (f"""<iframe src={ inline ("foo" )} ></iframe>""" )
157194 await bidi_session .browsing_context .navigate (
158195 context = new_tab ["context" ], url = page , wait = "complete"
159196 )
160197
161- contexts = await bidi_session .browsing_context .get_tree (root = new_tab ["context" ])
198+ contexts = await bidi_session .browsing_context .get_tree (
199+ root = new_tab ["context" ])
162200 iframe_context = contexts [0 ]["children" ][0 ]
163201
164202 await setup_beforeunload_page (iframe_context )
165203
204+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
205+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
206+
166207 url_after = inline ("<div>foo</div>" )
167208
168- result = await bidi_session .browsing_context .navigate (
169- context = new_tab ["context" ], url = url_after , wait = "none"
209+ navigated_future = asyncio .create_task (
210+ bidi_session .browsing_context .navigate (
211+ context = new_tab ["context" ], url = url_after , wait = value
212+ ))
213+
214+ # Wait for the prompt to open.
215+ await wait_for_future_safe (on_prompt_opened )
216+ # Make sure the navigation is not finished.
217+ assert not navigated_future .done ()
218+
219+ await bidi_session .browsing_context .handle_user_prompt (
220+ context = new_tab ["context" ], accept = accept
170221 )
171222
172- assert result ["url" ] == url_after
173- any_string (result ["navigation" ])
223+ if accept :
224+ await navigated_future
225+ else :
226+ with pytest .raises (error .UnknownErrorException ):
227+ await wait_for_future_safe (navigated_future )
0 commit comments