Skip to content

Chrome DevTools for coding agents

fix: timout in WaitForHelper.ts (#2772)

Nikolay Vitkovdc1d055e162a
diff --git a/src/utils/WaitForHelper.ts b/src/utils/WaitForHelper.ts
index 3fb830a6..7389e986 100644
--- a/src/utils/WaitForHelper.ts
+++ b/src/utils/WaitForHelper.ts
@@ -212,7 +212,7 @@ export class WaitForHelper {
// If no navigation occurs, this.#abortController will cancel it in the finally block.
const navigationFinished = this.#page
.waitForNavigation({
- timeout: options?.timeout ?? this.#navigationTimeout,
+ timeout: 0,
signal: this.#abortController.signal,
ignoreSameDocumentNavigation: true,
})
@@ -253,7 +253,10 @@ export class WaitForHelper {
// Only await navigation if one was actually initiated; otherwise, the
// pending waitForNavigation promise will be cancelled when this.#abortController aborts.
if (navigationStarted) {
- await navigationFinished;
+ await Promise.race([
+ navigationFinished,
+ this.timeout(options?.timeout ?? this.#navigationTimeout),
+ ]);
}
if (this.#dialogDetected) {
diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.ts
index 1f55514b..529c3a5e 100644
--- a/tests/tools/pages.test.ts
+++ b/tests/tools/pages.test.ts
@@ -891,8 +891,9 @@ describe('pages', () => {
it('respects the timeout parameter', async () => {
await withMcpContext(async (response, context, args) => {
- const page = context.getSelectedMcpPage().pptrPage;
- const stub = sinon.stub(page, 'waitForNavigation').resolves(null);
+ const mcpPage = context.getSelectedMcpPage();
+ const waitForEventsSpy = sinon.spy(mcpPage, 'waitForEventsAfterAction');
+ const gotoSpy = sinon.spy(mcpPage.pptrPage, 'goto');
try {
await navigatePage(args).handler(
@@ -901,19 +902,25 @@ describe('pages', () => {
url: 'data:text/html,<html></html>',
timeout: 12345,
},
- page: context.getSelectedMcpPage(),
+ page: mcpPage,
},
response,
context,
);
} finally {
- stub.restore();
+ waitForEventsSpy.restore();
+ gotoSpy.restore();
}
+ sinon.assert.calledOnceWithExactly(
+ gotoSpy,
+ 'data:text/html,<html></html>',
+ {timeout: 12345},
+ );
assert.strictEqual(
- stub.firstCall.args[0]?.timeout,
+ waitForEventsSpy.firstCall.args[1]?.timeout,
12345,
- 'The timeout parameter should be passed to waitForNavigation',
+ 'The timeout parameter should be passed to waitForEventsAfterAction',
);
});
});
diff --git a/tests/utils/WaitForHelper.test.ts b/tests/utils/WaitForHelper.test.ts
index 27ea5f9d..e3301b76 100644
--- a/tests/utils/WaitForHelper.test.ts
+++ b/tests/utils/WaitForHelper.test.ts
@@ -53,9 +53,12 @@ describe('WaitForHelper', () => {
it('awaits navigation when action takes longer than expectNavigationIn', async () => {
await withMcpContext(async (response, context) => {
+ server.addHtmlRoute('/nav-initial', html`<main>initial</main>`);
server.addHtmlRoute('/nav-target', html`<main>navigated</main>`);
+ const startUrl = server.getRoute('/nav-initial');
const url = server.getRoute('/nav-target');
const mcpPage = context.getSelectedMcpPage();
+ await mcpPage.pptrPage.goto(startUrl);
const result = await mcpPage.waitForEventsAfterAction(
async () => {