Skip to content

Commit

Permalink
[wdspec] align navigation tests with spec
Browse files Browse the repository at this point in the history
Align WebDriver BiDi spec tests with the breaking change in navigation wait logic: w3c/webdriver-bidi#855.
  • Loading branch information
sadym-chromium committed Feb 6, 2025
1 parent 93527fd commit 1bf7cc8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 57 deletions.
106 changes: 80 additions & 26 deletions webdriver/tests/bidi/browsing_context/navigate/navigate.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import asyncio

import pytest
import webdriver.bidi.error as error
from webdriver.bidi.modules.script import ContextTarget

from . import navigate_and_assert
from ... import any_string

pytestmark = pytest.mark.asyncio

USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"


async def test_payload(bidi_session, inline, new_tab):
url = inline("<div>foo</div>")
Expand Down Expand Up @@ -81,12 +84,10 @@ async def test_relative_url(bidi_session, new_tab, url):
"/webdriver/tests/bidi/browsing_context/support/empty.html"
)

# Navigate to page1 with wait=interactive to make sure the document's base URI
# was updated.
await navigate_and_assert(bidi_session, new_tab, url_before, "interactive")
await navigate_and_assert(bidi_session, new_tab, url_before, "none")

url_after = url_before.replace("empty.html", "other.html")
await navigate_and_assert(bidi_session, new_tab, url_after, "interactive")
await navigate_and_assert(bidi_session, new_tab, url_after, "none")


async def test_same_document_navigation_in_before_unload(bidi_session, new_tab, url):
Expand All @@ -110,25 +111,44 @@ async def test_same_document_navigation_in_before_unload(bidi_session, new_tab,


@pytest.mark.capabilities({"unhandledPromptBehavior": {'beforeUnload': 'ignore'}})
async def test_wait_none_with_beforeunload_prompt(
bidi_session, new_tab, setup_beforeunload_page, inline
):
@pytest.mark.parametrize("value", ["none", "interactive", "complete"])
@pytest.mark.parametrize("accept", [True, False])
async def test_navigate_with_beforeunload_prompt(bidi_session, new_tab,
setup_beforeunload_page, inline, subscribe_events, wait_for_event,
wait_for_future_safe, value, accept):
await setup_beforeunload_page(new_tab)

await subscribe_events(events=[USER_PROMPT_OPENED_EVENT])
on_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT)

url_after = inline("<div>foo</div>")

result = await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=url_after, wait="none"
navigated_future = asyncio.create_task(
bidi_session.browsing_context.navigate(context=new_tab["context"],
url=url_after, wait=value))

# Wait for the prompt to open.
await wait_for_future_safe(on_prompt_opened)
# Make sure the navigation is not finished.
assert not navigated_future.done()

await bidi_session.browsing_context.handle_user_prompt(
context=new_tab["context"], accept=accept
)

assert result["url"] == url_after
any_string(result["navigation"])
if accept:
await navigated_future
else:
with pytest.raises(error.UnknownErrorException):
await wait_for_future_safe(navigated_future)


@pytest.mark.capabilities({"unhandledPromptBehavior": {'beforeUnload': 'ignore'}})
async def test_wait_none_with_beforeunload_prompt_in_iframe(
bidi_session, new_tab, setup_beforeunload_page, inline
):
@pytest.mark.parametrize("value", ["none", "interactive", "complete"])
@pytest.mark.parametrize("accept", [True, False])
async def test_navigate_with_beforeunload_prompt_in_iframe(bidi_session,
new_tab, setup_beforeunload_page, inline, subscribe_events,
wait_for_event, wait_for_future_safe, value, accept):
page = inline(f"""<iframe src={inline("foo")}></iframe>""")
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=page, wait="complete"
Expand All @@ -139,35 +159,69 @@ async def test_wait_none_with_beforeunload_prompt_in_iframe(

await setup_beforeunload_page(iframe_context)

await subscribe_events(events=[USER_PROMPT_OPENED_EVENT])
on_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT)

url_after = inline("<div>foo</div>")

result = await bidi_session.browsing_context.navigate(
context=iframe_context["context"], url=url_after, wait="none"
navigated_future = asyncio.create_task(
bidi_session.browsing_context.navigate(
context=iframe_context["context"], url=url_after, wait=value))

# Wait for the prompt to open.
await wait_for_future_safe(on_prompt_opened)
# Make sure the navigation is not finished.
assert not navigated_future.done()

await bidi_session.browsing_context.handle_user_prompt(
context=new_tab["context"], accept=accept
)

assert result["url"] == url_after
any_string(result["navigation"])
if accept:
await navigated_future
else:
with pytest.raises(error.UnknownErrorException):
await wait_for_future_safe(navigated_future)


@pytest.mark.capabilities({"unhandledPromptBehavior": {'beforeUnload': 'ignore'}})
async def test_wait_none_with_beforeunload_prompt_in_iframe_navigate_in_top_context(
bidi_session, new_tab, setup_beforeunload_page, inline
):
@pytest.mark.parametrize("value", ["none", "interactive", "complete"])
@pytest.mark.parametrize("accept", [True, False])
async def test_navigate_with_beforeunload_prompt_in_iframe_navigate_in_top_context(
bidi_session, new_tab, setup_beforeunload_page, inline,
subscribe_events, wait_for_event, wait_for_future_safe, value, accept):
page = inline(f"""<iframe src={inline("foo")}></iframe>""")
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=page, wait="complete"
)

contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])
contexts = await bidi_session.browsing_context.get_tree(
root=new_tab["context"])
iframe_context = contexts[0]["children"][0]

