diff --git a/docs/tool-reference.md b/docs/tool-reference.md index 7bf9e7669c..711a77f0d3 100644 --- a/docs/tool-reference.md +++ b/docs/tool-reference.md @@ -359,6 +359,7 @@ - **args** (array) _(optional)_: An optional list of arguments to pass to the function. - **dialogAction** (string) _(optional)_: Handle dialogs while execution. "accept", "dismiss", or string for response of window.prompt. Defaults to accept. - **filePath** (string) _(optional)_: The absolute or relative path to a file to save the script output to. If omitted, the output is returned inline. +- **waitForStableDom** (boolean) _(optional)_: Whether to wait for the DOM to settle. Pass false if the script only reads data. Defaults to true. --- diff --git a/src/McpPage.ts b/src/McpPage.ts index 21d9ca93a9..5d0b1af3d9 100644 --- a/src/McpPage.ts +++ b/src/McpPage.ts @@ -416,6 +416,7 @@ export class McpPage implements ContextPage { action: () => Promise, options?: { timeout?: number; + waitForStableDom?: boolean; handleDialog?: DialogAction | Partial>; }, diff --git a/src/WaitForHelper.ts b/src/WaitForHelper.ts index 7724b35890..d06481d6cc 100644 --- a/src/WaitForHelper.ts +++ b/src/WaitForHelper.ts @@ -145,6 +145,7 @@ export class WaitForHelper { action: () => Promise, options?: { timeout?: number; + waitForStableDom?: boolean; handleDialog?: DialogAction | Partial>; }, @@ -214,7 +215,9 @@ export class WaitForHelper { // Wait for stable dom after navigation so we execute in // the correct context - await this.waitForStableDom(); + if (options?.waitForStableDom !== false) { + await this.waitForStableDom(); + } } catch (error) { logger?.(error); } finally { diff --git a/src/bin/chrome-devtools-cli-options.ts b/src/bin/chrome-devtools-cli-options.ts index 010421f5ba..5026670244 100644 --- a/src/bin/chrome-devtools-cli-options.ts +++ b/src/bin/chrome-devtools-cli-options.ts @@ -249,6 +249,13 @@ export const commands: Commands = { 'Handle dialogs while execution. "accept", "dismiss", or string for response of window.prompt. Defaults to accept.', required: false, }, + waitForStableDom: { + name: 'waitForStableDom', + type: 'boolean', + description: + 'Whether to wait for the DOM to settle. Pass false if the script only reads data. Defaults to true.', + required: false, + }, }, }, execute_3p_developer_tool: { diff --git a/src/telemetry/tool_call_metrics.json b/src/telemetry/tool_call_metrics.json index 194ac0ddd5..e0305fdd6b 100644 --- a/src/telemetry/tool_call_metrics.json +++ b/src/telemetry/tool_call_metrics.json @@ -119,6 +119,10 @@ { "name": "file_path_length", "argType": "number" + }, + { + "name": "wait_for_stable_dom", + "argType": "boolean" } ] }, diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts index 7689e5a0d6..faa838441f 100644 --- a/src/tools/ToolDefinition.ts +++ b/src/tools/ToolDefinition.ts @@ -314,6 +314,7 @@ export type ContextPage = Readonly<{ action: () => Promise, options?: { timeout?: number; + waitForStableDom?: boolean; handleDialog?: DialogAction | Partial>; }, diff --git a/src/tools/script.ts b/src/tools/script.ts index 3c7611a3e4..9223bac10e 100644 --- a/src/tools/script.ts +++ b/src/tools/script.ts @@ -52,6 +52,12 @@ Example with arguments: \`(el) => el.innerText\` .describe( 'Handle dialogs while execution. "accept", "dismiss", or string for response of window.prompt. Defaults to accept.', ), + waitForStableDom: zod + .boolean() + .optional() + .describe( + 'Whether to wait for the DOM to settle. Pass false if the script only reads data. Defaults to true.', + ), ...(cliArgs?.categoryExtensions ? { serviceWorkerId: zod @@ -73,6 +79,7 @@ Example with arguments: \`(el) => el.innerText\` pageId, dialogAction, filePath, + waitForStableDom, } = request.params; if (cliArgs?.categoryExtensions && serviceWorkerId) { @@ -95,7 +102,8 @@ Example with arguments: \`(el) => el.innerText\` context, }); }, - {handleDialog: dialogAction ?? 'accept'}, + // Service workers cannot interact with the DOM, so never wait for it. + {handleDialog: dialogAction ?? 'accept', waitForStableDom: false}, ); if (result.dialogHandled) { context.getSelectedMcpPage().clearDialog(); @@ -129,7 +137,7 @@ Example with arguments: \`(el) => el.innerText\` context, }); }, - {handleDialog: dialogAction ?? 'accept'}, + {handleDialog: dialogAction ?? 'accept', waitForStableDom}, ); response.attachWaitForResult(result); }, diff --git a/tests/tools/script.test.ts b/tests/tools/script.test.ts index c2883ab70c..c590cca713 100644 --- a/tests/tools/script.test.ts +++ b/tests/tools/script.test.ts @@ -8,14 +8,18 @@ import assert from 'node:assert'; import path from 'node:path'; import {describe, it} from 'node:test'; +import sinon from 'sinon'; + import type {ParsedArguments} from '../../src/bin/chrome-devtools-mcp-cli-options.js'; import {TextSnapshot} from '../../src/TextSnapshot.js'; import {installExtension} from '../../src/tools/extensions.js'; import {evaluateScript} from '../../src/tools/script.js'; +import {WaitForHelper} from '../../src/WaitForHelper.js'; import {serverHooks} from '../server.js'; import { assertNoServiceWorkerReported, extractExtensionId, + getTextContent, html, withMcpContext, } from '../utils.js'; @@ -42,6 +46,56 @@ describe('script', () => { assert.strictEqual(JSON.parse(lineEvaluation), 10); }); }); + it('skips the stable DOM wait when waitForStableDom is false', async () => { + await withMcpContext(async (response, context) => { + const spy = sinon.spy(WaitForHelper.prototype, 'waitForStableDom'); + try { + await evaluateScript().handler( + { + params: {function: String(() => 1), waitForStableDom: false}, + }, + response, + context, + ); + sinon.assert.notCalled(spy); + + await evaluateScript().handler( + { + params: {function: String(() => 1)}, + }, + response, + context, + ); + sinon.assert.calledOnce(spy); + } finally { + spy.restore(); + } + }); + }); + it('still awaits a navigation when waitForStableDom is false', async () => { + await withMcpContext(async (response, context) => { + server.addHtmlRoute('/nav-target', html`
navigated
`); + const url = server.getRoute('/nav-target'); + await evaluateScript().handler( + { + params: { + function: `() => { + location.href = '${url}'; + }`, + waitForStableDom: false, + }, + }, + response, + context, + ); + const result = await response.handle(context); + const textContent = getTextContent(result.content[0]); + assert.ok( + textContent.includes(`Page navigated to ${url}`), + `Expected the navigation to be awaited and reported, got: ${textContent}`, + ); + }); + }); it('runs in selected page', async () => { await withMcpContext(async (response, context) => { await evaluateScript().handler(