1
1
import asyncio
2
2
3
3
import pytest
4
+ import webdriver .bidi .error as error
4
5
from webdriver .bidi .modules .script import ContextTarget
5
6
6
7
from . import navigate_and_assert
7
8
from ... import any_string
8
9
9
10
pytestmark = pytest .mark .asyncio
10
11
12
+ USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"
13
+
11
14
12
15
async def test_payload (bidi_session , inline , new_tab ):
13
16
url = inline ("<div>foo</div>" )
@@ -81,12 +84,10 @@ async def test_relative_url(bidi_session, new_tab, url):
81
84
"/webdriver/tests/bidi/browsing_context/support/empty.html"
82
85
)
83
86
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" )
87
88
88
89
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 " )
90
91
91
92
92
93
async 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,
110
111
111
112
112
113
@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 ):
116
119
await setup_beforeunload_page (new_tab )
117
120
121
+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
122
+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
123
+
118
124
url_after = inline ("<div>foo</div>" )
119
125
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
122
137
)
123
138
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 )
126
144
127
145
128
146
@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 ):
132
152
page = inline (f"""<iframe src={ inline ("foo" )} ></iframe>""" )
133
153
await bidi_session .browsing_context .navigate (
134
154
context = new_tab ["context" ], url = page , wait = "complete"
@@ -139,35 +159,69 @@ async def test_wait_none_with_beforeunload_prompt_in_iframe(
139
159
140
160
await setup_beforeunload_page (iframe_context )
141
161
162
+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
163
+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
164
+
142
165
url_after = inline ("<div>foo</div>" )
143
166
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
146
178
)
147
179
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 )
150
185
151
186
152
187
@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 ):
156
193
page = inline (f"""<iframe src={ inline ("foo" )} ></iframe>""" )
157
194
await bidi_session .browsing_context .navigate (
158
195
context = new_tab ["context" ], url = page , wait = "complete"
159
196
)
160
197
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" ])
162
200
iframe_context = contexts [0 ]["children" ][0 ]
163
201
164
202
await setup_beforeunload_page (iframe_context )
165
203
204
+ await subscribe_events (events = [USER_PROMPT_OPENED_EVENT ])
205
+ on_prompt_opened = wait_for_event (USER_PROMPT_OPENED_EVENT )
206
+
166
207
url_after = inline ("<div>foo</div>" )
167
208
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
170
221
)
171
222
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