8
8
9
9
pytestmark = pytest .mark .asyncio
10
10
11
+ USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"
12
+
11
13
12
14
async def test_payload (bidi_session , inline , new_tab ):
13
15
url = inline ("<div>foo</div>" )
@@ -81,12 +83,10 @@ async def test_relative_url(bidi_session, new_tab, url):
81
83
"/webdriver/tests/bidi/browsing_context/support/empty.html"
82
84
)
83
85
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" )
86
+ await navigate_and_assert (bidi_session , new_tab , url_before , "none" )
87
87
88
88
url_after = url_before .replace ("empty.html" , "other.html" )
89
- await navigate_and_assert (bidi_session , new_tab , url_after , "interactive " )
89
+ await navigate_and_assert (bidi_session , new_tab , url_after , "none " )
90
90
91
91
92
92
async def test_same_document_navigation_in_before_unload (bidi_session , new_tab , url ):
@@ -110,25 +110,44 @@ async def test_same_document_navigation_in_before_unload(bidi_session, new_tab,
110
110
111
111
112
112
@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
- ):
113
+ @pytest .mark .parametrize ("value" , ["none" , "interactive" , "complete" ])
114
+ @pytest .mark .parametrize ("accept" , [True , False ])
115
+ async def test_navigate_with_beforeunload_prompt (bidi_session , new_tab ,
116
+ setup_beforeunload_page , inline , subscribe_events , wait_for_event ,
117
+ wait_for_future_safe , value , accept ):
116
118
await setup_beforeunload_page (new_tab )
117
119
120
+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
121
+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
122
+
118
123
url_after = inline ("<div>foo</div>" )
119
124
120
- result = await bidi_session .browsing_context .navigate (
121
- context = new_tab ["context" ], url = url_after , wait = "none"
125
+ navigated_future = asyncio .create_task (
126
+ bidi_session .browsing_context .navigate (context = new_tab ["context" ],
127
+ url = url_after , wait = value ))
128
+
129
+ # Wait for the prompt to open.
130
+ await wait_for_future_safe (on_prompt_opened )
131
+ # Make sure the navigation is not finished.
132
+ assert not navigated_future .done ()
133
+
134
+ await bidi_session .browsing_context .handle_user_prompt (
135
+ context = new_tab ["context" ], accept = accept
122
136
)
123
137
124
- assert result ["url" ] == url_after
125
- any_string (result ["navigation" ])
138
+ if accept :
139
+ await navigated_future
140
+ else :
141
+ with pytest .raises (error .UnknownErrorException ):
142
+ await wait_for_future_safe (navigated_future )
126
143
127
144
128
145
@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
- ):
146
+ @pytest .mark .parametrize ("value" , ["none" , "interactive" , "complete" ])
147
+ @pytest .mark .parametrize ("accept" , [True , False ])
148
+ async def test_navigate_with_beforeunload_prompt_in_iframe (bidi_session ,
149
+ new_tab , setup_beforeunload_page , inline , subscribe_events ,
150
+ wait_for_event , wait_for_future_safe , value , accept ):
132
151
page = inline (f"""<iframe src={ inline ("foo" )} ></iframe>""" )
133
152
await bidi_session .browsing_context .navigate (
134
153
context = new_tab ["context" ], url = page , wait = "complete"
@@ -139,35 +158,69 @@ async def test_wait_none_with_beforeunload_prompt_in_iframe(
139
158
140
159
await setup_beforeunload_page (iframe_context )
141
160
161
+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
162
+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
163
+
142
164
url_after = inline ("<div>foo</div>" )
143
165
144
- result = await bidi_session .browsing_context .navigate (
145
- context = iframe_context ["context" ], url = url_after , wait = "none"
166
+ navigated_future = asyncio .create_task (
167
+ bidi_session .browsing_context .navigate (
168
+ context = iframe_context ["context" ], url = url_after , wait = value ))
169
+
170
+ # Wait for the prompt to open.
171
+ await wait_for_future_safe (on_prompt_opened )
172
+ # Make sure the navigation is not finished.
173
+ assert not navigated_future .done ()
174
+
175
+ await bidi_session .browsing_context .handle_user_prompt (
176
+ context = new_tab ["context" ], accept = accept
146
177
)
147
178
148
- assert result ["url" ] == url_after
149
- any_string (result ["navigation" ])
179
+ if accept :
180
+ await navigated_future
181
+ else :
182
+ with pytest .raises (error .UnknownErrorException ):
183
+ await wait_for_future_safe (navigated_future )
150
184
151
185
152
186
@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
- ):
187
+ @pytest .mark .parametrize ("value" , ["none" , "interactive" , "complete" ])
188
+ @pytest .mark .parametrize ("accept" , [True , False ])
189
+ async def test_navigate_with_beforeunload_prompt_in_iframe_navigate_in_top_context (
190
+ bidi_session , new_tab , setup_beforeunload_page , inline ,
191
+ subscribe_events , wait_for_event , wait_for_future_safe , value , accept ):
156
192
page = inline (f"""<iframe src={ inline ("foo" )} ></iframe>""" )
157
193
await bidi_session .browsing_context .navigate (
158
194
context = new_tab ["context" ], url = page , wait = "complete"
159
195
)
160
196
161
- contexts = await bidi_session .browsing_context .get_tree (root = new_tab ["context" ])
197
+ contexts = await bidi_session .browsing_context .get_tree (
198
+ root = new_tab ["context" ])
162
199
iframe_context = contexts [0 ]["children" ][0 ]
163
200
164
201
await setup_beforeunload_page (iframe_context )
165
202
203
+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
204
+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
205
+
166
206
url_after = inline ("<div>foo</div>" )
167
207
168
- result = await bidi_session .browsing_context .navigate (
169
- context = new_tab ["context" ], url = url_after , wait = "none"
208
+ navigated_future = asyncio .create_task (
209
+ bidi_session .browsing_context .navigate (
210
+ context = new_tab ["context" ], url = url_after , wait = value
211
+ ))
212
+
213
+ # Wait for the prompt to open.
214
+ await wait_for_future_safe (on_prompt_opened )
215
+ # Make sure the navigation is not finished.
216
+ assert not navigated_future .done ()
217
+
218
+ await bidi_session .browsing_context .handle_user_prompt (
219
+ context = new_tab ["context" ], accept = accept
170
220
)
171
221
172
- assert result ["url" ] == url_after
173
- any_string (result ["navigation" ])
222
+ if accept :
223
+ await navigated_future
224
+ else :
225
+ with pytest .raises (error .UnknownErrorException ):
226
+ await wait_for_future_safe (navigated_future )
0 commit comments