Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(mcp): report capped time in browser_wait_for (#42932)

Yury Semikhatskyafa6836a5119
diff --git a/packages/playwright-core/src/tools/backend/wait.ts b/packages/playwright-core/src/tools/backend/wait.ts
index 11aa2a8d5e..95ba71d1be 100644
--- a/packages/playwright-core/src/tools/backend/wait.ts
+++ b/packages/playwright-core/src/tools/backend/wait.ts
@@ -17,6 +17,8 @@
import * as z from 'zod';
import { defineTool } from './tool';
+const maxWaitSeconds = 30;
+
const wait = defineTool({
capability: 'core',
@@ -25,7 +27,7 @@ const wait = defineTool({
title: 'Wait for',
description: 'Wait for text to appear or disappear or a specified time to pass',
inputSchema: z.object({
- time: z.number().optional().describe('The time to wait in seconds'),
+ time: z.number().optional().describe(`The time to wait in seconds, at most ${maxWaitSeconds}`),
text: z.string().optional().describe('The text to wait for'),
textGone: z.string().optional().describe('The text to wait for to disappear'),
}),
@@ -36,9 +38,10 @@ const wait = defineTool({
if (!params.text && !params.textGone && !params.time)
throw new Error('Either time, text or textGone must be provided');
- if (params.time) {
- response.addCode(`await new Promise(f => setTimeout(f, ${params.time!} * 1000));`);
- await new Promise(f => setTimeout(f, Math.min(30000, params.time! * 1000)));
+ const time = params.time ? Math.min(maxWaitSeconds, params.time) : undefined;
+ if (time) {
+ response.addCode(`await new Promise(f => setTimeout(f, ${time} * 1000));`);
+ await new Promise(f => setTimeout(f, time * 1000));
}
const tab = context.currentTabOrDie();
@@ -55,7 +58,12 @@ const wait = defineTool({
await locator.waitFor({ state: 'visible', ...tab.actionTimeoutOptions });
}
- response.addTextResult(`Waited for ${params.text || params.textGone || params.time}`);
+ if (params.text || params.textGone)
+ response.addTextResult(`Waited for ${params.text || params.textGone}`);
+ else if (time !== params.time)
+ response.addTextResult(`Waited for ${time} seconds (requested ${params.time}, maximum is ${maxWaitSeconds})`);
+ else
+ response.addTextResult(`Waited for ${time} seconds`);
response.setIncludeSnapshot();
},
});
diff --git a/tests/mcp/wait.spec.ts b/tests/mcp/wait.spec.ts
index 561f375e51..a179486807 100644
--- a/tests/mcp/wait.spec.ts
+++ b/tests/mcp/wait.spec.ts
@@ -112,6 +112,25 @@ test('browser_wait_for(time)', async ({ client, server }) => {
name: 'browser_wait_for',
arguments: { time: 1 },
})).toHaveResponse({
+ result: `Waited for 1 seconds`,
code: `await new Promise(f => setTimeout(f, 1 * 1000));`,
});
});
+
+test('browser_wait_for(time) reports capped time', async ({ client, server }) => {
+ test.slow();
+ server.setContent('/', `<body><div>Hello World</div></body>`, 'text/html');
+
+ await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: server.PREFIX },
+ });
+
+ expect(await client.callTool({
+ name: 'browser_wait_for',
+ arguments: { time: 70 },
+ })).toHaveResponse({
+ result: `Waited for 30 seconds (requested 70, maximum is 30)`,
+ code: `await new Promise(f => setTimeout(f, 30 * 1000));`,
+ });
+});