Skip to content

Commit

Permalink
fix: always provide url in browsingContext.navigationStarted (#2483)
Browse files Browse the repository at this point in the history
Rely on `Page.frameRequestedNavigation` and
`Page.frameScheduledNavigation`.
  • Loading branch information
sadym-chromium authored Aug 29, 2024
1 parent 0fa46f6 commit 318d621
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/bidiMapper/modules/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ export class BrowsingContextImpl {
readonly #logger?: LoggerFn;
// Keeps track of the previously set viewport.
#previousViewport: {width: number; height: number} = {width: 0, height: 0};

// The URL of the navigation that is currently in progress. A workaround of the CDP
// lacking URL for the pending navigation events, e.g. `Page.frameStartedLoading`.
// Set on `Page.navigate`, `Page.reload` commands and on deprecated CDP event
// `Page.frameScheduledNavigation`.
// Set on `Page.navigate`, `Page.reload` commands, on `Page.frameRequestedNavigation` or
// on a deprecated `Page.frameScheduledNavigation` event. The latest is required as the
// `Page.frameRequestedNavigation` event is not emitted for same-document navigations.
#pendingNavigationUrl: string | undefined;
#virtualNavigationId: string = uuidv4();

Expand Down Expand Up @@ -456,6 +458,13 @@ export class BrowsingContextImpl {
this.#pendingNavigationUrl = params.url;
});

this.#cdpTarget.cdpClient.on('Page.frameRequestedNavigation', (params) => {
if (this.id !== params.frameId) {
return;
}
this.#pendingNavigationUrl = params.url;
});

this.#cdpTarget.cdpClient.on('Page.lifecycleEvent', (params) => {
if (this.id !== params.frameId) {
return;
Expand Down
94 changes: 94 additions & 0 deletions tests/browsing_context/test_navigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,100 @@ async def test_browsingContext_navigationStartedEvent_viaScript(
}


@pytest.mark.asyncio
async def test_browsingContext_navigationStartedEvent_iframe_viaCommand(
websocket, context_id, url_base, html, iframe):
await subscribe(websocket, ["browsingContext.navigationStarted"])
iframe_url = html("<h1>FRAME</h1>")
page_url = html(iframe(iframe_url))
await send_JSON_command(
websocket, {
"method": "browsingContext.navigate",
"params": {
"context": context_id,
"url": page_url,
"wait": "complete",
}
})

response = await read_JSON_message(websocket)
assert response == {
'type': 'event',
"method": "browsingContext.navigationStarted",
"params": {
"context": context_id,
"navigation": ANY_UUID,
"timestamp": ANY_TIMESTAMP,
"url": page_url,
}
}

response = await read_JSON_message(websocket)
assert response == {
'type': 'event',
"method": "browsingContext.navigationStarted",
"params": {
"context": ANY_STR,
"navigation": ANY_UUID,
"timestamp": ANY_TIMESTAMP,
"url": iframe_url,
}
}


@pytest.mark.asyncio
async def test_browsingContext_navigationStartedEvent_iframe_viaScript(
websocket, context_id, url_base, html, iframe):
await subscribe(websocket, ["browsingContext.navigationStarted"])
iframe_url = html("<h1>FRAME</h1>")
page_url = html(iframe(iframe_url))

command_id = await send_JSON_command(
websocket, {
"method": "script.callFunction",
"params": {
"functionDeclaration": """(url) => {
location.href = url;
}""",
"arguments": [{
"type": "string",
"value": page_url
}],
"target": {
"context": context_id
},
"awaitPromise": True
}
})

response = await read_JSON_message(websocket)
assert response == {
'type': 'event',
"method": "browsingContext.navigationStarted",
"params": {
"context": context_id,
"navigation": ANY_UUID,
"timestamp": ANY_TIMESTAMP,
"url": page_url,
}
}

response = await read_JSON_message(websocket)
assert response == AnyExtending({"id": command_id, "type": "success"})

response = await read_JSON_message(websocket)
assert response == {
'type': 'event',
"method": "browsingContext.navigationStarted",
"params": {
"context": ANY_STR,
"navigation": ANY_UUID,
"timestamp": ANY_TIMESTAMP,
"url": iframe_url,
}
}


@pytest.mark.asyncio
async def test_browsingContext_navigationStartedEvent_viaCommand(
websocket, context_id, html):
Expand Down

0 comments on commit 318d621

Please sign in to comment.