Skip to content

Commit 6d487d6

Browse files
authored
feat(mcp): make post-action settle delay configurable (#41924)
1 parent ba223bb commit 6d487d6

6 files changed

Lines changed: 15 additions & 2 deletions

File tree

packages/playwright-core/src/tools/backend/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type ContextConfig = {
5656
action?: number;
5757
navigation?: number;
5858
expect?: number;
59+
settle?: number;
5960
};
6061
browser?: {
6162
initScript?: string[];

packages/playwright-core/src/tools/backend/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type * as playwright from '../../..';
1818
import type { Tab } from './tab';
1919

2020
export async function waitForCompletion<R>(tab: Tab, callback: () => Promise<R>): Promise<R> {
21+
const settleMs = tab.context.config.timeouts?.settle ?? 500;
2122
const requests: playwright.Request[] = [];
2223

2324
const requestListener = (request: playwright.Request) => requests.push(request);
@@ -29,7 +30,7 @@ export async function waitForCompletion<R>(tab: Tab, callback: () => Promise<R>)
2930
let result: R;
3031
try {
3132
result = await callback();
32-
await tab.waitForTimeout(500);
33+
await tab.waitForTimeout(settleMs);
3334
} finally {
3435
disposeListeners();
3536
}
@@ -50,7 +51,7 @@ export async function waitForCompletion<R>(tab: Tab, callback: () => Promise<R>)
5051
const timeout = new Promise<void>(resolve => setTimeout(resolve, 5000));
5152
await Promise.race([Promise.all(promises), timeout]);
5253
if (requests.length)
53-
await tab.waitForTimeout(500);
54+
await tab.waitForTimeout(settleMs);
5455

5556
return result;
5657
}

packages/playwright-core/src/tools/mcp/config.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ export type Config = {
210210
* Configures default expect timeout: https://playwright.dev/docs/test-timeouts#expect-timeout. Defaults to 5000ms.
211211
*/
212212
expect?: number;
213+
214+
/**
215+
* How long to wait after each action for triggered work (navigations, requests) to settle before responding. Defaults to 500ms.
216+
*/
217+
settle?: number;
213218
};
214219

215220
/**

packages/playwright-core/src/tools/mcp/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export type CLIOptions = {
7474
testIdAttribute?: string;
7575
timeoutAction?: number;
7676
timeoutNavigation?: number;
77+
timeoutSettle?: number;
7778
userAgent?: string;
7879
userDataDir?: string;
7980
viewportSize?: ViewportSize;
@@ -88,6 +89,7 @@ const defaultConfig: MergedConfig = {
8889
action: 5000,
8990
navigation: 60000,
9091
expect: 5000,
92+
settle: 500,
9193
},
9294
};
9395

@@ -378,6 +380,7 @@ function configFromCLIOptions(cliOptions: CLIOptions): Config & { configFile?: s
378380
timeouts: {
379381
action: cliOptions.timeoutAction,
380382
navigation: cliOptions.timeoutNavigation,
383+
settle: cliOptions.timeoutSettle,
381384
},
382385
};
383386

@@ -434,6 +437,7 @@ export function configFromEnv(env?: NodeJS.ProcessEnv): Config & { configFile?:
434437
options.testIdAttribute = envToString(e.PLAYWRIGHT_MCP_TEST_ID_ATTRIBUTE);
435438
options.timeoutAction = numberParser(e.PLAYWRIGHT_MCP_TIMEOUT_ACTION);
436439
options.timeoutNavigation = numberParser(e.PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION);
440+
options.timeoutSettle = numberParser(e.PLAYWRIGHT_MCP_TIMEOUT_SETTLE);
437441
options.userAgent = envToString(e.PLAYWRIGHT_MCP_USER_AGENT);
438442
options.userDataDir = envToString(e.PLAYWRIGHT_MCP_USER_DATA_DIR);
439443
options.viewportSize = resolutionParser('--viewport-size', e.PLAYWRIGHT_MCP_VIEWPORT_SIZE);

packages/playwright-core/src/tools/mcp/configIni.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const longhandTypes: Record<string, LonghandType> = {
181181
// timeouts
182182
'timeouts.action': 'number',
183183
'timeouts.navigation': 'number',
184+
'timeouts.settle': 'number',
184185

185186
// snapshot
186187
'snapshot.mode': 'string',

packages/playwright-core/src/tools/mcp/program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export function decorateMCPCommand(command: Command) {
7474
.option('--test-id-attribute <attribute>', 'specify the attribute to use for test ids, defaults to "data-testid"')
7575
.option('--timeout-action <timeout>', 'specify action timeout in milliseconds, defaults to 5000ms', numberParser)
7676
.option('--timeout-navigation <timeout>', 'specify navigation timeout in milliseconds, defaults to 60000ms', numberParser)
77+
.option('--timeout-settle <timeout>', 'how long to wait after each action for triggered work to settle, in milliseconds, defaults to 500ms', numberParser)
7778
.option('--user-agent <ua string>', 'specify user agent string')
7879
.option('--user-data-dir <path>', 'path to the user data directory. If not specified, a temporary directory will be created.')
7980
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280x720"', resolutionParser.bind(null, '--viewport-size'))

0 commit comments

Comments
 (0)