await setup_beforeunload_page(iframe_context)

await subscribe_events(events=[USER_PROMPT_OPENED_EVENT])
on_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT)

url_after = inline("<div>foo</div>")

result = await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=url_after, wait="none"
navigated_future = asyncio.create_task(
bidi_session.browsing_context.navigate(
context=new_tab["context"], url=url_after, wait=value
))

# Wait for the prompt to open.
await wait_for_future_safe(on_prompt_opened)
# Make sure the navigation is not finished.
assert not navigated_future.done()

await bidi_session.browsing_context.handle_user_prompt(
context=new_tab["context"], accept=accept
)

assert result["url"] == url_after
any_string(result["navigation"])
if accept:
await navigated_future
else:
with pytest.raises(error.UnknownErrorException):
await wait_for_future_safe(navigated_future)
16 changes: 7 additions & 9 deletions webdriver/tests/bidi/browsing_context/navigate/wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ async def test_expected_url(bidi_session, inline, new_tab, value):
context=new_tab["context"], url=url, wait=value
)
assert result["url"] == url
if value != "none":
contexts = await bidi_session.browsing_context.get_tree(
root=new_tab["context"], max_depth=0
)
assert contexts[0]["url"] == url
contexts = await bidi_session.browsing_context.get_tree(
root=new_tab["context"], max_depth=0)
assert contexts[0]["url"] == url


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

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

# We cannot assert the URL for "none" by definition, and for "complete", since
# we expect a timeout. For the timeout case, the wait_for_navigation helper will
# resume after 1 second, there is no guarantee that the URL has been updated.
if wait == "interactive":
# We cannot assert the URL for "complete", since we expect a timeout. For
# this case, the wait_for_navigation helper will resume after 1 second,
# there is no guarantee that the URL has been updated.
if wait != "complete":
contexts = await bidi_session.browsing_context.get_tree(
root=new_tab["context"], max_depth=0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,15 @@ async def test_with_beforeunload_prompt(
target_url = url("/webdriver/tests/support/html/default.html", domain="alt")

on_navigation_started = wait_for_event(NAVIGATION_STARTED_EVENT)
result = await bidi_session.browsing_context.navigate(

# Trigger navigation, but don't wait for it to be finished.
asyncio.create_task(bidi_session.browsing_context.navigate(
context=new_tab["context"], url=target_url, wait="none"
)
))

event = await wait_for_future_safe(on_navigation_started)

assert event["context"] == new_tab["context"]
assert event["navigation"] == result["navigation"]
assert event["url"] == target_url


Expand Down
19 changes: 9 additions & 10 deletions webdriver/tests/bidi/browsing_context/reload/wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ async def test_expected_url(bidi_session, inline, new_tab, wait):
wait=wait
)

if wait != "none":
assert reload_result["navigation"] != navigate_result["navigation"]
assert reload_result["url"] == url
assert reload_result["navigation"] != navigate_result["navigation"]
assert reload_result["url"] == url

contexts = await bidi_session.browsing_context.get_tree(
root=new_tab["context"], max_depth=0)
assert contexts[0]["url"] == url
contexts = await bidi_session.browsing_context.get_tree(
root=new_tab["context"], max_depth=0)
assert contexts[0]["url"] == url


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

# We cannot assert the URL for "none" by definition, and for "complete", since
# we expect a timeout. For the timeout case, the wait_for_navigation helper will
# resume after 1 second, there is no guarantee that the URL has been updated.
if wait == "interactive":
# We cannot assert the URL for "complete", since we expect a timeout. For
# this case, the wait_for_navigation helper will resume after 1 second,
# there is no guarantee that the URL has been updated.
if wait != "complete":
contexts = await bidi_session.browsing_context.get_tree(
root=new_tab["context"], max_depth=0)
assert contexts[0]["url"] == url
Expand Down
13 changes: 4 additions & 9 deletions webdriver/tests/bidi/network/fetch_error/fetch_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,16 @@ async def test_navigation_id(
assert fetch_error_event["navigation"] is None

on_fetch_error = wait_for_event(FETCH_ERROR_EVENT)
result = await bidi_session.browsing_context.navigate(
asyncio.ensure_future(bidi_session.browsing_context.navigate(
context=new_tab["context"],
url=PAGE_INVALID_URL,
)
url=PAGE_INVALID_URL))
fetch_error_event = await wait_for_future_safe(on_fetch_error)

expected_request = {"method": "GET", "url": PAGE_INVALID_URL}
assert_fetch_error_event(
fetch_error_event,
expected_request=expected_request,
navigation=result["navigation"],
)
assert fetch_error_event["navigation"] == result["navigation"]


@pytest.mark.parametrize(
Expand Down Expand Up @@ -320,10 +317,10 @@ async def test_redirect_navigation(
on_fetch_error = wait_for_event(FETCH_ERROR_EVENT)
on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)

result = await bidi_session.browsing_context.navigate(
asyncio.ensure_future(bidi_session.browsing_context.navigate(
context=new_tab["context"],
url=redirect_url,
)
))

wait = AsyncPoll(bidi_session, timeout=2)
fetch_error_event = await on_fetch_error
Expand All @@ -333,14 +330,12 @@ async def test_redirect_navigation(
assert_response_event(
response_completed_event,
expected_request=expected_request,
navigation=result["navigation"],
redirect_count=0,
)
expected_request = {"method": "GET", "url": PAGE_INVALID_URL}
assert_fetch_error_event(
fetch_error_event,
expected_request=expected_request,
navigation=result["navigation"],
redirect_count=1,
)

Expand Down

0 comments on commit 1bf7cc8

Please sign in to comment.