Skip to content

Commit 262012f

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 952bb9e commit 262012f

File tree

5 files changed

+104
-57
lines changed

5 files changed

+104
-57
lines changed

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

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import asyncio
22

33
import pytest
4+
import webdriver.bidi.error as error
45
from webdriver.bidi.modules.script import ContextTarget
56

67
from . import navigate_and_assert
78
from ... import any_string
89

910
pytestmark = pytest.mark.asyncio
1011

12+
USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"
13+
1114

1215
async 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

9293
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,
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)

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ async def test_expected_url(bidi_session, inline, new_tab, value):
2828
context=new_tab["context"], url=url, wait=value
2929
)
3030
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
31+
contexts = await bidi_session.browsing_context.get_tree(
32+
root=new_tab["context"], max_depth=0)
33+
assert contexts[0]["url"] == url
3634

3735

3836
@pytest.mark.parametrize(
@@ -49,10 +47,10 @@ async def test_slow_image_blocks_load(bidi_session, inline, new_tab, wait, expec
4947

5048
await wait_for_navigation(bidi_session, new_tab["context"], url, wait, expect_timeout)
5149

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":
50+
# We cannot assert the URL for "complete", since we expect a timeout. For
51+
# this case, the wait_for_navigation helper will resume after 1 second,
52+
# there is no guarantee that the URL has been updated.
53+
if wait != "complete":
5654
contexts = await bidi_session.browsing_context.get_tree(
5755
root=new_tab["context"], max_depth=0
5856
)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,15 @@ 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.
445+
asyncio.create_task(bidi_session.browsing_context.navigate(
444446
context=new_tab["context"], url=target_url, wait="none"
445-
)
447+
))
446448

447449
event = await wait_for_future_safe(on_navigation_started)
448450

449451
assert event["context"] == new_tab["context"]
450-
assert event["navigation"] == result["navigation"]
451452
assert event["url"] == target_url
452453

453454

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: 4 additions & 9 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(
@@ -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,14 +330,12 @@ 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

0 commit comments

Comments
 (0)