fix: timout in WaitForHelper.ts (#2772)

This fixes two issues: 
- Timer would start too early and could timeout before the navigation
begin. Clean up is handled by the AbortController
- Cross process navigation can be slow on Windows, we started from
`about:blank` to `http://127.0.0.1`
This commit is contained in:
Nikolay Vitkov
2026-09-21 08:23:58 +00:00
committed by GitHub
parent 882f93e9a8
commit dc1d055e16
3 changed files with 21 additions and 8 deletions
+5 -2
View File
@@ -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) {
+13 -6
View File
@@ -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',
);
});
});
+3
View File
@@ -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 () => {