Chrome DevTools for coding agents
fix: bound take_snapshot wait and clear the geolocation override (#3265)
Alex Rudenko6282f548b0f3
diff --git a/src/McpPage.ts b/src/McpPage.tsindex 9533ad6b..51ba3874 100644--- a/src/McpPage.ts+++ b/src/McpPage.ts@@ -1240,7 +1240,7 @@ export class McpPage implements ContextPage {}if (!options.geolocation) {- await page.setGeolocation({latitude: 0, longitude: 0});+ await page.setGeolocation(null);delete newSettings.geolocation;} else {await page.setGeolocation(options.geolocation);diff --git a/src/TextSnapshot.ts b/src/TextSnapshot.tsindex 72be3b96..66b60f63 100644--- a/src/TextSnapshot.ts+++ b/src/TextSnapshot.ts@@ -13,6 +13,9 @@ import type {import type {DevToolsData} from './tools/ToolDefinition.js';import type {TextSnapshotNode} from './types.js';import {logger} from './utils/logger.js';+import {withTimeout} from './utils/timeout.js';++const ACCESSIBILITY_SNAPSHOT_TIMEOUT_MS = 10_000;/*** InlineTextBox nodes are layout line fragments of a StaticText node. They@@ -62,10 +65,16 @@ export class TextSnapshot {} = {},): Promise<TextSnapshot> {const verbose = options.verbose ?? false;- const rootNode = await page.pptrPage.accessibility.snapshot({- includeIframes: true,- interestingOnly: !verbose,- });+ const rootNode = await withTimeout(+ page.pptrPage.accessibility.snapshot({+ includeIframes: true,+ interestingOnly: !verbose,+ }),+ {+ timeout: ACCESSIBILITY_SNAPSHOT_TIMEOUT_MS,+ message: `Timed out after ${ACCESSIBILITY_SNAPSHOT_TIMEOUT_MS}ms waiting for the accessibility tree. The page may have a pending navigation or a crashed renderer. Navigate or reload the page, or select another page.`,+ },+ );if (!rootNode) {throw new Error('Failed to create accessibility snapshot');}diff --git a/src/third_party/index.ts b/src/third_party/index.tsindex 35b46249..40d95f69 100644--- a/src/third_party/index.ts+++ b/src/third_party/index.ts@@ -81,6 +81,7 @@ export type {Realm} from 'puppeteer-core/internal/api/Realm.js';export {FrameEvent} from 'puppeteer-core/internal/api/Frame.js';export type {JSONSchema7, JSONSchema7Definition} from 'json-schema';export {Mutex} from 'puppeteer-core/internal/util/Mutex.js';+export {Deferred} from 'puppeteer-core/internal/util/Deferred.js';export {DisposableStack,AsyncDisposableStack,diff --git a/src/utils/timeout.ts b/src/utils/timeout.tsnew file mode 100644index 00000000..db408b0f--- /dev/null+++ b/src/utils/timeout.ts@@ -0,0 +1,18 @@+/**+ * @license+ * Copyright 2026 Google LLC+ * SPDX-License-Identifier: Apache-2.0+ */++import {Deferred} from '../third_party/index.js';++/**+ * Rejects with a Puppeteer `TimeoutError` with `message` if `promise` does not+ * settle within `timeout` ms. The timer is cleared when the race settles.+ */+export async function withTimeout<T>(+ promise: Promise<T>,+ options: {timeout: number; message: string},+): Promise<T> {+ return await Deferred.race([promise, Deferred.create<T>(options)]);+}diff --git a/tests/McpPage.test.ts b/tests/McpPage.test.tsindex 90e822d6..dbe71f8d 100644--- a/tests/McpPage.test.ts+++ b/tests/McpPage.test.ts@@ -689,7 +689,7 @@ describe('McpPage', () => {});});- it('calls setGeolocation with (0, 0) when geolocation is omitted', async () => {+ it('clears the geolocation override when geolocation is omitted', async () => {const {mcpPage, pptrPage} = await createMcpPage();await mcpPage.emulate({geolocation: {latitude: 48.137154, longitude: 11.576124},@@ -697,10 +697,7 @@ describe('McpPage', () => {await mcpPage.emulate({});assert.strictEqual(mcpPage.geolocation, null);sinon.assert.calledTwice(pptrPage.setGeolocation);- sinon.assert.calledWithExactly(pptrPage.setGeolocation.secondCall, {- latitude: 0,- longitude: 0,- });+ sinon.assert.calledWithExactly(pptrPage.setGeolocation.secondCall, null);});it('calls setUserAgent with the given user agent', async () => {diff --git a/tests/TextSnapshot.test.ts b/tests/TextSnapshot.test.tsindex 7c1970f5..f84ec773 100644--- a/tests/TextSnapshot.test.ts+++ b/tests/TextSnapshot.test.ts@@ -7,6 +7,8 @@import assert from 'node:assert';import {afterEach, describe, it} from 'node:test';+import sinon from 'sinon';+import {TextSnapshot} from '../src/TextSnapshot.js';import type {TextSnapshotNode} from '../src/types.js';@@ -18,6 +20,19 @@ describe('TextSnapshot', () => {TextSnapshot.resetCounter();});+ it('rejects when the accessibility tree does not arrive within the timeout', async () => {+ const clock = sinon.useFakeTimers({toFake: ['setTimeout', 'clearTimeout']});+ const page = createMockMcpPage();+ page.pptrPage.accessibility.snapshot.returns(new Promise(() => {}));++ const assertion = assert.rejects(+ TextSnapshot.create(page),+ /Timed out after 10000ms waiting for the accessibility tree/,+ );+ await clock.tickAsync(10_000);+ await assertion;+ });+it('does not assign uids to nodes without backendNodeId', async () => {const page = createMockMcpPage();page.uniqueBackendNodeIdToMcpId = new Map();diff --git a/tests/utils/timeout.test.ts b/tests/utils/timeout.test.tsnew file mode 100644index 00000000..cd8441e0--- /dev/null+++ b/tests/utils/timeout.test.ts@@ -0,0 +1,38 @@+/**+ * @license+ * Copyright 2026 Google LLC+ * SPDX-License-Identifier: Apache-2.0+ */++import assert from 'node:assert';+import {describe, it} from 'node:test';++import sinon from 'sinon';++import {TimeoutError} from '../../src/third_party/index.js';+import {withTimeout} from '../../src/utils/timeout.js';++describe('withTimeout', () => {+ it('returns the value when the promise settles before the timeout', async () => {+ const result = await withTimeout(Promise.resolve('value'), {+ timeout: 1000,+ message: 'timed out',+ });+ assert.strictEqual(result, 'value');+ });++ it('rejects with a TimeoutError when the promise does not settle in time', async () => {+ const clock = sinon.useFakeTimers({toFake: ['setTimeout', 'clearTimeout']});+ const result = withTimeout(new Promise(() => {}), {+ timeout: 1000,+ message: 'timed out',+ });+ const assertion = assert.rejects(result, (error: unknown) => {+ assert.ok(error instanceof TimeoutError);+ assert.strictEqual(error.message, 'timed out');+ return true;+ });+ await clock.tickAsync(1000);+ await assertion;+ });+});