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): report session params mismatch (#39098)

Pavel Feldmanef0c7b3b3b64
diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.ts
index a759a03b3e..0e4b051f07 100644
--- a/packages/playwright/src/mcp/terminal/program.ts
+++ b/packages/playwright/src/mcp/terminal/program.ts
@@ -246,13 +246,18 @@ to restart the session daemon.`);
});
child.unref();
- console.log(`<!-- Daemon for \`${this.name}\` session started with pid ${child.pid}.`);
- if (this._config.cli.config)
- console.log(`- Using config file at \`${path.relative(process.cwd(), this._config.cli.config)}\`.`);
+ console.log(`### Daemon for \`${this.name}\` session started with pid ${child.pid}.`);
+ const configArgs = configToFormattedArgs(this._config.cli);
+ if (configArgs.length) {
+ console.log(`- Session options:`);
+ for (const flag of configArgs)
+ console.log(` ${flag}`);
+ }
const sessionSuffix = this.name !== 'default' ? ` "${this.name}"` : '';
- 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.`);
- console.log('-->');
+ const sessionOption = this.name !== 'default' ? ` --session="${this.name}"` : '';
+ console.log(`- playwright-cli session-stop${sessionSuffix} # to stop when done.`);
+ console.log(`- playwright-cli${sessionOption} config [options] # to change session options.`);
+ console.log(`- playwright-cli session-delete${sessionSuffix} # to delete session data.`);
// Wait for the socket to become available with retries.
const maxRetries = 50;
@@ -327,9 +332,18 @@ class SessionManager {
if (!session) {
session = new Session(this.clientInfo, sessionName, sessionConfigFromArgs(this.clientInfo, sessionName, args));
this.sessions.set(sessionName, session);
+ } else {
+ if (hasGlobalArgs(args)) {
+ const configFromArgs = sessionConfigFromArgs(this.clientInfo, sessionName, args);
+ const formattedArgs = configToFormattedArgs(configFromArgs.cli);
+ console.log('The session is already configured. To change session options, run:');
+ console.log('');
+ console.log(` playwright-cli${sessionName !== 'default' ? ` --session=${sessionName}` : ''} config ${formattedArgs.join(' ')}`);
+ process.exit(1);
+ }
}
- for (const globalOption of ['browser', 'config', 'extension', 'headed', 'help', 'isolated', 'session', 'version'])
+ for (const globalOption of globalArgs)
delete args[globalOption];
const result = await session.run(args);
console.log(result.text);
@@ -577,3 +591,25 @@ function sessionConfigFromArgs(clientInfo: ClientInfo, sessionName: string, args
userDataDirPrefix: path.resolve(clientInfo.daemonProfilesDir, `ud-${sessionName}`),
};
}
+
+const globalArgs = ['browser', 'config', 'extension', 'headed', 'help', 'in-memory', 'version', 'session'];
+
+function hasGlobalArgs(args: MinimistArgs): boolean {
+ return globalArgs.some(option => args[option] !== undefined);
+}
+
+function configToFormattedArgs(config: SessionConfig['cli']): string[] {
+ const args: string[] = [];
+ const add = (flag: string, value: string | boolean | undefined) => {
+ if (typeof value === 'boolean' && value)
+ args.push(`--${flag}`);
+ else if (typeof value === 'string')
+ args.push(`--${flag}=${value}`);
+ };
+ add('browser', config.browser);
+ add('config', config.config ? path.relative(process.cwd(), config.config) : undefined);
+ add('extension', config.extension);
+ add('headed', config.headed);
+ add('in-memory', config.isolated);
+ return args;
+}
diff --git a/tests/mcp/cli-session.spec.ts b/tests/mcp/cli-session.spec.ts
index dbe8c07ab7..76b895f25a 100644
--- a/tests/mcp/cli-session.spec.ts
+++ b/tests/mcp/cli-session.spec.ts
@@ -140,9 +140,41 @@ test('config should work', async ({ cli, server }, testInfo) => {
await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2));
const { output: configureOutput } = await cli('config', '--config=' + configPath);
- expect(configureOutput).toContain(`- Using config file at \`session-config.json\`.`);
+ expect(configureOutput).toContain(`--config=`);
await cli('open', server.PREFIX);
const { output: afterOutput } = await cli('eval', 'window.innerWidth + "x" + window.innerHeight');
expect(afterOutput).toContain('700x500');
});
+
+test('session start should print session options', async ({ cli, server }, testInfo) => {
+ const configPath = testInfo.outputPath('my-config.json');
+ await fs.promises.writeFile(configPath, JSON.stringify({}, null, 2));
+
+ const { output } = await cli('open', '--headed', '--config=' + configPath, server.HELLO_WORLD);
+ expect(output).toContain('Session options:');
+ expect(output).toContain('--headed');
+ expect(output).toContain('--config=my-config.json');
+});
+
+test('session mismatch should report error for default session', async ({ cli, server }) => {
+ // Start a default session
+ await cli('open', server.HELLO_WORLD);
+
+ // Try to pass global options to the already running session
+ const { output, exitCode } = await cli('snapshot', '--headed');
+ expect(exitCode).toBe(1);
+ expect(output).toContain('The session is already configured.');
+ expect(output).toContain('playwright-cli config --headed');
+});
+
+test('session mismatch should report error for named session', async ({ cli, server }) => {
+ // Start a named session
+ await cli('open', '--session=mismatch-named', server.HELLO_WORLD);
+
+ // Try to pass global options to the already running session
+ const { output, exitCode } = await cli('snapshot', '--session=mismatch-named', '--headed');
+ expect(exitCode).toBe(1);
+ expect(output).toContain('The session is already configured.');
+ expect(output).toContain('playwright-cli --session=mismatch-named config --headed');
+});