Skip to content

Commit

Permalink
feat: align abort navigation with the spec (#2715)
Browse files Browse the repository at this point in the history
Related issue:
#2709

Should be reverted if w3c/webdriver-bidi#799
is fixed
  • Loading branch information
sadym-chromium authored Oct 22, 2024
1 parent 48d496a commit 6edf07b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/bidiMapper/modules/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,27 @@ export class BrowsingContextImpl {
if (this.id !== params.frameId) {
return;
}
// If there is a pending navigation, reject it.
this.#pendingCommandNavigation?.reject(
new UnknownErrorException(
`navigation canceled, as new navigation is requested by ${params.reason}`,
),
);

if (this.#pendingCommandNavigation !== undefined) {
// The pending navigation was aborted by the new one.
this.#eventManager.registerEvent(
{
type: 'event',
method: ChromiumBidi.BrowsingContext.EventNames.NavigationAborted,
params: {
context: this.id,
navigation: this.#navigationId,
timestamp: BrowsingContextImpl.getTimestamp(),
url: this.#url,
},
},
this.id,
);
this.#pendingCommandNavigation.reject(
ChromiumBidi.BrowsingContext.EventNames.NavigationAborted,
);
this.#pendingCommandNavigation = undefined;
}
this.#pendingNavigationUrl = params.url;
});

Expand Down Expand Up @@ -907,7 +922,7 @@ export class BrowsingContextImpl {

if (wait === BrowsingContext.ReadinessState.None) {
// Do not wait for the result of the navigation promise.
this.#pendingCommandNavigation.resolve();
this.#pendingCommandNavigation?.resolve();
this.#pendingCommandNavigation = undefined;

return {
Expand All @@ -924,13 +939,20 @@ export class BrowsingContextImpl {
this.#waitNavigation(wait, cdpNavigateResult.loaderId === undefined),
// Throw an error if the navigation is canceled.
this.#pendingCommandNavigation,
]);
]).catch((e) => {
// Aborting navigation should not fail the original navigation command for now.
// https://github.com/w3c/webdriver-bidi/issues/799#issue-2605618955
if (e !== ChromiumBidi.BrowsingContext.EventNames.NavigationAborted) {
throw e;
}
});

this.#pendingCommandNavigation.resolve();
// `#pendingCommandNavigation` can be already rejected and set to undefined.
this.#pendingCommandNavigation?.resolve();
this.#pendingCommandNavigation = undefined;
return {
navigation: navigationId,
// Url can change due to redirect get the latest one.
// Url can change due to redirect. Get the latest one.
url: this.#url,
};
}
Expand Down
37 changes: 37 additions & 0 deletions tests/browsing_context/test_navigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@
read_JSON_message, send_JSON_command, subscribe)


@pytest.mark.asyncio
async def test_browsingContext_navigateWaitInteractive_redirect(
websocket, context_id, html, url_example, read_sorted_messages):
await subscribe(websocket, ["browsingContext.navigationAborted"])

initial_url = html(f"<script>window.location='{url_example}';</script>")

command_id = await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"url": initial_url,
"wait": "interactive",
"context": context_id
}
})

[navigation_result,
navigation_aborted_event] = await read_sorted_messages(2)
assert navigation_result == AnyExtending({
'id': command_id,
'type': 'success'
})
initial_navigation_id = navigation_result['result']['navigation']

assert navigation_aborted_event == AnyExtending({
'method': 'browsingContext.navigationAborted',
'type': 'event',
'params': {
'context': context_id,
'navigation': initial_navigation_id,
'timestamp': ANY_TIMESTAMP,
'url': initial_url
}
})


@pytest.mark.asyncio
async def test_browsingContext_navigateWaitNone_navigated(
websocket, context_id, html):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[error.py]
[test_with_new_navigation_inside_page]
expected: FAIL
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[error.py]
[test_with_new_navigation_inside_page]
expected: FAIL
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[error.py]
[test_with_new_navigation_inside_page]
expected: FAIL

0 comments on commit 6edf07b

Please sign in to comment.