Skip to content

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

chore: allow not generating code from mcp commands (#38821)

Pavel Feldman57cd12676f62
diff --git a/packages/playwright/src/mcp/browser/config.ts b/packages/playwright/src/mcp/browser/config.ts
index 6a87338be9..bed7edea6f 100644
--- a/packages/playwright/src/mcp/browser/config.ts
+++ b/packages/playwright/src/mcp/browser/config.ts
@@ -39,6 +39,7 @@ export type CLIOptions = {
caps?: string[];
cdpEndpoint?: string;
cdpHeader?: Record<string, string>;
+ codegen?: 'typescript' | 'none';
config?: string;
consoleLevel?: 'error' | 'warning' | 'info' | 'debug';
device?: string;
@@ -249,6 +250,7 @@ export function configFromCLIOptions(cliOptions: CLIOptions): Config {
blockedOrigins: cliOptions.blockedOrigins,
},
allowUnrestrictedFileAccess: cliOptions.allowUnrestrictedFileAccess,
+ codegen: cliOptions.codegen,
saveSession: cliOptions.saveSession,
saveTrace: cliOptions.saveTrace,
saveVideo: cliOptions.saveVideo,
diff --git a/packages/playwright/src/mcp/browser/response.ts b/packages/playwright/src/mcp/browser/response.ts
index 14d0bb8968..095a3c1394 100644
--- a/packages/playwright/src/mcp/browser/response.ts
+++ b/packages/playwright/src/mcp/browser/response.ts
@@ -111,8 +111,8 @@ export class Response {
// All the async snapshotting post-action is happening here.
// Everything below should race against modal states.
- if (this._includeSnapshot !== 'none' && this._context.currentTab())
- this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot();
+ if (this._context.currentTab())
+ this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot(this._includeSnapshot !== 'none');
for (const tab of this._context.tabs())
await tab.updateTitle();
}
@@ -138,7 +138,7 @@ export class Response {
renderedResponse.results.push(...this._result);
// Add code if it exists.
- if (this._code.length)
+ if (this._code.length && this._context.config.codegen !== 'none')
renderedResponse.code.push(...this._code);
// List browser tabs.
@@ -152,11 +152,11 @@ export class Response {
if (this._tabSnapshot?.modalStates.length) {
const modalStatesMarkdown = renderModalStates(this._tabSnapshot.modalStates);
renderedResponse.states.modal = modalStatesMarkdown.join('\n');
- } else if (this._tabSnapshot) {
- renderTabSnapshot(this._tabSnapshot, this._includeSnapshot, renderedResponse);
} else if (this._includeModalStates) {
const modalStatesMarkdown = renderModalStates(this._includeModalStates);
renderedResponse.states.modal = modalStatesMarkdown.join('\n');
+ } else if (this._tabSnapshot) {
+ renderTabSnapshot(this._tabSnapshot, this._includeSnapshot, renderedResponse);
}
if (this._files.length) {
@@ -202,7 +202,7 @@ function renderTabSnapshot(tabSnapshot: TabSnapshot, includeSnapshot: 'none' | '
if (tabSnapshot.consoleMessages.length) {
const lines: string[] = [];
for (const message of tabSnapshot.consoleMessages)
- lines.push(`- ${trim(message.toString(), 100)}`);
+ lines.push(`- ${trimMiddle(message.toString(), 100)}`);
response.updates.push({ category: 'console', content: lines.join('\n') });
}
@@ -254,10 +254,10 @@ function renderTabsMarkdown(tabs: Tab[], force: boolean = false): string[] {
return lines;
}
-function trim(text: string, maxLength: number) {
+function trimMiddle(text: string, maxLength: number) {
if (text.length <= maxLength)
return text;
- return text.slice(0, maxLength) + '...';
+ return text.slice(0, Math.floor(maxLength / 2)) + '...' + text.slice(- 3 - Math.floor(maxLength / 2));
}
export class RenderedResponse {
diff --git a/packages/playwright/src/mcp/browser/tab.ts b/packages/playwright/src/mcp/browser/tab.ts
index 29a053c207..958a9c27a2 100644
--- a/packages/playwright/src/mcp/browser/tab.ts
+++ b/packages/playwright/src/mcp/browser/tab.ts
@@ -225,11 +225,11 @@ export class Tab extends EventEmitter<TabEventsInterface> {
return this._requests;
}
- async captureSnapshot(): Promise<TabSnapshot> {
+ async captureSnapshot(includePageSnapshot: boolean): Promise<TabSnapshot> {
await this._initializedPromise;
let tabSnapshot: TabSnapshot | undefined;
const modalStates = await this._raceAgainstModalStates(async () => {
- const snapshot = await this.page._snapshotForAI({ track: 'response' });
+ const snapshot = includePageSnapshot ? await this.page._snapshotForAI({ track: 'response' }) : { full: '', incremental: '' };
tabSnapshot = {
url: this.page.url(),
title: await this.page.title(),
diff --git a/packages/playwright/src/mcp/browser/tools/navigate.ts b/packages/playwright/src/mcp/browser/tools/navigate.ts
index 519bef07b8..e68c65e6cf 100644
--- a/packages/playwright/src/mcp/browser/tools/navigate.ts
+++ b/packages/playwright/src/mcp/browser/tools/navigate.ts
@@ -32,7 +32,17 @@ const navigate = defineTool({
handle: async (context, params, response) => {
const tab = await context.ensureTab();
- await tab.navigate(params.url);
+ let url = params.url;
+ try {
+ new URL(url);
+ } catch (e) {
+ if (url.startsWith('localhost'))
+ url = 'http://' + url;
+ else
+ url = 'https://' + url;
+ }
+
+ await tab.navigate(url);
response.setIncludeSnapshot();
response.addCode(`await page.goto('${params.url}');`);
diff --git a/packages/playwright/src/mcp/config.d.ts b/packages/playwright/src/mcp/config.d.ts
index bfe28a4848..823ccfb985 100644
--- a/packages/playwright/src/mcp/config.d.ts
+++ b/packages/playwright/src/mcp/config.d.ts
@@ -200,4 +200,9 @@ export type Config = {
* By default (false), file uploads are restricted to paths within the MCP roots only.
*/
allowUnrestrictedFileAccess?: boolean;
+
+ /**
+ * Specify the language to use for code generation.
+ */
+ codegen?: 'typescript' | 'none';
};
diff --git a/packages/playwright/src/mcp/program.ts b/packages/playwright/src/mcp/program.ts
index 02ed0e443c..77aee4edef 100644
--- a/packages/playwright/src/mcp/program.ts
+++ b/packages/playwright/src/mcp/program.ts
@@ -73,6 +73,7 @@ export function decorateCommand(command: Command, version: string) {
.option('--user-agent <ua string>', 'specify user agent string')
.option('--user-data-dir <path>', 'path to the user data directory. If not specified, a temporary directory will be created.')
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280x720"', resolutionParser.bind(null, '--viewport-size'))
+ .option('--codegen <lang>', 'specify the language to use for code generation, possible values: "typescript", "none". Default is "typescript".', enumParser.bind(null, '--codegen', ['none', 'typescript']))
.addOption(new ProgramOption('--vision', 'Legacy option, use --caps=vision instead').hideHelp())
.addOption(new ProgramOption('--daemon <socket>', 'run as daemon').hideHelp())
.action(async options => {
@@ -109,6 +110,8 @@ export function decorateCommand(command: Command, version: string) {
}
if (options.daemon) {
+ config.snapshot.mode = 'none';
+ config.codegen = 'none';
const serverBackendFactory: mcpServer.ServerBackendFactory = {
name: 'Playwright',
nameInConfig: 'playwright-daemon',
diff --git a/packages/playwright/src/mcp/terminal/cli.ts b/packages/playwright/src/mcp/terminal/cli.ts
index de388fffc3..db52e2b52b 100644
--- a/packages/playwright/src/mcp/terminal/cli.ts
+++ b/packages/playwright/src/mcp/terminal/cli.ts
@@ -55,7 +55,8 @@ addCommand('click <ref>', 'click an element using a ref from a snapshot, e.g. e6
});
addCommand('snapshot', 'get accessible snapshot of the current page', async () => {
- await runMcpCommand('browser_snapshot', {});
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
+ await runMcpCommand('browser_snapshot', { filename: `snapshot-${timestamp}.md` });
});
addCommand('drag <startRef> <endRef>', 'drag from one element to another', async (startRef, endRef) => {
diff --git a/tests/mcp/secrets.spec.ts b/tests/mcp/secrets.spec.ts
index e207834a15..bfd806a91e 100644
--- a/tests/mcp/secrets.spec.ts
+++ b/tests/mcp/secrets.spec.ts
@@ -170,7 +170,7 @@ await page.getByRole('textbox').press('Enter');` },
{ category: 'console', content: expect.stringContaining('[LOG] Key pressed: Enter , Text: <secret>X-PASSWORD</secret>') },
],
'dev.lowire/state': {
- 'page': expect.stringMatching(/textbox (\[active\] )?\[ref=e2\]: <secret>X-PASSWORD<\/secret>/),
+ page: expect.stringMatching(/textbox (\[active\] )?\[ref=e2\]: <secret>X-PASSWORD<\/secret>/),
},
});
}
@@ -187,7 +187,9 @@ await page.getByRole('textbox').press('Enter');` },
'dev.lowire/history': [
{ category: 'result', content: expect.stringContaining('[LOG] Key pressed: Enter , Text: <secret>X-PASSWORD</secret>') },
],
- 'dev.lowire/state': {},
+ 'dev.lowire/state': {
+ page: expect.any(String),
+ },
});
}
});
diff --git a/tests/mcp/snapshot-mode.spec.ts b/tests/mcp/snapshot-mode.spec.ts
index a25046bc2b..f2539d7e44 100644
--- a/tests/mcp/snapshot-mode.spec.ts
+++ b/tests/mcp/snapshot-mode.spec.ts
@@ -98,7 +98,8 @@ test('should respect --snapshot-mode=none', async ({ startClient, server }) => {
url: server.PREFIX,
},
})).toHaveResponse({
- pageState: undefined
+ pageState: `- Page URL: ${server.PREFIX}/
+- Page Title:`
});
});