Skip to content

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

chore: add playwright-cli config (#38971)

Pavel Feldmanfe5b234f4e2a
diff --git a/packages/playwright/src/mcp/browser/config.ts b/packages/playwright/src/mcp/browser/config.ts
index 98dde261b4..1de7b618a7 100644
--- a/packages/playwright/src/mcp/browser/config.ts
+++ b/packages/playwright/src/mcp/browser/config.ts
@@ -112,7 +112,10 @@ const defaultDaemonConfig = (cliOptions: CLIOptions) => mergeConfig(defaultConfi
userDataDir: '<daemon-data-dir>',
launchOptions: {
headless: !cliOptions.daemonHeaded,
- }
+ },
+ contextOptions: {
+ viewport: cliOptions.daemonHeaded ? null : { width: 1280, height: 720 },
+ },
},
outputMode: 'file',
codegen: 'none',
diff --git a/packages/playwright/src/mcp/terminal/SKILL.md b/packages/playwright/src/mcp/terminal/SKILL.md
index b3f2eed795..72db230f5b 100644
--- a/packages/playwright/src/mcp/terminal/SKILL.md
+++ b/packages/playwright/src/mcp/terminal/SKILL.md
@@ -105,6 +105,16 @@ playwright-cli tracing-start
playwright-cli tracing-stop
```
+### Configuration
+```bash
+# configure the session
+playwright-cli config my-config.json
+# Configure named session
+playwright-cli --session=mysession config my-config.json
+# Start with configured session
+playwright-cli open --config=my-config.json
+```
+
### Sessions
```bash
diff --git a/packages/playwright/src/mcp/terminal/command.ts b/packages/playwright/src/mcp/terminal/command.ts
index b0e44c0b72..5bba0ba615 100644
--- a/packages/playwright/src/mcp/terminal/command.ts
+++ b/packages/playwright/src/mcp/terminal/command.ts
@@ -17,7 +17,7 @@
import type zodType from 'zod';
-export type Category = 'core' | 'navigation' | 'keyboard' | 'mouse' | 'export' | 'storage' | 'tabs' | 'devtools' | 'session';
+export type Category = 'core' | 'navigation' | 'keyboard' | 'mouse' | 'export' | 'storage' | 'tabs' | 'devtools' | 'session' | 'config';
export type CommandSchema<Args extends zodType.ZodTypeAny, Options extends zodType.ZodTypeAny> = {
name: string;
diff --git a/packages/playwright/src/mcp/terminal/commands.ts b/packages/playwright/src/mcp/terminal/commands.ts
index eab4a6fe07..f6a89064de 100644
--- a/packages/playwright/src/mcp/terminal/commands.ts
+++ b/packages/playwright/src/mcp/terminal/commands.ts
@@ -512,6 +512,17 @@ const sessionDelete = declareCommand({
toolParams: ({ name }) => ({ name }),
});
+const config = declareCommand({
+ name: 'config',
+ description: 'Restart session with new config, defaults to `playwright-cli.json`',
+ category: 'config',
+ args: z.object({
+ config: z.string().optional().describe('Path to the configuration file'),
+ }),
+ toolName: '',
+ toolParams: () => ({}),
+});
+
const commandsArray: AnyCommandSchema[] = [
// core category
open,
@@ -559,6 +570,9 @@ const commandsArray: AnyCommandSchema[] = [
tabClose,
tabSelect,
+ // config
+ config,
+
// devtools category
networkRequests,
runCode,
diff --git a/packages/playwright/src/mcp/terminal/helpGenerator.ts b/packages/playwright/src/mcp/terminal/helpGenerator.ts
index 82829029e4..787b0d340e 100644
--- a/packages/playwright/src/mcp/terminal/helpGenerator.ts
+++ b/packages/playwright/src/mcp/terminal/helpGenerator.ts
@@ -72,6 +72,7 @@ const categories: { name: Category, title: string }[] = [
{ name: 'tabs', title: 'Tabs' },
{ name: 'storage', title: 'Storage' },
{ name: 'devtools', title: 'DevTools' },
+ { name: 'config', title: 'Configuration' },
{ name: 'session', title: 'Sessions' },
] as const;
@@ -95,8 +96,8 @@ export function generateHelp() {
}
lines.push('\nGlobal options:');
- lines.push(formatWithGap(' --config <path>', 'use custom configuration file, defaults to "playwright-cli.json"'));
- lines.push(formatWithGap(' --headed', 'run in headed mode'));
+ lines.push(formatWithGap(' --config <path>', 'create a session with custom config, defaults to `playwright-cli.json`'));
+ lines.push(formatWithGap(' --headed', 'create a headed session'));
lines.push(formatWithGap(' --help [command]', 'print help'));
lines.push(formatWithGap(' --session', 'run command in the scope of a specific session'));
lines.push(formatWithGap(' --version', 'print version'));
diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.ts
index 9552ee4088..3b6c0347f0 100644
--- a/packages/playwright/src/mcp/terminal/program.ts
+++ b/packages/playwright/src/mcp/terminal/program.ts
@@ -104,22 +104,22 @@ class SessionManager {
this._options = options;
}
- async list(): Promise<{ name: string, live: boolean }[]> {
+ async list(): Promise<Map<string, boolean>> {
const dir = daemonProfilesDir;
try {
const files = await fs.promises.readdir(dir);
- const sessions: { name: string, live: boolean }[] = [];
+ const sessions = new Map<string, boolean>();
for (const file of files) {
if (file.startsWith('ud-')) {
// Session is like ud-<sessionName>-browserName
const sessionName = file.split('-')[1];
const live = await this._canConnect(sessionName);
- sessions.push({ name: sessionName, live });
+ sessions.set(sessionName, live);
}
}
return sessions;
} catch {
- return [];
+ return new Map<string, boolean>();
}
}
@@ -186,6 +186,19 @@ class SessionManager {
}
}
+ async configure(args: any): Promise<void> {
+ const sessionName = this._resolveSessionName(args.session);
+
+ if (await this._canConnect(sessionName)) {
+ const session = await this._connect(sessionName);
+ await session.stop();
+ }
+
+ this._options.config = args._[1];
+ const session = await this._connect(sessionName);
+ session.close();
+ }
+
private async _connect(sessionName: string): Promise<Session> {
const socketPath = this._daemonSocketPath(sessionName);
debugCli(`Connecting to daemon at ${socketPath}`);
@@ -230,7 +243,7 @@ class SessionManager {
console.log(`<!-- Daemon for \`${sessionName}\` session started with pid ${child.pid}.`);
if (configFile)
- console.log(`- Using config file at \`${configFile}\`.`);
+ console.log(`- Using config file at \`${path.relative(process.cwd(), configFile)}\`.`);
const sessionSuffix = sessionName !== 'default' ? ` "${sessionName}"` : '';
console.log(`- You can stop the session daemon with \`playwright-cli session-stop${sessionSuffix}\` when done.`);
console.log(`- You can delete the session data with \`playwright-cli session-delete${sessionSuffix}\` when done.`);
@@ -302,17 +315,15 @@ class SessionManager {
}
}
-async function handleSessionCommand(sessionManager: SessionManager, args: any): Promise<void> {
- const subcommand = args._[0].split('-').slice(1).join('-');
-
+async function handleSessionCommand(sessionManager: SessionManager, subcommand: string, args: any): Promise<void> {
if (subcommand === 'list') {
const sessions = await sessionManager.list();
console.log('Sessions:');
- for (const session of sessions) {
- const liveMarker = session.live ? ' (live)' : '';
- console.log(` ${session.name}${liveMarker}`);
+ for (const [sessionName, live] of sessions.entries()) {
+ const liveMarker = live ? ' (live)' : '';
+ console.log(` ${sessionName}${liveMarker}`);
}
- if (sessions.length === 0)
+ if (sessions.size === 0)
console.log(' (no sessions)');
return;
}
@@ -324,8 +335,8 @@ async function handleSessionCommand(sessionManager: SessionManager, args: any):
if (subcommand === 'stop-all') {
const sessions = await sessionManager.list();
- for (const session of sessions)
- await sessionManager.stop(session.name);
+ for (const sessionName of sessions.keys())
+ await sessionManager.stop(sessionName);
return;
}
@@ -334,6 +345,11 @@ async function handleSessionCommand(sessionManager: SessionManager, args: any):
return;
}
+ if (subcommand === 'config') {
+ await sessionManager.configure(args);
+ return;
+ }
+
console.error(`Unknown session subcommand: ${subcommand}`);
process.exit(1);
}
@@ -394,7 +410,13 @@ export async function program(options: { version: string }) {
const sessionManager = new SessionManager(args);
if (commandName.startsWith('session')) {
- await handleSessionCommand(sessionManager, args);
+ const subcommand = args._[0].split('-').slice(1).join('-');
+ await handleSessionCommand(sessionManager, subcommand, args);
+ return;
+ }
+
+ if (commandName === 'config') {
+ await handleSessionCommand(sessionManager, 'config', args);
return;
}
diff --git a/tests/mcp/cli.spec.ts b/tests/mcp/cli.spec.ts
index 7445a94ffc..1809054749 100644
--- a/tests/mcp/cli.spec.ts
+++ b/tests/mcp/cli.spec.ts
@@ -478,3 +478,29 @@ test.describe('session', () => {
expect(output).toContain(`No user data found for session 'nonexistent'.`);
});
});
+
+test.describe('config', () => {
+ test('should work', async ({ cli, server }, testInfo) => {
+ // Start a session with default config
+ await cli('open', server.PREFIX);
+ const { output: beforeOutput } = await cli('eval', 'window.innerWidth + "x" + window.innerHeight');
+ expect(beforeOutput).toContain('1280x720');
+
+ const config = {
+ browser: {
+ contextOptions: {
+ viewport: { width: 700, height: 500 },
+ },
+ },
+ };
+ const configPath = testInfo.outputPath('session-config.json');
+ await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2));
+
+ const { output: configureOutput } = await cli('config', configPath);
+ expect(configureOutput).toContain(`- Using config file at \`session-config.json\`.`);
+
+ await cli('open', server.PREFIX);
+ const { output: afterOutput } = await cli('eval', 'window.innerWidth + "x" + window.innerHeight');
+ expect(afterOutput).toContain('700x500');
+ });
+});