Skip to content

Commit 9326693

Browse files
[wdspec] align navigation tests with spec
Align WebDriver BiDi spec tests with the breaking change in navigation wait logic: w3c/webdriver-bidi#855.
1 parent 93527fd commit 9326693

File tree

5 files changed

+112
-60
lines changed

5 files changed

+112
-60
lines changed

webdriver/tests/bidi/browsing_context/navigate/navigate.py

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
pytestmark = pytest.mark.asyncio
1010

11+
USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"
12+
1113

1214
async def test_payload(bidi_session, inline, new_tab):
1315
url = inline("<div>foo</div>")
@@ -81,12 +83,10 @@ async def test_relative_url(bidi_session, new_tab, url):
8183
"/webdriver/tests/bidi/browsing_context/support/empty.html"
8284
)
8385

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")
8787

8888
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")
9090

9191

9292
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,
110110

111111

112112
@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):
116118
await setup_beforeunload_page(new_tab)
117119

120+
await subscribe_events(events=[USER_PROMPT_OPENED_EVENT])
121+
on_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT)
122+
118123
url_after = inline("<div>foo</div>")
119124

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
122136
)
123137

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)
126143

127144

128145
@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):
132151
page = inline(f"""<iframe src={inline("foo")}></iframe>""")
133152
await bidi_session.browsing_context.navigate(
134153
context=new_tab["context"], url=page, wait="complete"
@@ -139,35 +158,69 @@ async def test_wait_none_with_beforeunload_prompt_in_iframe(
139158

140159
await setup_beforeunload_page(iframe_context)
141160

161+
await subscribe_events(events=[USER_PROMPT_OPENED_EVENT])
162+
on_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT)
163+
142164
url_after = inline("<div>foo</div>")
143165

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
146177
)
147178

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)
150184

151185

152186
@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):
156192
page = inline(f"""<iframe src={inline("foo")}></iframe>""")
157193
await bidi_session.browsing_context.navigate(
158194
context=new_tab["context"], url=page, wait="complete"
159195
)
160196

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"])
162199
iframe_context = contexts[0]["children"][0]
163200

164201
await setup_beforeunload_page(iframe_context)
165202

203+
await subscribe_events(events=[USER_PROMPT_OPENED_EVENT])
204+
on_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT)
205+
166206
url_after = inline("<div>foo</div>")
167207

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
170220
)
171221

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)

webdriver/tests/bidi/browsing_context/navigate/wait.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import pytest
22
import asyncio
33

4+
import webdriver.bidi.error as error
5+
6+
47
pytestmark = pytest.mark.asyncio
58

9+
USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"
10+
611

712
async def wait_for_navigation(bidi_session, context, url, wait, expect_timeout):
813
# Ultimately, "interactive" and "complete" should support a timeout argument.
@@ -28,11 +33,9 @@ async def test_expected_url(bidi_session, inline, new_tab, value):
2833
context=new_tab["context"], url=url, wait=value
2934
)
3035
assert result["url"] == url
31-
if value != "none":
32-
contexts = await bidi_session.browsing_context.get_tree(
33-
root=new_tab["context"], max_depth=0
34-
)
35-
assert contexts[0]["url"] == url
36+
contexts = await bidi_session.browsing_context.get_tree(
37+
root=new_tab["context"], max_depth=0)
38+
assert contexts[0]["url"] == url
3639

3740

