Chrome DevTools for coding agents
fix(wait): explain whitespace-sensitive text matching (#3350)
a576854981a037939b19
diff --git a/docs/tool-reference.md b/docs/tool-reference.mdindex a18967cd..276a0587 100644--- a/docs/tool-reference.md+++ b/docs/tool-reference.md@@ -270,7 +270,7 @@**Parameters:**- **pageId** (number) **(required)**: Targets a specific page by ID.-- **text** (array) **(required)**: Non-empty list of texts. Resolves when any value appears on the page.+- **text** (array) **(required)**: Non-empty list of texts. Resolves when any value matches an accessible name exactly or is a substring of DOM text. Matching is case-sensitive. DOM text preserves spaces and newlines, unlike snapshot text which may normalize whitespace. Use the original whitespace or a shorter distinctive substring.- **timeout** (integer) _(optional)_: Maximum wait time in milliseconds. If set to 0, the default timeout will be used.- **visible** (boolean) _(optional)_: If true, wait until the matching text is visible. By default, hidden text also counts.diff --git a/src/tools/snapshot.ts b/src/tools/snapshot.tsindex 420abdff..15df41a7 100644--- a/src/tools/snapshot.ts+++ b/src/tools/snapshot.ts@@ -4,7 +4,7 @@* SPDX-License-Identifier: Apache-2.0*/-import {zod} from '../third_party/index.js';+import {TimeoutError, zod} from '../third_party/index.js';import {ToolCategory} from './categories.js';import {@@ -62,7 +62,11 @@ export const waitFor: PageToolFactory = definePageTool(() => ({.array(zod.string()).min(1).describe(- 'Non-empty list of texts. Resolves when any value appears on the page.',+ 'Non-empty list of texts. Resolves when any value matches an ' ++ 'accessible name exactly or is a substring of DOM text. Matching ' ++ 'is case-sensitive. DOM text preserves spaces and newlines, unlike ' ++ 'snapshot text which may normalize whitespace. Use the original ' ++ 'whitespace or a shorter distinctive substring.',),...timeoutSchema,visible: zod@@ -78,10 +82,22 @@ export const waitFor: PageToolFactory = definePageTool(() => ({verifyFilesSchema: {},handler: async (request, response) => {const page = request.page;- await page.waitForTextOnPage(request.params.text, {- timeout: request.params.timeout,- visible: request.params.visible,- });+ try {+ await page.waitForTextOnPage(request.params.text, {+ timeout: request.params.timeout,+ visible: request.params.visible,+ });+ } catch (error) {+ if (error instanceof TimeoutError) {+ throw new TimeoutError(+ `${error.message}. Text matching is case-sensitive. DOM text must ` ++ 'preserve spaces and newlines, whereas snapshot text may ' ++ 'normalize whitespace. Try a shorter distinctive substring.',+ {cause: error},+ );+ }+ throw error;+ }response.appendResponseLine(`Element matching one of ${JSON.stringify(request.params.text)} found.`,diff --git a/tests/tools/snapshot.test.ts b/tests/tools/snapshot.test.tsindex 05597648..80193c1f 100644--- a/tests/tools/snapshot.test.ts+++ b/tests/tools/snapshot.test.ts@@ -4,12 +4,15 @@* SPDX-License-Identifier: Apache-2.0*/+import assert from 'node:assert/strict';import {describe, it} from 'node:test';import sinon from 'sinon';+import {McpResponse} from '../../src/McpResponse.js';+import {TimeoutError} from '../../src/third_party/index.js';import {takeSnapshot, waitFor} from '../../src/tools/snapshot.js';-import {createHandlerMocks} from '../mocks.js';+import {createHandlerMocks, createMockServerConfig} from '../mocks.js';describe('snapshot', () => {describe('take_snapshot', () => {@@ -37,6 +40,90 @@ describe('snapshot', () => {});describe('wait_for', () => {+ it('explains whitespace matching when the wait times out', async () => {+ const {page, context, response, args} = createHandlerMocks();+ const error = new TimeoutError('Timed out after waiting 2000ms');+ page.waitForTextOnPage.rejects(error);++ await assert.rejects(+ waitFor(args).handler(+ {params: {text: ['Order confirmed'], timeout: 2000}, page},+ response,+ context,+ ),+ thrown => {+ assert.ok(thrown instanceof TimeoutError);+ assert.strictEqual(thrown.cause, error);+ assert.strictEqual(+ thrown.message,+ `${error.message}. Text matching is case-sensitive. DOM text must ` ++ 'preserve spaces and newlines, whereas snapshot text may ' ++ 'normalize whitespace. Try a shorter distinctive substring.',+ );+ return true;+ },+ );+ sinon.assert.notCalled(response.appendResponseLine);+ sinon.assert.notCalled(response.includeSnapshot);+ });++ it('keeps timeout advice in the formatted error and structuredContent', async () => {+ const {page, context, args} = createHandlerMocks();+ const response = new McpResponse(createMockServerConfig());+ const error = new TimeoutError('Timed out after waiting 2000ms');+ page.waitForTextOnPage.rejects(error);+ await assert.rejects(+ waitFor(args).handler(+ {params: {text: ['Order confirmed']}, page},+ response,+ context,+ ),+ thrown => {+ assert.ok(thrown instanceof TimeoutError);+ response.setError(thrown);+ return true;+ },+ );+ const result = await response.handle(context);+ const message =+ `${error.message}. Text matching is case-sensitive. DOM text must ` ++ 'preserve spaces and newlines, whereas snapshot text may ' ++ 'normalize whitespace. Try a shorter distinctive substring.';+ assert.deepStrictEqual(result.content, [+ {type: 'text', text: `Error: ${message}`},+ ]);+ assert.deepStrictEqual(result.structuredContent, {errorMessage: message});+ });++ it('preserves newlines and repeated spaces in the wait pattern', async () => {+ const {page, context, response, args} = createHandlerMocks();+ const text = ['Order\n confirmed', 'Multi spaced words'];+ await waitFor(args).handler({params: {text}, page}, response, context);+ sinon.assert.calledOnceWithExactly(page.waitForTextOnPage, text, {+ timeout: undefined,+ visible: undefined,+ });+ sinon.assert.calledOnceWithExactly(+ response.appendResponseLine,+ `Element matching one of ${JSON.stringify(text)} found.`,+ );+ });++ it('does not add whitespace advice for non-timeout errors', async () => {+ const {page, context, response, args} = createHandlerMocks();+ const error = new Error('Target closed');+ page.waitForTextOnPage.rejects(error);+ await assert.rejects(+ waitFor(args).handler(+ {params: {text: ['Order confirmed']}, page},+ response,+ context,+ ),+ error,+ );+ sinon.assert.notCalled(response.appendResponseLine);+ sinon.assert.notCalled(response.includeSnapshot);+ });it('waits for text and appends response line', async () => {const {page, context, response, args} = createHandlerMocks();await waitFor(args).handler(