Skip to content

Commit

Permalink
Tolerate multiple requestWillBeSent events for a single request
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Dec 3, 2024
1 parent fd26a09 commit 10c5468
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bidiMapper/modules/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ export class BrowsingContextImpl {
return;
}

this.#navigationTracker.requestWillBeSent();
this.#navigationTracker.requestWillBeSent(params.requestId);
});

this.#cdpTarget.cdpClient.on('Page.lifecycleEvent', (params) => {
Expand Down
8 changes: 7 additions & 1 deletion src/bidiMapper/modules/context/NavigationTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type NavigationEventName =
class NavigationState {
started = new Deferred<void>();
finished = new Deferred<NavigationEventName>();
cdpNetworkRequestId?: string;

readonly navigationId = uuidv4();
url: string;
Expand Down Expand Up @@ -156,7 +157,11 @@ export class NavigationTracker {
}
}

requestWillBeSent() {
requestWillBeSent(cdpNetworkRequestId: string) {
if (this.#currentNavigation.cdpNetworkRequestId === cdpNetworkRequestId) {
// The same request can be due to redirect. Ignore if so.
return;
}
this.#currentNavigation.finished.resolve(
ChromiumBidi.BrowsingContext.EventNames.NavigationAborted,
);
Expand All @@ -171,6 +176,7 @@ export class NavigationTracker {

this.#ongoingNavigation.started.resolve();
this.#currentNavigation = this.#ongoingNavigation;
this.#currentNavigation.cdpNetworkRequestId = cdpNetworkRequestId;
this.#ongoingNavigation = undefined;
}

Expand Down
11 changes: 9 additions & 2 deletions tests/browsing_context/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async def test_nested_navigate_scriptFragmentRedirect_checkEvents(
}
})

messages = await read_sorted_messages(4)
messages = await read_sorted_messages(5)
await assert_no_more_messages()
assert messages == [
AnyExtending({
Expand All @@ -190,6 +190,13 @@ async def test_nested_navigate_scriptFragmentRedirect_checkEvents(
},
'type': 'event',
}),
AnyExtending({
'method': 'browsingContext.fragmentNavigated',
'params': {
'context': iframe_id,
'url': final_url,
}
}),
AnyExtending({
'method': 'browsingContext.load',
'params': {
Expand All @@ -205,7 +212,7 @@ async def test_nested_navigate_scriptFragmentRedirect_checkEvents(
'url': initial_url,
},
'type': 'event',
}),
})
]


Expand Down
1 change: 1 addition & 0 deletions tests/network/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ async def test_network_before_request_sent_event_with_data_url_emitted(
@pytest.mark.asyncio
async def test_network_specific_context_subscription_does_not_enable_cdp_network_globally(
websocket, context_id, create_context, url_base):
pytest.xfail("Network domain is enabled for navigation detection")
await subscribe(websocket, ["network.beforeRequestSent"], [context_id])

new_context_id = await create_context()
Expand Down

0 comments on commit 10c5468

Please sign in to comment.