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 relative paths when changing cwd (#39024)

Pavel Feldmana9b17c0da6ee
diff --git a/packages/playwright/src/mcp/browser/browserServerBackend.ts b/packages/playwright/src/mcp/browser/browserServerBackend.ts
index e37d46f616..40e19d9413 100644
--- a/packages/playwright/src/mcp/browser/browserServerBackend.ts
+++ b/packages/playwright/src/mcp/browser/browserServerBackend.ts
@@ -17,7 +17,7 @@
import { FullConfig } from './config';
import { Context } from './context';
import { logUnhandledError } from '../log';
-import { Response, serializeResponse, serializeStructuredResponse } from './response';
+import { Response, serializeResponse } from './response';
import { SessionLog } from './sessionLog';
import { browserTools, filteredTools } from './tools';
import { toMcpTool } from '../sdk/tool';
@@ -33,7 +33,6 @@ export class BrowserServerBackend implements ServerBackend {
private _sessionLog: SessionLog | undefined;
private _config: FullConfig;
private _browserContextFactory: BrowserContextFactory;
- private _isStructuredOutput: boolean;
onBrowserContextClosed: (() => void) | undefined;
@@ -41,7 +40,6 @@ export class BrowserServerBackend implements ServerBackend {
this._config = config;
this._browserContextFactory = factory;
this._tools = options.allTools ? browserTools : filteredTools(config);
- this._isStructuredOutput = options.structuredOutput ?? false;
}
async initialize(clientInfo: mcpServer.ClientInfo): Promise<void> {
@@ -68,6 +66,7 @@ export class BrowserServerBackend implements ServerBackend {
};
}
const parsedArguments = tool.schema.inputSchema.parse(rawArguments || {}) as any;
+ const cwd = rawArguments?._meta && typeof rawArguments?._meta === 'object' && (rawArguments._meta as any)?.cwd;
const context = this._context!;
const response = Response.create(context, name, parsedArguments);
context.setRunningTool(name);
@@ -75,10 +74,7 @@ export class BrowserServerBackend implements ServerBackend {
try {
await tool.handle(context, parsedArguments, response);
const sections = await response.build();
- if (this._isStructuredOutput)
- responseObject = await serializeStructuredResponse(sections);
- else
- responseObject = await serializeResponse(context, sections, context.firstRootPath());
+ responseObject = await serializeResponse(context, sections, cwd ?? context.firstRootPath());
this._sessionLog?.logResponse(name, parsedArguments, responseObject);
} catch (error: any) {
return {
diff --git a/packages/playwright/src/mcp/browser/response.ts b/packages/playwright/src/mcp/browser/response.ts
index dad16c6d9e..80bdf8ba2a 100644
--- a/packages/playwright/src/mcp/browser/response.ts
+++ b/packages/playwright/src/mcp/browser/response.ts
@@ -33,7 +33,6 @@ type Result = {
data?: Buffer;
isBase64?: boolean;
title: string;
- codeframe?: 'yaml' | 'js';
file?: {
prefix: string;
ext: string;
@@ -46,6 +45,7 @@ export type Section = {
title: string;
content: Result[];
isError?: boolean;
+ codeframe?: 'yaml' | 'js';
};
export class Response {
@@ -104,8 +104,8 @@ export class Response {
async build(): Promise<Section[]> {
const rootPath = this._context.firstRootPath();
const sections: Section[] = [];
- const addSection = (title: string) => {
- const section = { title, content: [] as Result[], isError: title === 'Error' };
+ const addSection = (title: string, codeframe?: 'yaml' | 'js') => {
+ const section = { title, content: [] as Result[], isError: title === 'Error', codeframe };
sections.push(section);
return section.content;
};
@@ -123,9 +123,9 @@ export class Response {
// Code
if (this._context.config.codegen !== 'none' && this._code.length) {
- const content = addSection('Ran Playwright code');
+ const content = addSection('Ran Playwright code', 'js');
for (const code of this._code)
- content.push({ text: code, title: 'code', codeframe: 'js' });
+ content.push({ text: code, title: 'code' });
}
// Render tab titles upon changes or when more than one tab.
@@ -149,9 +149,9 @@ export class Response {
// Handle tab snapshot
if (tabSnapshot && this._includeSnapshot !== 'none') {
- const content = addSection('Snapshot');
+ const content = addSection('Snapshot', 'yaml');
const snapshot = this._includeSnapshot === 'full' ? tabSnapshot.ariaSnapshot : tabSnapshot.ariaSnapshotDiff ?? tabSnapshot.ariaSnapshot;
- content.push({ text: snapshot, codeframe: 'yaml', title: 'snapshot', file: { prefix: 'page', ext: 'yml', suggestedFilename: this._includeSnapshotFileName } });
+ content.push({ text: snapshot, title: 'snapshot', file: { prefix: 'page', ext: 'yml', suggestedFilename: this._includeSnapshotFileName } });
}
// Handle tab log
@@ -217,7 +217,7 @@ function parseSections(text: string): Map<string, string> {
return sections;
}
-export async function serializeResponse(context: Context, sections: Section[], rootPath?: string): Promise<CallToolResult> {
+export async function serializeResponse(context: Context, sections: Section[], relativeTo?: string): Promise<CallToolResult> {
const redactText = (text: string): string => {
for (const [secretName, secretValue] of Object.entries(context.config.secrets ?? {}))
text = text.replaceAll(secretValue, `<secret>${secretName}</secret>`);
@@ -227,29 +227,30 @@ export async function serializeResponse(context: Context, sections: Section[], r
const text: string[] = [];
for (const section of sections) {
text.push(`### ${section.title}`);
+ const codeframe: string[] = [];
for (const result of section.content) {
- if (!result.file) {
- if (result.text !== undefined)
- text.push(result.text);
- continue;
- }
-
- if (result.file.suggestedFilename || context.config.outputMode === 'file' || result.data) {
+ if (result.file && (result.file.suggestedFilename || context.config.outputMode === 'file' || result.data)) {
const generatedFileName = await context.outputFile(dateAsFileName(result.file.prefix, result.file.ext), { origin: 'code', title: section.title });
const fileName = result.file.suggestedFilename ? await context.outputFile(result.file.suggestedFilename, { origin: 'llm', title: section.title }) : generatedFileName;
- text.push(`- [${result.title}](${rootPath ? path.relative(rootPath, fileName) : fileName})`);
+ text.push(`- [${result.title}](${relativeTo ? path.relative(relativeTo, fileName) : fileName})`);
if (result.data)
await fs.promises.writeFile(fileName, result.data, 'utf-8');
else
await fs.promises.writeFile(fileName, result.text!);
} else {
- if (result.codeframe)
- text.push(`\`\`\`${result.codeframe}\n${result.text!}\n\`\`\``);
- else
- text.push(result.text!);
+ if (result.text !== undefined)
+ codeframe.push(result.text);
}
}
+
+ if (codeframe?.length) {
+ if (section.codeframe)
+ text.push(`\`\`\`${section.codeframe}\n${codeframe.join('\n')}\n\`\`\``);
+ else
+ text.push(codeframe.join('\n'));
+ }
}
+
const content: (TextContent | ImageContent)[] = [
{
type: 'text',
@@ -271,22 +272,6 @@ export async function serializeResponse(context: Context, sections: Section[], r
};
}
-export async function serializeStructuredResponse(sections: Section[]): Promise<CallToolResult> {
- for (const section of sections) {
- for (const result of section.content) {
- if (!result.data)
- continue;
- result.isBase64 = true;
- result.text = result.data.toString('base64');
- result.data = undefined;
- }
- }
- return {
- content: [{ type: 'text' as const, text: '', _meta: { sections } }],
- isError: sections.some(section => section.isError),
- };
-}
-
export function parseResponse(response: CallToolResult) {
if (response.content?.[0].type !== 'text')
return undefined;
diff --git a/packages/playwright/src/mcp/program.ts b/packages/playwright/src/mcp/program.ts
index 24f6e59f88..c5c34400a7 100644
--- a/packages/playwright/src/mcp/program.ts
+++ b/packages/playwright/src/mcp/program.ts
@@ -116,7 +116,7 @@ export function decorateCommand(command: Command, version: string) {
name: 'Playwright',
nameInConfig: 'playwright-daemon',
version,
- create: () => new BrowserServerBackend(config, contextFactory, { allTools: true, structuredOutput: true })
+ create: () => new BrowserServerBackend(config, contextFactory, { allTools: true })
};
const socketPath = await startMcpDaemonServer(options.daemon, serverBackendFactory, options.daemonVersion);
console.error(`Daemon server listening on ${socketPath}`);
diff --git a/packages/playwright/src/mcp/terminal/daemon.ts b/packages/playwright/src/mcp/terminal/daemon.ts
index 7dd871dedf..bf39d9b126 100644
--- a/packages/playwright/src/mcp/terminal/daemon.ts
+++ b/packages/playwright/src/mcp/terminal/daemon.ts
@@ -29,8 +29,6 @@ import { parseCommand } from './command';
import type { ServerBackendFactory } from '../sdk/server';
import type * as mcp from '../sdk/exports';
-import type { StructuredResponse } from './program';
-import type { Section } from '../browser/response';
const daemonDebug = debug('pw:daemon');
@@ -111,6 +109,8 @@ export async function startMcpDaemonServer(
});
} else if (method === 'run') {
const { toolName, toolParams } = parseCliCommand(params.args);
+ if (params.cwd)
+ toolParams._meta = { cwd: params.cwd };
const response = await backend.callTool(toolName, toolParams, () => {});
await connection.send({ id, result: formatResult(response) });
} else {
@@ -142,14 +142,13 @@ export async function startMcpDaemonServer(
});
}
-function formatResult(result: mcp.CallToolResult): StructuredResponse {
+function formatResult(result: mcp.CallToolResult) {
const isError = result.isError;
const text = result.content[0].type === 'text' ? result.content[0].text : undefined;
- const sections = result.content[0]._meta?.sections as Section[];
- return { isError, text, sections };
+ return { isError, text };
}
-function parseCliCommand(args: Record<string, string> & { _: string[] }): { toolName: string, toolParams: mcp.CallToolRequest['params']['arguments'] } {
+function parseCliCommand(args: Record<string, string> & { _: string[] }): { toolName: string, toolParams: NonNullable<mcp.CallToolRequest['params']['arguments']> } {
const command = commands[args._[0]];
if (!command)
throw new Error('Command is required');
diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.ts
index 464a3bdb03..97c19a380f 100644
--- a/packages/playwright/src/mcp/terminal/program.ts
+++ b/packages/playwright/src/mcp/terminal/program.ts
@@ -51,7 +51,7 @@ class Session {
}
async run(args: any) {
- return await this._send('run', { args });
+ return await this._send('run', { args, cwd: process.cwd() });
}
async stop(): Promise<void> {
@@ -308,7 +308,7 @@ class SessionManager {
}
const result = await session.run({ ...args, outputDir });
- await printResponse(result);
+ console.log(result.text);
session.close();
}
@@ -469,76 +469,8 @@ export async function program(options: { version: string }) {
await sessionManager.run(args);
}
-export async function printResponse(response: StructuredResponse) {
- const { sections } = response;
- if (!sections) {
- console.log(response.text);
- return;
- }
-
- const text: string[] = [];
- for (const section of sections) {
- text.push(`### ${section.title}`);
- for (const result of section.content) {
- if (!result.file) {
- if (result.text !== undefined) {
- if (result.codeframe)
- text.push(`\`\`\`${result.codeframe}\n${result.text}\n\`\`\``);
- else
- text.push(result.text);
- }
- continue;
- }
-
- const generatedFileName = await outputFile(dateAsFileName(result.file.prefix, result.file.ext), { origin: 'code' });
- const fileName = result.file.suggestedFilename ? await outputFile(result.file.suggestedFilename, { origin: 'llm' }) : generatedFileName;
- text.push(`- [${result.title}](${path.relative(process.cwd(), fileName)})`);
- if (result.data)
- await fs.promises.writeFile(fileName, result.data);
- else if (result.isBase64)
- await fs.promises.writeFile(fileName, Buffer.from(result.text!, 'base64'));
- else
- await fs.promises.writeFile(fileName, result.text!);
- }
- }
- console.log(text.join('\n'));
-}
-
-function dateAsFileName(prefix: string, extension: string): string {
- const date = new Date();
- return `${prefix}-${date.toISOString().replace(/[:.]/g, '-')}.${extension}`;
-}
-
const outputDir = path.join(process.cwd(), '.playwright-cli');
-async function outputFile(fileName: string, options: { origin: 'code' | 'llm' | 'web' }): Promise<string> {
- await fs.promises.mkdir(outputDir, { recursive: true });
-
- // Trust code.
- if (options.origin === 'code')
- return path.resolve(outputDir, fileName);
-
- // Trust llm to use valid characters in file names.
- if (options.origin === 'llm') {
- fileName = fileName.split('\\').join('/');
- const resolvedFile = path.resolve(outputDir, fileName);
- if (!resolvedFile.startsWith(path.resolve(outputDir) + path.sep))
- throw new Error(`Resolved file path ${resolvedFile} is outside of the output directory ${outputDir}. Use relative file names to stay within the output directory.`);
- return resolvedFile;
- }
-
- // Do not trust web, at all.
- return path.join(outputDir, sanitizeForFilePath(fileName));
-}
-
-function sanitizeForFilePath(s: string) {
- const sanitize = (s: string) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
- const separator = s.lastIndexOf('.');
- if (separator === -1)
- return sanitize(s);
- return sanitize(s.substring(0, separator)) + '.' + sanitize(s.substring(separator + 1));
-}
-
function resolveConfigFile(configParam: string | undefined) {
const configFile = configParam || process.env.PLAYWRIGHT_CLI_CONFIG;
if (configFile)
diff --git a/tests/mcp/cli-fixtures.ts b/tests/mcp/cli-fixtures.ts
index d5367f9edd..9dc73db3c0 100644
--- a/tests/mcp/cli-fixtures.ts
+++ b/tests/mcp/cli-fixtures.ts
@@ -24,7 +24,7 @@ import type { CommonFixtures } from '../config/commonFixtures';
export { expect } from './fixtures';
export const test = baseTest.extend<{
- cli: (...args: string[]) => Promise<{
+ cli: (...args: any[]) => Promise<{
output: string,
error: string,
exitCode: number | undefined,
@@ -36,11 +36,13 @@ export const test = baseTest.extend<{
const sessions: { name: string, pid: number }[] = [];
await use(async (...args: string[]) => {
- return await runCli(childProcess, args, { mcpBrowser, mcpHeadless }, sessions);
+ const cliArgs = args.filter(arg => typeof arg === 'string');
+ const cliOptions = args.findLast(arg => typeof arg === 'object') || {};
+ return await runCli(childProcess, cliArgs, cliOptions, { mcpBrowser, mcpHeadless }, sessions);
});
for (const session of sessions) {
- await runCli(childProcess, ['session-stop', session.name], { mcpBrowser, mcpHeadless }, []).catch(e => {
+ await runCli(childProcess, ['session-stop', session.name], {}, { mcpBrowser, mcpHeadless }, []).catch(e => {
if (!e.message.includes('is not running'))
throw e;
});
@@ -54,13 +56,13 @@ export const test = baseTest.extend<{
},
});
-async function runCli(childProcess: CommonFixtures['childProcess'], args: string[], options: { mcpBrowser: string, mcpHeadless: boolean }, sessions: { name: string, pid: number }[]) {
+async function runCli(childProcess: CommonFixtures['childProcess'], args: string[], cliOptions: { cwd?: string }, options: { mcpBrowser: string, mcpHeadless: boolean }, sessions: { name: string, pid: number }[]) {
const stepTitle = `cli ${args.join(' ')}`;
return await test.step(stepTitle, async () => {
const testInfo = test.info();
const cli = childProcess({
command: [process.execPath, require.resolve('../../packages/playwright/lib/mcp/terminal/cli.js'), ...args],
- cwd: testInfo.outputPath(),
+ cwd: cliOptions.cwd ?? testInfo.outputPath(),
env: {
...process.env,
PLAYWRIGHT_DAEMON_INSTALL_DIR: testInfo.outputPath(),
@@ -116,7 +118,11 @@ async function loadSnapshot(output: string) {
throw new Error('Snapshot file not found');
const fileLine = lines[lines.indexOf('### Snapshot') + 1];
const fileName = fileLine.match(/- \[(.+)\]\((.+)\)/)![2];
- return await fs.promises.readFile(test.info().outputPath(fileName), 'utf8');
+ try {
+ return await fs.promises.readFile(test.info().outputPath(fileName), 'utf8').catch(() => undefined);
+ } catch (e) {
+ return '';
+ }
}
export const eventsPage = `<!DOCTYPE html>
diff --git a/tests/mcp/cli.spec.ts b/tests/mcp/cli.spec.ts
index 53cd4246f7..4941b540c3 100644
--- a/tests/mcp/cli.spec.ts
+++ b/tests/mcp/cli.spec.ts
@@ -583,3 +583,18 @@ test.describe('versions', () => {
}
});
});
+
+test.describe('folders', () => {
+ test('snapshot', async ({ cli, server }, testInfo) => {
+ {
+ const { output } = await cli('open', server.HELLO_WORLD);
+ expect(output).toContain('.playwright-cli' + path.sep + 'page-');
+ }
+ {
+ const nested = testInfo.outputPath('nested');
+ await fs.promises.mkdir(nested, { recursive: true });
+ const { output } = await cli('open', server.HELLO_WORLD, { cwd: nested });
+ expect(output).toContain('..' + path.sep + '.playwright-cli' + path.sep + 'page-');
+ }
+ });
+});