Skip to content

Commit

Permalink
test: extend navigation tests (#3110)
Browse files Browse the repository at this point in the history
Extend test coverage for navigation. Extracted from
#3104.
  • Loading branch information
sadym-chromium authored Feb 11, 2025
1 parent 2422f2a commit 63025b3
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 12 deletions.
86 changes: 85 additions & 1 deletion tests/browsing_context/__snapshots__/test_navigate_events.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,67 @@
}),
])
# ---
# name: test_navigate_beforeunload_cancel[capabilities0]
list([
dict({
'method': 'script.message',
'params': dict({
'channel': 'beforeunload_channel',
'data': dict({
'type': 'string',
'value': 'beforeunload',
}),
'source': dict({
'context': 'stable_0',
}),
}),
'type': 'event',
}),
dict({
'method': 'browsingContext.navigationStarted',
'params': dict({
'context': 'stable_0',
'navigation': 'stable_1',
'url': 'stable_2',
}),
'type': 'event',
}),
dict({
'method': 'browsingContext.userPromptOpened',
'params': dict({
'context': 'stable_0',
'handler': 'dismiss',
'message': '',
'type': 'beforeunload',
}),
'type': 'event',
}),
dict({
'method': 'browsingContext.navigationFailed',
'params': dict({
'context': 'stable_0',
'navigation': 'stable_1',
'url': 'stable_2',
}),
'type': 'event',
}),
dict({
'error': 'unknown error',
'id': 'stable_3',
'message': 'net::ERR_ABORTED',
'type': 'error',
}),
dict({
'method': 'browsingContext.userPromptClosed',
'params': dict({
'accepted': False,
'context': 'stable_0',
'type': 'beforeunload',
}),
'type': 'event',
}),
])
# ---
# name: test_navigate_checkEvents[complete]
list([
dict({
Expand Down Expand Up @@ -688,6 +749,29 @@
# ---
# name: test_navigate_hang_navigate_again_checkEvents
list([
dict({
'method': 'script.message',
'params': dict({
'channel': 'beforeunload_channel',
'data': dict({
'type': 'string',
'value': 'beforeunload',
}),
'source': dict({
'context': 'stable_0',
}),
}),
'type': 'event',
}),
dict({
'method': 'browsingContext.navigationStarted',
'params': dict({
'context': 'stable_0',
'navigation': 'stable_1',
'url': 'stable_2',
}),
'type': 'event',
}),
dict({
'method': 'network.beforeRequestSent',
'params': dict({
Expand All @@ -704,7 +788,7 @@
'params': dict({
'context': 'stable_0',
'navigation': 'stable_1',
'url': 'stable_3',
'url': 'stable_2',
}),
'type': 'event',
}),
Expand Down
76 changes: 66 additions & 10 deletions tests/browsing_context/test_navigate_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import pytest
from syrupy.filters import props
from test_helpers import (execute_command, goto_url, send_JSON_command,
subscribe, wait_for_event)
subscribe)

SNAPSHOT_EXCLUDE = props("timestamp", "timings", "headers", "stacktrace",
"response", "initiator", "realm")
Expand All @@ -25,29 +25,38 @@
]


async def set_beforeunload_handler(websocket, context_id):
async def set_beforeunload_handler(websocket, context_id, show_popup=False):
await execute_command(
websocket, {
"method": "script.callFunction",
"params": {
"functionDeclaration": """
(channel) => {
(channel, show_popup) => {
window.addEventListener('beforeunload', () => {
channel("beforeunload");
},false)
if(show_popup) {
event.returnValue = "Are you sure you want to leave?";
event.preventDefault();
}
},false);
document.body.click();
}
""",
"arguments": [{
"type": "channel",
"value": {
"channel": "beforeunload_channel",
"ownership": "none",
},
}
}, {
"type": "boolean",
"value": show_popup
}],
"target": {
"context": context_id,
},
"awaitPromise": False
"awaitPromise": False,
"userActivation": True
}
})

Expand Down Expand Up @@ -220,8 +229,7 @@ async def test_navigate_dataUrl_checkEvents(websocket, context_id, url_base,
@pytest.mark.asyncio
async def test_navigate_hang_navigate_again_checkEvents(
websocket, context_id, url_base, url_hang_forever,
url_example_another_origin, read_messages, snapshot,
assert_no_more_messages):
url_example_another_origin, read_messages, snapshot):
# Use `url_example_another_origin`, as `url_example` will hang because of
# `url_hang_forever`.
await goto_url(websocket, context_id, url_base)
Expand All @@ -230,6 +238,9 @@ async def test_navigate_hang_navigate_again_checkEvents(
websocket,
["browsingContext", "script.message", "network.beforeRequestSent"])

messages_log = []
known_values = {}

await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
Expand All @@ -240,7 +251,10 @@ async def test_navigate_hang_navigate_again_checkEvents(
}
})

await wait_for_event(websocket, "browsingContext.navigationStarted")
messages_log += await read_messages(3,
keys_to_stabilize=KEYS_TO_STABILIZE,
known_values=known_values,
sort=False)

await send_JSON_command(
websocket, {
Expand All @@ -252,10 +266,52 @@ async def test_navigate_hang_navigate_again_checkEvents(
}
})

messages = await read_messages(10,
messages_log += await read_messages(9,
keys_to_stabilize=KEYS_TO_STABILIZE,
known_values=known_values,
check_no_other_messages=True,
sort=False)

assert messages_log == snapshot(exclude=SNAPSHOT_EXCLUDE)


@pytest.mark.asyncio
@pytest.mark.parametrize('capabilities', [{
'unhandledPromptBehavior': {
'beforeUnload': 'dismiss'
}
}],
indirect=True)
async def test_navigate_beforeunload_cancel(websocket, context_id, url_base,
url_hang_forever,
url_example_another_origin,
read_messages, snapshot):
# Use `url_example_another_origin`, as `url_example` will hang because of
# `url_hang_forever`.
await goto_url(websocket, context_id, url_base)
await set_beforeunload_handler(websocket, context_id, True)
await subscribe(
websocket,
["browsingContext", "script.message", "network.beforeRequestSent"])

known_values = {}

await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": url_hang_forever,
"wait": "complete",
"context": context_id
}
})

messages = await read_messages(6,
keys_to_stabilize=KEYS_TO_STABILIZE,
known_values=known_values,
check_no_other_messages=True,
sort=False)

assert messages == snapshot(exclude=SNAPSHOT_EXCLUDE)


Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ async def read_messages(message_count,
bool] = lambda _: True,
keys_to_stabilize: list[str] = [],
check_no_other_messages: bool = False,
known_values: dict[str, str] | None = None,
sort=True):
messages = []
for _ in range(message_count):
Expand All @@ -352,7 +353,7 @@ async def read_messages(message_count,
messages.sort(key=lambda x: x["method"] if "method" in x else str(
x["id"]) if "id" in x else "")
# Stabilize some values through the messages.
stabilize_key_values(messages, keys_to_stabilize)
stabilize_key_values(messages, keys_to_stabilize, known_values)

if len(messages) > message_count:
# "Assert equals" to produce a readable overview of all received
Expand Down

0 comments on commit 63025b3

Please sign in to comment.