Skip to content

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

chore(cli): print generated code (#39022)

Pavel Feldman2a13d9867245
diff --git a/packages/playwright/src/mcp/browser/config.ts b/packages/playwright/src/mcp/browser/config.ts
index 3b1329b4e3..0eccd0d064 100644
--- a/packages/playwright/src/mcp/browser/config.ts
+++ b/packages/playwright/src/mcp/browser/config.ts
@@ -118,7 +118,6 @@ const defaultDaemonConfig = (cliOptions: CLIOptions) => mergeConfig(defaultConfi
},
},
outputMode: 'file',
- codegen: 'none',
snapshot: {
mode: 'full',
},
diff --git a/packages/playwright/src/mcp/browser/response.ts b/packages/playwright/src/mcp/browser/response.ts
index 3ec8263850..dad16c6d9e 100644
--- a/packages/playwright/src/mcp/browser/response.ts
+++ b/packages/playwright/src/mcp/browser/response.ts
@@ -33,6 +33,7 @@ type Result = {
data?: Buffer;
isBase64?: boolean;
title: string;
+ codeframe?: 'yaml' | 'js';
file?: {
prefix: string;
ext: string;
@@ -124,7 +125,7 @@ export class Response {
if (this._context.config.codegen !== 'none' && this._code.length) {
const content = addSection('Ran Playwright code');
for (const code of this._code)
- content.push({ text: code, title: 'code' });
+ content.push({ text: code, title: 'code', codeframe: 'js' });
}
// Render tab titles upon changes or when more than one tab.
@@ -150,7 +151,7 @@ export class Response {
if (tabSnapshot && this._includeSnapshot !== 'none') {
const content = addSection('Snapshot');
const snapshot = this._includeSnapshot === 'full' ? tabSnapshot.ariaSnapshot : tabSnapshot.ariaSnapshotDiff ?? tabSnapshot.ariaSnapshot;
- content.push({ text: snapshot, title: 'snapshot', file: { prefix: 'page', ext: 'yml', suggestedFilename: this._includeSnapshotFileName } });
+ content.push({ text: snapshot, codeframe: 'yaml', title: 'snapshot', file: { prefix: 'page', ext: 'yml', suggestedFilename: this._includeSnapshotFileName } });
}
// Handle tab log
@@ -242,8 +243,8 @@ export async function serializeResponse(context: Context, sections: Section[], r
else
await fs.promises.writeFile(fileName, result.text!);
} else {
- if (result.file.ext === 'yml')
- text.push(`\`\`\`yaml\n${result.text!}\n\`\`\``);
+ if (result.codeframe)
+ text.push(`\`\`\`${result.codeframe}\n${result.text!}\n\`\`\``);
else
text.push(result.text!);
}
diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.ts
index db033f67f0..464a3bdb03 100644
--- a/packages/playwright/src/mcp/terminal/program.ts
+++ b/packages/playwright/src/mcp/terminal/program.ts
@@ -481,8 +481,12 @@ export async function printResponse(response: StructuredResponse) {
text.push(`### ${section.title}`);
for (const result of section.content) {
if (!result.file) {
- if (result.text !== undefined)
- text.push(result.text);
+ if (result.text !== undefined) {
+ if (result.codeframe)
+ text.push(`\`\`\`${result.codeframe}\n${result.text}\n\`\`\``);
+ else
+ text.push(result.text);
+ }
continue;
}
diff --git a/tests/mcp/cli.spec.ts b/tests/mcp/cli.spec.ts
index 12bb983a90..53cd4246f7 100644
--- a/tests/mcp/cli.spec.ts
+++ b/tests/mcp/cli.spec.ts
@@ -52,8 +52,12 @@ test.describe('core', () => {
const { snapshot } = await cli('open', server.PREFIX);
expect(snapshot).toContain(`- button "Submit" [ref=e2]`);
- const { snapshot: clickSnapshot } = await cli('click', 'e2');
+ const { output, snapshot: clickSnapshot } = await cli('click', 'e2');
expect(clickSnapshot).toBeTruthy();
+ expect(output).toContain(`### Ran Playwright code
+\`\`\`js
+await page.getByRole('button', { name: 'Submit' }).click();
+\`\`\``);
});
test('click link', async ({ cli, server }) => {