Skip to content

Chrome DevTools for coding agents

fix: report page loads that keep the URL (#3395)

Thomas Bachem3cc38bb3640a
diff --git a/src/utils/WaitForHelper.ts b/src/utils/WaitForHelper.ts
index ec537953..f64b95d2 100644
--- a/src/utils/WaitForHelper.ts
+++ b/src/utils/WaitForHelper.ts
@@ -29,6 +29,7 @@ export class WaitForHelper {
/** Track all dialogs as they pause the renderer. */
#dialogDetected = false;
#initialUrl: string;
+ #loadedNewDocument = false;
#openedPages = new Set<Target>();
constructor(
@@ -177,6 +178,22 @@ export class WaitForHelper {
this.#publicPage.off('popup', popupHandler);
});
+ // CDP reports each new document the main frame commits, a reload or a
+ // back/forward cache restore included, with `Page.frameNavigated`, while
+ // a same-document navigation comes as `Page.navigatedWithinDocument`.
+ const frameNavigatedHandler = (
+ event: Protocol.Page.FrameNavigatedEvent,
+ ): void => {
+ if (!event.frame.parentId) {
+ this.#loadedNewDocument = true;
+ }
+ };
+ const client = this.#page._client();
+ client.on('Page.frameNavigated', frameNavigatedHandler);
+ this.#abortController.signal.addEventListener('abort', () => {
+ client.off('Page.frameNavigated', frameNavigatedHandler);
+ });
+
// A scoped AbortController used to clean up navigation probe listeners.
// When aborted (either after navigation detection finishes or if this.#abortController
// aborts), it removes CDP navigation probe listeners and automatically
@@ -302,7 +319,7 @@ export class WaitForHelper {
#getResult(): WaitForEventsResult {
const urlAfterAction = this.#page.url();
return {
- ...(urlAfterAction !== this.#initialUrl
+ ...(urlAfterAction !== this.#initialUrl || this.#loadedNewDocument
? {navigatedToUrl: urlAfterAction}
: {}),
dialogHandled: this.#dialogHandled,
diff --git a/tests/utils/WaitForHelper.test.ts b/tests/utils/WaitForHelper.test.ts
index b0a9b7d4..cdbaec7e 100644
--- a/tests/utils/WaitForHelper.test.ts
+++ b/tests/utils/WaitForHelper.test.ts
@@ -182,6 +182,26 @@ describe('WaitForHelper', () => {
});
});
+ it('captures a reload that keeps the URL', async () => {
+ await withMcpContext(async (response, context) => {
+ server.addHtmlRoute('/nav-reload', html`<main>reload</main>`);
+ const url = server.getRoute('/nav-reload');
+ const mcpPage = context.getSelectedMcpPage();
+ await mcpPage.pptrPage.goto(url);
+
+ const result = await mcpPage.waitForEventsAfterAction(
+ async () => {
+ await mcpPage.pptrPage.evaluate(() => {
+ setTimeout(() => location.reload(), 0);
+ });
+ },
+ {waitForStableDom: false, expectNavigationIn: 1000},
+ );
+
+ assert.strictEqual(result.navigatedToUrl, url);
+ });
+ });
+
it('captures navigatedToUrl for same-document navigation alone', async () => {
await withMcpContext(async (response, context) => {
server.addHtmlRoute('/nav-start-push', html`<main>start push</main>`);
@@ -204,6 +224,25 @@ describe('WaitForHelper', () => {
});
});
+ it('does not capture navigatedToUrl for same-document navigation to the same URL', async () => {
+ await withMcpContext(async (response, context) => {
+ server.addHtmlRoute('/nav-replace', html`<main>replace</main>`);
+ const mcpPage = context.getSelectedMcpPage();
+ await mcpPage.pptrPage.goto(server.getRoute('/nav-replace'));
+
+ const result = await mcpPage.waitForEventsAfterAction(
+ async () => {
+ await mcpPage.pptrPage.evaluate(() => {
+ history.replaceState({}, '', location.href);
+ });
+ },
+ {waitForStableDom: false},
+ );
+
+ assert.strictEqual(result.navigatedToUrl, undefined);
+ });
+ });
+
it('captures popup targets opened by the action', async () => {
const pptrPage = createMockPuppeteerPage();
pptrPage.url.returns('https://example.com');