3841
@pytest.mark.parametrize(
@@ -49,10 +52,10 @@ async def test_slow_image_blocks_load(bidi_session, inline, new_tab, wait, expec
4952

5053
await wait_for_navigation(bidi_session, new_tab["context"], url, wait, expect_timeout)
5154

52-
# We cannot assert the URL for "none" by definition, and for "complete", since
53-
# we expect a timeout. For the timeout case, the wait_for_navigation helper will
54-
# resume after 1 second, there is no guarantee that the URL has been updated.
55-
if wait == "interactive":
55+
# We cannot assert the URL for "complete", since we expect a timeout. For
56+
# this case, the wait_for_navigation helper will resume after 1 second,
57+
# there is no guarantee that the URL has been updated.
58+
if wait != "complete":
5659
contexts = await bidi_session.browsing_context.get_tree(
5760
root=new_tab["context"], max_depth=0
5861
)

webdriver/tests/bidi/browsing_context/navigation_started/navigation_started.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,16 @@ async def test_with_beforeunload_prompt(
440440
target_url = url("/webdriver/tests/support/html/default.html", domain="alt")
441441

442442
on_navigation_started = wait_for_event(NAVIGATION_STARTED_EVENT)
443-
result = await bidi_session.browsing_context.navigate(
443+
444+
# Trigger navigation, but don't wait for it to be finished. Needed to be run
445+
# in a task.
446+
asyncio.create_task(bidi_session.browsing_context.navigate(
444447
context=new_tab["context"], url=target_url, wait="none"
445-
)
448+
))
446449

447450
event = await wait_for_future_safe(on_navigation_started)
448451

449452
assert event["context"] == new_tab["context"]
450-
assert event["navigation"] == result["navigation"]
451453
assert event["url"] == target_url
452454

453455

webdriver/tests/bidi/browsing_context/reload/wait.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ async def test_expected_url(bidi_session, inline, new_tab, wait):
3838
wait=wait
3939
)
4040

41-
if wait != "none":
42-
assert reload_result["navigation"] != navigate_result["navigation"]
43-
assert reload_result["url"] == url
41+
assert reload_result["navigation"] != navigate_result["navigation"]
42+
assert reload_result["url"] == url
4443

45-
contexts = await bidi_session.browsing_context.get_tree(
46-
root=new_tab["context"], max_depth=0)
47-
assert contexts[0]["url"] == url
44+
contexts = await bidi_session.browsing_context.get_tree(
45+
root=new_tab["context"], max_depth=0)
46+
assert contexts[0]["url"] == url
4847

4948

5049
@pytest.mark.parametrize(
@@ -68,10 +67,10 @@ async def test_slow_image_blocks_load(bidi_session, inline, new_tab, wait,
6867
await wait_for_reload(bidi_session, new_tab["context"], wait,
6968
expect_timeout)
7069

71-
# We cannot assert the URL for "none" by definition, and for "complete", since
72-
# we expect a timeout. For the timeout case, the wait_for_navigation helper will
73-
# resume after 1 second, there is no guarantee that the URL has been updated.
74-
if wait == "interactive":
70+
# We cannot assert the URL for "complete", since we expect a timeout. For
71+
# this case, the wait_for_navigation helper will resume after 1 second,
72+
# there is no guarantee that the URL has been updated.
73+
if wait != "complete":
7574
contexts = await bidi_session.browsing_context.get_tree(
7675
root=new_tab["context"], max_depth=0)
7776
assert contexts[0]["url"] == url

webdriver/tests/bidi/network/fetch_error/fetch_error.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,16 @@ async def test_navigation_id(
152152
assert fetch_error_event["navigation"] is None
153153

154154
on_fetch_error = wait_for_event(FETCH_ERROR_EVENT)
155-
result = await bidi_session.browsing_context.navigate(
155+
asyncio.ensure_future(bidi_session.browsing_context.navigate(
156156
context=new_tab["context"],
157-
url=PAGE_INVALID_URL,
158-
)
157+
url=PAGE_INVALID_URL))
159158
fetch_error_event = await wait_for_future_safe(on_fetch_error)
160159

161160
expected_request = {"method": "GET", "url": PAGE_INVALID_URL}
162161
assert_fetch_error_event(
163162
fetch_error_event,
164163
expected_request=expected_request,
165-
navigation=result["navigation"],
166164
)
167-
assert fetch_error_event["navigation"] == result["navigation"]
168165

169166

170167
@pytest.mark.parametrize(
@@ -303,7 +300,7 @@ async def test_redirect_fetch(
303300

304301
@pytest.mark.asyncio
305302
async def test_redirect_navigation(
306-
bidi_session, new_tab, wait_for_event, url, setup_network_test
303+
bidi_session, new_tab, wait_for_event, url, setup_network_test
307304
):
308305
redirect_url = url(
309306
f"/webdriver/tests/support/http_handlers/redirect.py?location={PAGE_INVALID_URL}"
@@ -320,10 +317,10 @@ async def test_redirect_navigation(
320317
on_fetch_error = wait_for_event(FETCH_ERROR_EVENT)
321318
on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
322319

323-
result = await bidi_session.browsing_context.navigate(
320+
asyncio.ensure_future(bidi_session.browsing_context.navigate(
324321
context=new_tab["context"],
325322
url=redirect_url,
326-
)
323+
))
327324

328325
wait = AsyncPoll(bidi_session, timeout=2)
329326
fetch_error_event = await on_fetch_error
@@ -333,19 +330,17 @@ async def test_redirect_navigation(
333330
assert_response_event(
334331
response_completed_event,
335332
expected_request=expected_request,
336-
navigation=result["navigation"],
337333
redirect_count=0,
338334
)
339335
expected_request = {"method": "GET", "url": PAGE_INVALID_URL}
340336
assert_fetch_error_event(
341337
fetch_error_event,
342338
expected_request=expected_request,
343-
navigation=result["navigation"],
344339
redirect_count=1,
345340
)
346341

347342
# Check that all events share the same requestId
348343
assert (
349-
fetch_error_event["request"]["request"]
350-
== response_completed_event["request"]["request"]
344+
fetch_error_event["request"]["request"]
345+
== response_completed_event["request"]["request"]
351346
)

0 commit comments

Comments
 (0)