Skip to content

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

feat(cli): add 'config --show' command to print resolved config (#39117)

Yury Semikhatskyf065cae88be1
diff --git a/packages/playwright/src/mcp/browser/tools.ts b/packages/playwright/src/mcp/browser/tools.ts
index 701d646ea8..633b018761 100644
--- a/packages/playwright/src/mcp/browser/tools.ts
+++ b/packages/playwright/src/mcp/browser/tools.ts
@@ -15,6 +15,7 @@
*/
import common from './tools/common';
+import config from './tools/config';
import console from './tools/console';
import cookies from './tools/cookies';
import dialogs from './tools/dialogs';
@@ -44,6 +45,7 @@ import type { FullConfig } from './config';
export const browserTools: Tool<any>[] = [
...common,
+ ...config,
...console,
...cookies,
...dialogs,
diff --git a/packages/playwright/src/mcp/browser/tools/config.ts b/packages/playwright/src/mcp/browser/tools/config.ts
new file mode 100644
index 0000000000..d586403889
--- /dev/null
+++ b/packages/playwright/src/mcp/browser/tools/config.ts
@@ -0,0 +1,38 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { z } from 'playwright-core/lib/mcpBundle';
+import { defineTool } from './tool';
+
+const configShow = defineTool({
+ capability: 'config',
+
+ schema: {
+ name: 'browser_get_config',
+ title: 'Get config',
+ description: 'Get the final resolved config after merging CLI options, environment variables and config file.',
+ inputSchema: z.object({}),
+ type: 'readOnly',
+ },
+
+ handle: async (context, params, response) => {
+ response.addTextResult(JSON.stringify(context.config, null, 2));
+ },
+});
+
+export default [
+ configShow,
+];
diff --git a/packages/playwright/src/mcp/config.d.ts b/packages/playwright/src/mcp/config.d.ts
index 0c49e5131b..89a55f1992 100644
--- a/packages/playwright/src/mcp/config.d.ts
+++ b/packages/playwright/src/mcp/config.d.ts
@@ -17,6 +17,7 @@
import type * as playwright from 'playwright-core';
export type ToolCapability =
+ 'config' |
'core' |
'core-navigation' |
'core-tabs' |
diff --git a/packages/playwright/src/mcp/terminal/command.ts b/packages/playwright/src/mcp/terminal/command.ts
index fb5712e4c8..5a07465395 100644
--- a/packages/playwright/src/mcp/terminal/command.ts
+++ b/packages/playwright/src/mcp/terminal/command.ts
@@ -24,6 +24,7 @@ export type CommandSchema<Args extends zodType.ZodTypeAny, Options extends zodTy
name: string;
category: Category;
description: string;
+ hidden?: boolean;
args?: Args;
options?: Options;
toolName: string | ((args: zodType.infer<Args> & zodType.infer<Options>) => string);
diff --git a/packages/playwright/src/mcp/terminal/commands.ts b/packages/playwright/src/mcp/terminal/commands.ts
index 5da23e7959..635709269d 100644
--- a/packages/playwright/src/mcp/terminal/commands.ts
+++ b/packages/playwright/src/mcp/terminal/commands.ts
@@ -791,13 +791,22 @@ const config = declareCommand({
options: z.object({
browser: z.string().optional().describe('Browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.'),
config: z.string().optional().describe('Path to the configuration file'),
- isolated: z.boolean().optional().describe('Keep the browser profile in memory, do not save it to disk.'),
headed: z.boolean().optional().describe('Run browser in headed mode'),
+ ['in-memory']: z.boolean().optional().describe('Keep the browser profile in memory, do not save it to disk.'),
}),
toolName: '',
toolParams: () => ({}),
});
+const configPrint = declareCommand({
+ name: 'config-print',
+ description: 'Print the final resolved config after merging CLI options, environment variables and config file.',
+ category: 'config',
+ hidden: true,
+ toolName: 'browser_get_config',
+ toolParams: () => ({}),
+});
+
const install = declareCommand({
name: 'install-browser',
description: 'Install browser',
@@ -892,6 +901,7 @@ const commandsArray: AnyCommandSchema[] = [
// config category
config,
+ configPrint,
// install category
install,
diff --git a/packages/playwright/src/mcp/terminal/helpGenerator.ts b/packages/playwright/src/mcp/terminal/helpGenerator.ts
index e70dc89350..53574cdcd4 100644
--- a/packages/playwright/src/mcp/terminal/helpGenerator.ts
+++ b/packages/playwright/src/mcp/terminal/helpGenerator.ts
@@ -85,8 +85,11 @@ export function generateHelp() {
const commandsByCategory = new Map<string, AnyCommandSchema[]>();
for (const c of categories)
commandsByCategory.set(c.name, []);
- for (const command of Object.values(commands))
+ for (const command of Object.values(commands)) {
+ if (command.hidden)
+ continue;
commandsByCategory.get(command.category)!.push(command);
+ }
for (const c of categories) {
const cc = commandsByCategory.get(c.name)!;
diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.ts
index 0e4b051f07..845caf8009 100644
--- a/packages/playwright/src/mcp/terminal/program.ts
+++ b/packages/playwright/src/mcp/terminal/program.ts
@@ -441,6 +441,10 @@ async function handleSessionCommand(sessionManager: SessionManager, subcommand:
}
if (subcommand === 'config') {
+ if (args.print) {
+ await sessionManager.run(args);
+ return;
+ }
await sessionManager.configure(args);
return;
}
@@ -487,6 +491,7 @@ const booleanOptions = [
'headed',
'help',
'in-memory',
+ 'print',
'version',
];
diff --git a/tests/mcp/cli-config.spec.ts b/tests/mcp/cli-config.spec.ts
index cfeac33b31..50f6ac0d31 100644
--- a/tests/mcp/cli-config.spec.ts
+++ b/tests/mcp/cli-config.spec.ts
@@ -59,6 +59,44 @@ test('headless options from config is respected', async ({ cli, server, mcpBrows
expect(output).toContain(' Chrome/');
});
+test('config-print prints merged config from file, env and cli', async ({ cli, server }, testInfo) => {
+ // Config file provides viewport and timeouts.
+ const fileConfig = {
+ browser: {
+ contextOptions: {
+ viewport: { width: 800, height: 600 },
+ },
+ },
+ timeouts: {
+ action: 10000,
+ navigation: 30000,
+ },
+ };
+ await fs.promises.writeFile(testInfo.outputPath('playwright-cli.json'), JSON.stringify(fileConfig, null, 2));
+
+ // Env var overrides navigation timeout (30000 from file → 45000 from env).
+ const env = { PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION: '45000' };
+
+ // CLI arg: --in-memory sets browser.isolated = true.
+ await cli('open', '--in-memory', server.PREFIX, { env });
+
+ // Query the resolved config from the running daemon.
+ const { output } = await cli('config-print', { env });
+ const configBegin = output.indexOf('{');
+ expect(configBegin).not.toBe(-1);
+ const config = JSON.parse(output.slice(configBegin));
+
+ // From config file.
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 800, height: 600 });
+ expect(config.timeouts.action).toBe(10000);
+
+ // Env var overrides file value.
+ expect(config.timeouts.navigation).toBe(45000);
+
+ // From CLI arg (--in-memory).
+ expect(config.browser.isolated).toBe(true);
+});
+
test('isolated', async ({ cli, server }, testInfo) => {
const config = {
browser: {
diff --git a/tests/mcp/config.spec.ts b/tests/mcp/config.spec.ts
index e6fb85bab8..9e0eaadfe8 100644
--- a/tests/mcp/config.spec.ts
+++ b/tests/mcp/config.spec.ts
@@ -16,7 +16,7 @@
import fs from 'node:fs';
-import { test, expect } from './fixtures';
+import { test, expect, parseResponse } from './fixtures';
import { resolveCLIConfig } from '../../packages/playwright/lib/mcp/browser/config';
import type { Config } from '../../packages/playwright/src/mcp/config';
@@ -90,3 +90,42 @@ test('test sandbox configuration', async ({}) => {
expect(await sandboxOption({ browser: 'chrome' })).toBe(true);
expect(await sandboxOption({ browser: 'msedge' })).toBe(true);
});
+
+test('browser_get_config returns merged config from file, env and cli', async ({ startClient }) => {
+ const { client } = await startClient({
+ config: {
+ browser: {
+ contextOptions: {
+ viewport: { width: 800, height: 600 },
+ },
+ },
+ capabilities: ['config'],
+ timeouts: {
+ action: 10000,
+ navigation: 30000,
+ },
+ },
+ args: ['--isolated'],
+ env: {
+ PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION: '45000',
+ },
+ });
+
+ const result = await client.callTool({
+ name: 'browser_get_config',
+ });
+
+ expect(result.isError).toBeFalsy();
+ const parsed = parseResponse(result);
+ const config = JSON.parse(parsed.result);
+
+ // From config file.
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 800, height: 600 });
+ expect(config.timeouts.action).toBe(10000);
+
+ // Env var overrides file value.
+ expect(config.timeouts.navigation).toBe(45000);
+
+ // From CLI arg (--isolated).
+ expect(config.browser.isolated).toBe(true);
+});