Skip to content

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

test(config): consolidate config resolution tests and add env injection (#39911)

Pavel Feldman33fd4917b68f
diff --git a/packages/playwright-core/src/tools/mcp/config.ts b/packages/playwright-core/src/tools/mcp/config.ts
index 32f302d37f..68eae08a60 100644
--- a/packages/playwright-core/src/tools/mcp/config.ts
+++ b/packages/playwright-core/src/tools/mcp/config.ts
@@ -110,8 +110,8 @@ export async function resolveConfig(config: Config): Promise<FullConfig> {
return { ...merged, browser };
}
-export async function resolveCLIConfigForMCP(cliOptions: CLIOptions): Promise<FullConfig> {
- const envOverrides = configFromEnv();
+export async function resolveCLIConfigForMCP(cliOptions: CLIOptions, env?: NodeJS.ProcessEnv): Promise<FullConfig> {
+ const envOverrides = configFromEnv(env);
const cliOverrides = configFromCLIOptions(cliOptions);
const configFile = cliOverrides.configFile ?? envOverrides.configFile;
const configInFile = await loadConfig(configFile);
@@ -128,7 +128,7 @@ export async function resolveCLIConfigForMCP(cliOptions: CLIOptions): Promise<Fu
return { ...result, browser, configFile };
}
-export async function resolveCLIConfigForCLI(daemonProfilesDir: string, sessionName: string, options: any): Promise<FullConfig> {
+export async function resolveCLIConfigForCLI(daemonProfilesDir: string, sessionName: string, options: any, env?: NodeJS.ProcessEnv): Promise<FullConfig> {
const config = options.config ? path.resolve(options.config) : undefined;
try {
const defaultConfigFile = path.resolve('.playwright', 'cli.config.json');
@@ -147,17 +147,17 @@ export async function resolveCLIConfigForCLI(daemonProfilesDir: string, sessionN
});
daemonOverrides.browser!.remoteEndpoint = options.attach;
- const envOverrides = configFromEnv();
- const configFile = envOverrides.configFile ?? daemonOverrides.configFile;
+ const envOverrides = configFromEnv(env);
+ const configFile = daemonOverrides.configFile ?? envOverrides.configFile;
const configInFile = await loadConfig(configFile);
- const globalConfigPath = path.join(process.env['PWTEST_CLI_GLOBAL_CONFIG'] ?? os.homedir(), '.playwright', 'cli.config.json');
+ const globalConfigPath = path.join((env ?? process.env)['PWTEST_CLI_GLOBAL_CONFIG'] ?? os.homedir(), '.playwright', 'cli.config.json');
const globalConfigInFile = await loadConfig(fs.existsSync(globalConfigPath) ? globalConfigPath : undefined);
let result = defaultConfig;
result = mergeConfig(result, globalConfigInFile);
result = mergeConfig(result, configInFile);
- result = mergeConfig(result, daemonOverrides);
result = mergeConfig(result, envOverrides);
+ result = mergeConfig(result, daemonOverrides);
if (result.browser.isolated === undefined)
result.browser.isolated = !options.profile && !options.persistent && !result.browser.userDataDir && !result.browser.remoteEndpoint && !result.extension;
@@ -332,50 +332,51 @@ function configFromCLIOptions(cliOptions: CLIOptions): Config & { configFile?: s
return { ...config, configFile: cliOptions.config };
}
-export function configFromEnv(): Config & { configFile?: string } {
+export function configFromEnv(env?: NodeJS.ProcessEnv): Config & { configFile?: string } {
+ const e = env ?? process.env;
const options: CLIOptions = {};
- options.allowedHosts = commaSeparatedList(process.env.PLAYWRIGHT_MCP_ALLOWED_HOSTS);
- options.allowedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_ALLOWED_ORIGINS);
- options.allowUnrestrictedFileAccess = envToBoolean(process.env.PLAYWRIGHT_MCP_ALLOW_UNRESTRICTED_FILE_ACCESS);
- options.blockedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_BLOCKED_ORIGINS);
- options.blockServiceWorkers = envToBoolean(process.env.PLAYWRIGHT_MCP_BLOCK_SERVICE_WORKERS);
- options.browser = envToString(process.env.PLAYWRIGHT_MCP_BROWSER);
- options.caps = commaSeparatedList(process.env.PLAYWRIGHT_MCP_CAPS);
- options.cdpEndpoint = envToString(process.env.PLAYWRIGHT_MCP_CDP_ENDPOINT);
- options.cdpHeader = headerParser(envToString(process.env.PLAYWRIGHT_MCP_CDP_HEADERS));
- options.cdpTimeout = numberParser(process.env.PLAYWRIGHT_MCP_CDP_TIMEOUT);
- options.config = envToString(process.env.PLAYWRIGHT_MCP_CONFIG);
- if (process.env.PLAYWRIGHT_MCP_CONSOLE_LEVEL)
- options.consoleLevel = enumParser<'error' | 'warning' | 'info' | 'debug'>('--console-level', ['error', 'warning', 'info', 'debug'], process.env.PLAYWRIGHT_MCP_CONSOLE_LEVEL);
- options.device = envToString(process.env.PLAYWRIGHT_MCP_DEVICE);
- options.executablePath = envToString(process.env.PLAYWRIGHT_MCP_EXECUTABLE_PATH);
- options.extension = envToBoolean(process.env.PLAYWRIGHT_MCP_EXTENSION);
- options.grantPermissions = commaSeparatedList(process.env.PLAYWRIGHT_MCP_GRANT_PERMISSIONS);
- options.headless = envToBoolean(process.env.PLAYWRIGHT_MCP_HEADLESS);
- options.host = envToString(process.env.PLAYWRIGHT_MCP_HOST);
- options.ignoreHttpsErrors = envToBoolean(process.env.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);
- const initPage = envToString(process.env.PLAYWRIGHT_MCP_INIT_PAGE);
+ options.allowedHosts = commaSeparatedList(e.PLAYWRIGHT_MCP_ALLOWED_HOSTS);
+ options.allowedOrigins = semicolonSeparatedList(e.PLAYWRIGHT_MCP_ALLOWED_ORIGINS);
+ options.allowUnrestrictedFileAccess = envToBoolean(e.PLAYWRIGHT_MCP_ALLOW_UNRESTRICTED_FILE_ACCESS);
+ options.blockedOrigins = semicolonSeparatedList(e.PLAYWRIGHT_MCP_BLOCKED_ORIGINS);
+ options.blockServiceWorkers = envToBoolean(e.PLAYWRIGHT_MCP_BLOCK_SERVICE_WORKERS);
+ options.browser = envToString(e.PLAYWRIGHT_MCP_BROWSER);
+ options.caps = commaSeparatedList(e.PLAYWRIGHT_MCP_CAPS);
+ options.cdpEndpoint = envToString(e.PLAYWRIGHT_MCP_CDP_ENDPOINT);
+ options.cdpHeader = headerParser(envToString(e.PLAYWRIGHT_MCP_CDP_HEADERS));
+ options.cdpTimeout = numberParser(e.PLAYWRIGHT_MCP_CDP_TIMEOUT);
+ options.config = envToString(e.PLAYWRIGHT_MCP_CONFIG);
+ if (e.PLAYWRIGHT_MCP_CONSOLE_LEVEL)
+ options.consoleLevel = enumParser<'error' | 'warning' | 'info' | 'debug'>('--console-level', ['error', 'warning', 'info', 'debug'], e.PLAYWRIGHT_MCP_CONSOLE_LEVEL);
+ options.device = envToString(e.PLAYWRIGHT_MCP_DEVICE);
+ options.executablePath = envToString(e.PLAYWRIGHT_MCP_EXECUTABLE_PATH);
+ options.extension = envToBoolean(e.PLAYWRIGHT_MCP_EXTENSION);
+ options.grantPermissions = commaSeparatedList(e.PLAYWRIGHT_MCP_GRANT_PERMISSIONS);
+ options.headless = envToBoolean(e.PLAYWRIGHT_MCP_HEADLESS);
+ options.host = envToString(e.PLAYWRIGHT_MCP_HOST);
+ options.ignoreHttpsErrors = envToBoolean(e.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);
+ const initPage = envToString(e.PLAYWRIGHT_MCP_INIT_PAGE);
if (initPage)
options.initPage = [initPage];
- const initScript = envToString(process.env.PLAYWRIGHT_MCP_INIT_SCRIPT);
+ const initScript = envToString(e.PLAYWRIGHT_MCP_INIT_SCRIPT);
if (initScript)
options.initScript = [initScript];
- options.isolated = envToBoolean(process.env.PLAYWRIGHT_MCP_ISOLATED);
- if (process.env.PLAYWRIGHT_MCP_IMAGE_RESPONSES)
- options.imageResponses = enumParser<'allow' | 'omit'>('--image-responses', ['allow', 'omit'], process.env.PLAYWRIGHT_MCP_IMAGE_RESPONSES);
- options.sandbox = envToBoolean(process.env.PLAYWRIGHT_MCP_SANDBOX);
- options.outputDir = envToString(process.env.PLAYWRIGHT_MCP_OUTPUT_DIR);
- options.port = numberParser(process.env.PLAYWRIGHT_MCP_PORT);
- options.proxyBypass = envToString(process.env.PLAYWRIGHT_MCP_PROXY_BYPASS);
- options.proxyServer = envToString(process.env.PLAYWRIGHT_MCP_PROXY_SERVER);
- options.secrets = dotenvFileLoader(process.env.PLAYWRIGHT_MCP_SECRETS_FILE);
- options.storageState = envToString(process.env.PLAYWRIGHT_MCP_STORAGE_STATE);
- options.testIdAttribute = envToString(process.env.PLAYWRIGHT_MCP_TEST_ID_ATTRIBUTE);
- options.timeoutAction = numberParser(process.env.PLAYWRIGHT_MCP_TIMEOUT_ACTION);
- options.timeoutNavigation = numberParser(process.env.PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION);
- options.userAgent = envToString(process.env.PLAYWRIGHT_MCP_USER_AGENT);
- options.userDataDir = envToString(process.env.PLAYWRIGHT_MCP_USER_DATA_DIR);
- options.viewportSize = resolutionParser('--viewport-size', process.env.PLAYWRIGHT_MCP_VIEWPORT_SIZE);
+ options.isolated = envToBoolean(e.PLAYWRIGHT_MCP_ISOLATED);
+ if (e.PLAYWRIGHT_MCP_IMAGE_RESPONSES)
+ options.imageResponses = enumParser<'allow' | 'omit'>('--image-responses', ['allow', 'omit'], e.PLAYWRIGHT_MCP_IMAGE_RESPONSES);
+ options.sandbox = envToBoolean(e.PLAYWRIGHT_MCP_SANDBOX);
+ options.outputDir = envToString(e.PLAYWRIGHT_MCP_OUTPUT_DIR);
+ options.port = numberParser(e.PLAYWRIGHT_MCP_PORT);
+ options.proxyBypass = envToString(e.PLAYWRIGHT_MCP_PROXY_BYPASS);
+ options.proxyServer = envToString(e.PLAYWRIGHT_MCP_PROXY_SERVER);
+ options.secrets = dotenvFileLoader(e.PLAYWRIGHT_MCP_SECRETS_FILE);
+ options.storageState = envToString(e.PLAYWRIGHT_MCP_STORAGE_STATE);
+ options.testIdAttribute = envToString(e.PLAYWRIGHT_MCP_TEST_ID_ATTRIBUTE);
+ options.timeoutAction = numberParser(e.PLAYWRIGHT_MCP_TIMEOUT_ACTION);
+ options.timeoutNavigation = numberParser(e.PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION);
+ options.userAgent = envToString(e.PLAYWRIGHT_MCP_USER_AGENT);
+ options.userDataDir = envToString(e.PLAYWRIGHT_MCP_USER_DATA_DIR);
+ options.viewportSize = resolutionParser('--viewport-size', e.PLAYWRIGHT_MCP_VIEWPORT_SIZE);
return configFromCLIOptions(options);
}
diff --git a/tests/mcp/cli-config-resolve.spec.ts b/tests/mcp/cli-config-resolve.spec.ts
deleted file mode 100644
index e2cbbd7934..0000000000
--- a/tests/mcp/cli-config-resolve.spec.ts
+++ /dev/null
@@ -1,273 +0,0 @@
-/**
- * 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 fs from 'fs';
-import path from 'path';
-
-import { test, expect } from '@playwright/test';
-
-import { resolveCLIConfigForCLI } from '../../packages/playwright-core/lib/tools/mcp/config';
-
-import type { Config } from '../../packages/playwright-core/src/tools/mcp/config.d';
-
-test.describe('resolveCLIConfigForCLI - browserName and channel', () => {
- test('no browser option defaults to chromium / chrome', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {});
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('chrome');
- });
-
- test('--browser=chrome sets chromium with chrome channel', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'chrome' });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('chrome');
- });
-
- test('--browser=chromium sets chromium with chrome-for-testing channel', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'chromium' });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('chrome-for-testing');
- });
-
- test('--browser=firefox sets firefox without channel', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'firefox' });
- expect(config.browser.browserName).toBe('firefox');
- expect(config.browser.launchOptions.channel).toBeUndefined();
- });
-
- test('--browser=webkit sets webkit without channel', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'webkit' });
- expect(config.browser.browserName).toBe('webkit');
- expect(config.browser.launchOptions.channel).toBeUndefined();
- });
-
- test('--browser=msedge sets chromium with msedge channel', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'msedge' });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('msedge');
- });
-
- test('config file browserName chromium does not auto-set channel', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ browser: { browserName: 'chromium' } }));
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBeUndefined();
- });
-
- test('config file browserName + channel are both preserved', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- browser: { browserName: 'chromium', launchOptions: { channel: 'msedge' } },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('msedge');
- });
-});
-
-test.describe('resolveCLIConfigForCLI - headless and viewport', () => {
- test('headless by default', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {});
- expect(config.browser.launchOptions.headless).toBe(true);
- });
-
- test('--headed sets headless=false', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { headed: true });
- expect(config.browser.launchOptions.headless).toBe(false);
- });
-
- test('headless viewport defaults to 1280x720', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {});
- expect(config.browser.contextOptions.viewport).toEqual({ width: 1280, height: 720 });
- });
-
- test('headed viewport defaults to null', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { headed: true });
- expect(config.browser.contextOptions.viewport).toBeNull();
- });
-
- test('config file viewport is preserved', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- browser: { contextOptions: { viewport: { width: 640, height: 480 } } },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
- expect(config.browser.contextOptions.viewport).toEqual({ width: 640, height: 480 });
- });
-});
-
-test.describe('resolveCLIConfigForCLI - isolated and userDataDir', () => {
- test('defaults to isolated when no profile, persistent, userDataDir, or remoteEndpoint', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {});
- expect(config.browser.isolated).toBe(true);
- });
-
- test('not isolated when --profile is set', async ({}, testInfo) => {
- const profileDir = testInfo.outputPath('my-profile');
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { profile: profileDir });
- expect(config.browser.isolated).toBe(false);
- expect(config.browser.userDataDir).toBe(profileDir);
- });
-
- test('not isolated when --persistent is set', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { persistent: true });
- expect(config.browser.isolated).toBe(false);
- });
-
- test('not isolated when --attach is set', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { attach: 'ws://localhost:1234' });
- expect(config.browser.isolated).toBe(false);
- });
-
- test('not isolated when config file sets userDataDir', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const userDataDir = testInfo.outputPath('custom-data');
- await fs.promises.writeFile(configFile, JSON.stringify({ browser: { userDataDir } }));
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
- expect(config.browser.isolated).toBe(false);
- expect(config.browser.userDataDir).toBe(userDataDir);
- });
-
- test('auto userDataDir uses daemonProfilesDir with session and browser token', async ({}, testInfo) => {
- const profilesDir = testInfo.outputPath('profiles');
- const config = await resolveCLIConfigForCLI(profilesDir, 'mysession', { persistent: true, browser: 'chrome' });
- expect(config.browser.userDataDir).toBe(path.resolve(profilesDir, 'ud-mysession-chrome'));
- });
-
- test('auto userDataDir uses browserName when no channel', async ({}, testInfo) => {
- const profilesDir = testInfo.outputPath('profiles');
- const config = await resolveCLIConfigForCLI(profilesDir, 'default', { persistent: true, browser: 'firefox' });
- expect(config.browser.userDataDir).toBe(path.resolve(profilesDir, 'ud-default-firefox'));
- });
-
- test('auto userDataDir uses undefined token when no browser specified', async ({}, testInfo) => {
- const profilesDir = testInfo.outputPath('profiles');
- const config = await resolveCLIConfigForCLI(profilesDir, 'default', { persistent: true });
- expect(config.browser.userDataDir).toBe(path.resolve(profilesDir, 'ud-default-undefined'));
- });
-
- test('no auto userDataDir when isolated', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {});
- expect(config.browser.isolated).toBe(true);
- expect(config.browser.userDataDir).toBeUndefined();
- });
-
- test('no auto userDataDir when --profile is set', async ({}, testInfo) => {
- const profileDir = testInfo.outputPath('my-profile');
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { profile: profileDir });
- expect(config.browser.userDataDir).toBe(profileDir);
- });
-
- test('no auto userDataDir when remoteEndpoint is set', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { attach: 'ws://localhost:1234' });
- expect(config.browser.userDataDir).toBeUndefined();
- });
-});
-
-test.describe('resolveCLIConfigForCLI - timeouts', () => {
- test('default timeouts', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {});
- expect(config.timeouts.action).toBe(5000);
- expect(config.timeouts.navigation).toBe(60000);
- expect(config.timeouts.expect).toBe(5000);
- });
-
- test('config file timeouts override defaults', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 7000 } }));
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
- expect(config.timeouts.action).toBe(7000);
- expect(config.timeouts.navigation).toBe(60000);
- });
-});
-
-test.describe('resolveCLIConfigForCLI - sandbox', () => {
- test('chromium sandbox enabled for chrome channel', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'chrome' });
- expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
- });
-
- test('chromium sandbox for chrome-for-testing channel', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'chromium' });
- expect(config.browser.launchOptions.channel).toBe('chrome-for-testing');
- if (process.platform === 'linux')
- expect(config.browser.launchOptions.chromiumSandbox).toBe(false);
- else
- expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
- });
-
- test('sandbox not set for non-chromium browsers', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'firefox' });
- expect(config.browser.launchOptions.chromiumSandbox).toBeUndefined();
- });
-});
-
-test.describe('resolveCLIConfigForCLI - skillMode and snapshotMode', () => {
- test('skillMode is always set', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {});
- expect(config.skillMode).toBe(true);
- });
-
- test('snapshot mode is full', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', {}) as any;
- expect(config.snapshot?.mode).toBe('full');
- });
-});
-
-test.describe('resolveCLIConfigForCLI - config file discovery', () => {
- test('explicit config file is used', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 3000 } }));
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
- expect(config.timeouts.action).toBe(3000);
- expect(config.configFile).toBe(configFile);
- });
-
- test('merge order: config file < daemon overrides', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- browser: { browserName: 'firefox' },
- timeouts: { action: 1000 },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- // --browser cli option overrides config file browserName.
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile, browser: 'webkit' });
- expect(config.browser.browserName).toBe('webkit');
- // Timeouts from file are preserved.
- expect(config.timeouts.action).toBe(1000);
- });
-
- test('config file values preserved when cli does not override', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- network: { allowedOrigins: ['https://example.com'] },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { config: configFile }) as any;
- expect(config.network?.allowedOrigins).toEqual(['https://example.com']);
- });
-});
-
-test.describe('resolveCLIConfigForCLI - extension', () => {
- test('--extension disables isolated', async ({}, testInfo) => {
- const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { extension: true }) as any;
- expect(config.extension).toBe(true);
- expect(config.browser.isolated).toBe(false);
- });
-});
diff --git a/tests/mcp/config-resolve.spec.ts b/tests/mcp/config-resolve.spec.ts
new file mode 100644
index 0000000000..14476722d7
--- /dev/null
+++ b/tests/mcp/config-resolve.spec.ts
@@ -0,0 +1,466 @@
+/**
+ * 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 fs from 'fs';
+import path from 'path';
+
+import { test, expect } from '@playwright/test';
+
+import { resolveCLIConfigForCLI, resolveCLIConfigForMCP } from '../../packages/playwright-core/lib/tools/mcp/config';
+
+import type { Config } from '../../packages/playwright-core/src/tools/mcp/config.d';
+
+// Empty env to isolate tests from the host environment.
+const emptyEnv = {};
+
+// ---------------------------------------------------------------------------
+// Shared behavior — browserName / channel resolution
+// These are tested via resolveCLIConfigForMCP; the underlying configFromCLIOptions
+// and validateBrowserConfig are shared with resolveCLIConfigForCLI.
+// ---------------------------------------------------------------------------
+
+test.describe('browserName and channel', () => {
+ test('no browser option defaults to chromium / chrome', async () => {
+ const config = await resolveCLIConfigForMCP({}, emptyEnv);
+ expect(config.browser.browserName).toBe('chromium');
+ expect(config.browser.launchOptions.channel).toBe('chrome');
+ });
+
+ test('--browser=chrome sets chromium with chrome channel', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'chrome' }, emptyEnv);
+ expect(config.browser.browserName).toBe('chromium');
+ expect(config.browser.launchOptions.channel).toBe('chrome');
+ });
+
+ test('--browser=chromium sets chromium with chrome-for-testing channel', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'chromium' }, emptyEnv);
+ expect(config.browser.browserName).toBe('chromium');
+ expect(config.browser.launchOptions.channel).toBe('chrome-for-testing');
+ });
+
+ test('--browser=firefox sets firefox without channel', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'firefox' }, emptyEnv);
+ expect(config.browser.browserName).toBe('firefox');
+ expect(config.browser.launchOptions.channel).toBeUndefined();
+ });
+
+ test('--browser=webkit sets webkit without channel', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'webkit' }, emptyEnv);
+ expect(config.browser.browserName).toBe('webkit');
+ expect(config.browser.launchOptions.channel).toBeUndefined();
+ });
+
+ test('--browser=msedge sets chromium with msedge channel', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'msedge' }, emptyEnv);
+ expect(config.browser.browserName).toBe('chromium');
+ expect(config.browser.launchOptions.channel).toBe('msedge');
+ });
+
+ test('config file browserName chromium does not auto-set channel', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ browser: { browserName: 'chromium' } }));
+ const config = await resolveCLIConfigForMCP({ config: configFile }, emptyEnv);
+ expect(config.browser.browserName).toBe('chromium');
+ expect(config.browser.launchOptions.channel).toBeUndefined();
+ });
+
+ test('config file browserName firefox does not set channel', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ browser: { browserName: 'firefox' } }));
+ const config = await resolveCLIConfigForMCP({ config: configFile }, emptyEnv);
+ expect(config.browser.browserName).toBe('firefox');
+ expect(config.browser.launchOptions.channel).toBeUndefined();
+ });
+
+ test('config file browserName + channel are both preserved', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ browser: { browserName: 'chromium', launchOptions: { channel: 'msedge' } },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLIConfigForMCP({ config: configFile }, emptyEnv);
+ expect(config.browser.browserName).toBe('chromium');
+ expect(config.browser.launchOptions.channel).toBe('msedge');
+ });
+
+ test('cli --browser overrides config file browserName', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ browser: { browserName: 'firefox' } }));
+ const config = await resolveCLIConfigForMCP({ config: configFile, browser: 'webkit' }, emptyEnv);
+ expect(config.browser.browserName).toBe('webkit');
+ });
+});
+
+// ---------------------------------------------------------------------------
+// Shared behavior — sandbox
+// ---------------------------------------------------------------------------
+
+test.describe('sandbox', () => {
+ test('chromium sandbox enabled for chrome channel', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'chrome' }, emptyEnv);
+ expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
+ });
+
+ test('chromium sandbox for chrome-for-testing channel', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'chromium' }, emptyEnv);
+ expect(config.browser.launchOptions.channel).toBe('chrome-for-testing');
+ if (process.platform === 'linux')
+ expect(config.browser.launchOptions.chromiumSandbox).toBe(false);
+ else
+ expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
+ });
+
+ test('sandbox not set for non-chromium browsers', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'firefox' }, emptyEnv);
+ expect(config.browser.launchOptions.chromiumSandbox).toBeUndefined();
+ });
+
+ test('explicit --sandbox overrides default', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'chromium', sandbox: true }, emptyEnv);
+ expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
+ });
+
+ test('explicit --no-sandbox overrides default', async () => {
+ const config = await resolveCLIConfigForMCP({ browser: 'chrome', sandbox: false }, emptyEnv);
+ expect(config.browser.launchOptions.chromiumSandbox).toBe(false);
+ });
+});
+
+// ---------------------------------------------------------------------------
+// Shared behavior — timeouts
+// ---------------------------------------------------------------------------
+
+test.describe('timeouts', () => {
+ test('default timeouts', async () => {
+ const config = await resolveCLIConfigForMCP({}, emptyEnv);
+ expect(config.timeouts.action).toBe(5000);
+ expect(config.timeouts.navigation).toBe(60000);
+ expect(config.timeouts.expect).toBe(5000);
+ });
+
+ test('config file timeouts override defaults', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 7000 } }));
+ const config = await resolveCLIConfigForMCP({ config: configFile }, emptyEnv);
+ expect(config.timeouts.action).toBe(7000);
+ expect(config.timeouts.navigation).toBe(60000);
+ });
+});
+
+// ---------------------------------------------------------------------------
+// Shared behavior — viewport
+// ---------------------------------------------------------------------------
+
+test.describe('viewport', () => {
+ test('headless sets default viewport 1280x720', async () => {
+ const config = await resolveCLIConfigForMCP({ headless: true }, emptyEnv);
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 1280, height: 720 });
+ });
+
+ test('headed sets viewport to null', async () => {
+ const config = await resolveCLIConfigForMCP({ headless: false }, emptyEnv);
+ expect(config.browser.contextOptions.viewport).toBeNull();
+ });
+
+ test('explicit viewport is preserved', async () => {
+ const config = await resolveCLIConfigForMCP({ headless: true, viewportSize: { width: 800, height: 600 } }, emptyEnv);
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 800, height: 600 });
+ });
+
+ test('config file viewport is preserved', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ browser: { contextOptions: { viewport: { width: 640, height: 480 } } },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLIConfigForMCP({ config: configFile, headless: true }, emptyEnv);
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 640, height: 480 });
+ });
+});
+
+// ---------------------------------------------------------------------------
+// Shared behavior — merge order and config file
+// ---------------------------------------------------------------------------
+
+test.describe('merge order', () => {
+ test('cli overrides config file', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ timeouts: { action: 1000 },
+ browser: { contextOptions: { viewport: { width: 640, height: 480 } } },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLIConfigForMCP({ config: configFile, timeoutAction: 9999 }, emptyEnv);
+ expect(config.timeouts.action).toBe(9999);
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 640, height: 480 });
+ });
+
+ test('config file values preserved when cli does not override', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ network: { allowedOrigins: ['https://example.com'] },
+ browser: { isolated: true },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLIConfigForMCP({ config: configFile }, emptyEnv) as any;
+ expect(config.network?.allowedOrigins).toEqual(['https://example.com']);
+ expect(config.browser.isolated).toBe(true);
+ });
+
+ test('env overrides config file', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { navigation: 1000 } }));
+ const config = await resolveCLIConfigForMCP({ config: configFile }, {
+ PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION: '45000',
+ });
+ expect(config.timeouts.navigation).toBe(45000);
+ });
+
+ test('cli overrides env', async () => {
+ const config = await resolveCLIConfigForMCP({ timeoutAction: 3000, browser: 'firefox' }, {
+ PLAYWRIGHT_MCP_TIMEOUT_ACTION: '9000',
+ PLAYWRIGHT_MCP_BROWSER: 'webkit',
+ });
+ expect(config.timeouts.action).toBe(3000);
+ expect(config.browser.browserName).toBe('firefox');
+ });
+
+ test('file browser.cdpHeaders preserved when env unset', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ browser: {
+ cdpEndpoint: 'ws://example.invalid',
+ cdpHeaders: { Authorization: 'Bearer token-from-file' },
+ },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLIConfigForMCP({ config: configFile }, emptyEnv);
+ expect(config.browser.cdpHeaders).toEqual({ Authorization: 'Bearer token-from-file' });
+ });
+});
+
+// ---------------------------------------------------------------------------
+// Shared behavior — validation
+// ---------------------------------------------------------------------------
+
+test.describe('validation', () => {
+ test('isolated + userDataDir throws', async () => {
+ await expect(resolveCLIConfigForMCP({ isolated: true, userDataDir: '/tmp/data' }, emptyEnv))
+ .rejects.toThrow('Browser userDataDir is not supported in isolated mode.');
+ });
+});
+
+// ---------------------------------------------------------------------------
+// MCP-specific: headless platform default, timeout CLI options
+// ---------------------------------------------------------------------------
+
+test.describe('resolveCLIConfigForMCP', () => {
+ test('headless defaults based on platform', async () => {
+ const config = await resolveCLIConfigForMCP({}, emptyEnv);
+ expect(config.browser.launchOptions.headless).toBeDefined();
+ });
+
+ test('cli timeout overrides defaults', async () => {
+ const config = await resolveCLIConfigForMCP({ timeoutAction: 10000, timeoutNavigation: 30000 }, emptyEnv);
+ expect(config.timeouts.action).toBe(10000);
+ expect(config.timeouts.navigation).toBe(30000);
+ expect(config.timeouts.expect).toBe(5000);
+ });
+
+ test('cli timeout overrides config file timeout', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 1000 } }));
+ const config = await resolveCLIConfigForMCP({ config: configFile, timeoutAction: 9999 }, emptyEnv);
+ expect(config.timeouts.action).toBe(9999);
+ });
+});
+
+// ---------------------------------------------------------------------------
+// CLI-specific: headless, isolated, userDataDir, extension, skillMode, snapshot
+// ---------------------------------------------------------------------------
+
+function resolveCLI(profilesDir: string, sessionName: string, options: any) {
+ return resolveCLIConfigForCLI(profilesDir, sessionName, options, emptyEnv);
+}
+
+test.describe('resolveCLIConfigForCLI - headless and viewport', () => {
+ test('headless by default', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', {});
+ expect(config.browser.launchOptions.headless).toBe(true);
+ });
+
+ test('--headed sets headless=false', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { headed: true });
+ expect(config.browser.launchOptions.headless).toBe(false);
+ });
+
+ test('headless viewport defaults to 1280x720', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', {});
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 1280, height: 720 });
+ });
+
+ test('headed viewport defaults to null', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { headed: true });
+ expect(config.browser.contextOptions.viewport).toBeNull();
+ });
+
+ test('config file viewport is preserved', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ browser: { contextOptions: { viewport: { width: 640, height: 480 } } },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
+ expect(config.browser.contextOptions.viewport).toEqual({ width: 640, height: 480 });
+ });
+});
+
+test.describe('resolveCLIConfigForCLI - isolated and userDataDir', () => {
+ test('defaults to isolated when no profile, persistent, userDataDir, or remoteEndpoint', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', {});
+ expect(config.browser.isolated).toBe(true);
+ });
+
+ test('not isolated when --profile is set', async ({}, testInfo) => {
+ const profileDir = testInfo.outputPath('my-profile');
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { profile: profileDir });
+ expect(config.browser.isolated).toBe(false);
+ expect(config.browser.userDataDir).toBe(profileDir);
+ });
+
+ test('not isolated when --persistent is set', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { persistent: true });
+ expect(config.browser.isolated).toBe(false);
+ });
+
+ test('not isolated when --attach is set', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { attach: 'ws://localhost:1234' });
+ expect(config.browser.isolated).toBe(false);
+ });
+
+ test('not isolated when config file sets userDataDir', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const userDataDir = testInfo.outputPath('custom-data');
+ await fs.promises.writeFile(configFile, JSON.stringify({ browser: { userDataDir } }));
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
+ expect(config.browser.isolated).toBe(false);
+ expect(config.browser.userDataDir).toBe(userDataDir);
+ });
+
+ test('auto userDataDir uses daemonProfilesDir with session and browser token', async ({}, testInfo) => {
+ const profilesDir = testInfo.outputPath('profiles');
+ const config = await resolveCLI(profilesDir, 'mysession', { persistent: true, browser: 'chrome' });
+ expect(config.browser.userDataDir).toBe(path.resolve(profilesDir, 'ud-mysession-chrome'));
+ });
+
+ test('auto userDataDir uses browserName when no channel', async ({}, testInfo) => {
+ const profilesDir = testInfo.outputPath('profiles');
+ const config = await resolveCLI(profilesDir, 'default', { persistent: true, browser: 'firefox' });
+ expect(config.browser.userDataDir).toBe(path.resolve(profilesDir, 'ud-default-firefox'));
+ });
+
+ test('auto userDataDir uses undefined token when no browser specified', async ({}, testInfo) => {
+ const profilesDir = testInfo.outputPath('profiles');
+ const config = await resolveCLI(profilesDir, 'default', { persistent: true });
+ expect(config.browser.userDataDir).toBe(path.resolve(profilesDir, 'ud-default-undefined'));
+ });
+
+ test('no auto userDataDir when isolated', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', {});
+ expect(config.browser.isolated).toBe(true);
+ expect(config.browser.userDataDir).toBeUndefined();
+ });
+
+ test('no auto userDataDir when --profile is set', async ({}, testInfo) => {
+ const profileDir = testInfo.outputPath('my-profile');
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { profile: profileDir });
+ expect(config.browser.userDataDir).toBe(profileDir);
+ });
+
+ test('no auto userDataDir when remoteEndpoint is set', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { attach: 'ws://localhost:1234' });
+ expect(config.browser.userDataDir).toBeUndefined();
+ });
+});
+
+test.describe('resolveCLIConfigForCLI - timeouts', () => {
+ test('config file timeouts override defaults', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 7000 } }));
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
+ expect(config.timeouts.action).toBe(7000);
+ expect(config.timeouts.navigation).toBe(60000);
+ });
+});
+
+test.describe('resolveCLIConfigForCLI - skillMode and snapshotMode', () => {
+ test('skillMode is always set', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', {});
+ expect(config.skillMode).toBe(true);
+ });
+
+ test('snapshot mode is full', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', {}) as any;
+ expect(config.snapshot?.mode).toBe('full');
+ });
+});
+
+test.describe('resolveCLIConfigForCLI - config file discovery', () => {
+ test('explicit config file is used', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 3000 } }));
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { config: configFile });
+ expect(config.timeouts.action).toBe(3000);
+ expect(config.configFile).toBe(configFile);
+ });
+
+ test('cli overrides env', async ({}, testInfo) => {
+ const config = await resolveCLIConfigForCLI(testInfo.outputPath('profiles'), 'default', { browser: 'firefox' }, {
+ PLAYWRIGHT_MCP_BROWSER: 'webkit',
+ });
+ expect(config.browser.browserName).toBe('firefox');
+ });
+
+ test('merge order: config file < daemon overrides', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ browser: { browserName: 'firefox' },
+ timeouts: { action: 1000 },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { config: configFile, browser: 'webkit' });
+ expect(config.browser.browserName).toBe('webkit');
+ expect(config.timeouts.action).toBe(1000);
+ });
+
+ test('config file values preserved when cli does not override', async ({}, testInfo) => {
+ const configFile = testInfo.outputPath('config.json');
+ const fileConfig: Config = {
+ network: { allowedOrigins: ['https://example.com'] },
+ };
+ await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { config: configFile }) as any;
+ expect(config.network?.allowedOrigins).toEqual(['https://example.com']);
+ });
+});
+
+test.describe('resolveCLIConfigForCLI - extension', () => {
+ test('--extension disables isolated', async ({}, testInfo) => {
+ const config = await resolveCLI(testInfo.outputPath('profiles'), 'default', { extension: true }) as any;
+ expect(config.extension).toBe(true);
+ expect(config.browser.isolated).toBe(false);
+ });
+});
diff --git a/tests/mcp/config.spec.ts b/tests/mcp/config.spec.ts
index fb78c3e188..8dc55216f8 100644
--- a/tests/mcp/config.spec.ts
+++ b/tests/mcp/config.spec.ts
@@ -17,7 +17,6 @@
import fs from 'node:fs';
import { test, expect, parseResponse } from './fixtures';
-import { resolveCLIConfigForMCP } from '../../packages/playwright-core/lib/tools/mcp/config';
import type { Config } from '../../packages/playwright-core/src/tools/mcp/config.d';
test('config user data dir', async ({ startClient, server }, testInfo) => {
@@ -105,42 +104,6 @@ test.describe(() => {
});
});
-async function sandboxOption(cli: any) {
- const config: any = await resolveCLIConfigForMCP(cli);
- return config.browser.launchOptions.chromiumSandbox;
-}
-
-test('test sandbox configuration', async ({}) => {
- expect(await sandboxOption({ browser: 'chromium' })).toBe(process.platform !== 'linux');
- expect(await sandboxOption({ browser: 'chromium', sandbox: true })).toBe(true);
- expect(await sandboxOption({ browser: 'chrome', sandbox: false })).toBe(false);
- expect(await sandboxOption({ browser: 'chrome' })).toBe(true);
- expect(await sandboxOption({ browser: 'msedge' })).toBe(true);
-});
-
-test('file browser.cdpHeaders preserved when PLAYWRIGHT_MCP_CDP_HEADERS unset', async ({}, testInfo) => {
- const prev = process.env.PLAYWRIGHT_MCP_CDP_HEADERS;
- delete process.env.PLAYWRIGHT_MCP_CDP_HEADERS;
- try {
- const config: Config = {
- browser: {
- cdpEndpoint: 'ws://example.invalid',
- cdpHeaders: { Authorization: 'Bearer token-from-file' },
- },
- };
- const configPath = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2));
-
- const resolved = await resolveCLIConfigForMCP({ config: configPath });
- expect(resolved.browser.cdpHeaders).toEqual({ Authorization: 'Bearer token-from-file' });
- } finally {
- if (prev !== undefined)
- process.env.PLAYWRIGHT_MCP_CDP_HEADERS = prev;
- else
- delete process.env.PLAYWRIGHT_MCP_CDP_HEADERS;
- }
-});
-
test('browser_get_config returns merged config from file, env and cli', async ({ startClient }) => {
const { client } = await startClient({
config: {
diff --git a/tests/mcp/mcp-config-resolve.spec.ts b/tests/mcp/mcp-config-resolve.spec.ts
deleted file mode 100644
index 639ac1fdf0..0000000000
--- a/tests/mcp/mcp-config-resolve.spec.ts
+++ /dev/null
@@ -1,233 +0,0 @@
-/**
- * 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 fs from 'fs';
-
-import { test, expect } from '@playwright/test';
-
-import { resolveCLIConfigForMCP } from '../../packages/playwright-core/lib/tools/mcp/config';
-
-import type { Config } from '../../packages/playwright-core/src/tools/mcp/config.d';
-
-test.describe('resolveCLIConfigForMCP - browserName and channel', () => {
- test('no config defaults to chromium with chrome channel', async () => {
- const config = await resolveCLIConfigForMCP({});
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('chrome');
- });
-
- test('--browser=chrome resolves to chromium with chrome channel', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'chrome' });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('chrome');
- });
-
- test('--browser=chromium resolves to chromium with chrome-for-testing channel', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'chromium' });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('chrome-for-testing');
- });
-
- test('--browser=firefox resolves to firefox without channel', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'firefox' });
- expect(config.browser.browserName).toBe('firefox');
- expect(config.browser.launchOptions.channel).toBeUndefined();
- });
-
- test('--browser=webkit resolves to webkit without channel', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'webkit' });
- expect(config.browser.browserName).toBe('webkit');
- expect(config.browser.launchOptions.channel).toBeUndefined();
- });
-
- test('--browser=msedge resolves to chromium with msedge channel', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'msedge' });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('msedge');
- });
-
- test('config file browserName chromium does not set chrome channel', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ browser: { browserName: 'chromium' } }));
- const config = await resolveCLIConfigForMCP({ config: configFile });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBeUndefined();
- });
-
- test('config file browserName firefox does not set channel', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ browser: { browserName: 'firefox' } }));
- const config = await resolveCLIConfigForMCP({ config: configFile });
- expect(config.browser.browserName).toBe('firefox');
- expect(config.browser.launchOptions.channel).toBeUndefined();
- });
-
- test('config file browserName + explicit channel are both preserved', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- browser: { browserName: 'chromium', launchOptions: { channel: 'msedge' } },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- const config = await resolveCLIConfigForMCP({ config: configFile });
- expect(config.browser.browserName).toBe('chromium');
- expect(config.browser.launchOptions.channel).toBe('msedge');
- });
-
- test('cli --browser overrides config file browserName', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ browser: { browserName: 'firefox' } }));
- const config = await resolveCLIConfigForMCP({ config: configFile, browser: 'webkit' });
- expect(config.browser.browserName).toBe('webkit');
- });
-});
-
-test.describe('resolveCLIConfigForMCP - headless and viewport', () => {
- test('headless defaults based on platform', async () => {
- const config = await resolveCLIConfigForMCP({});
- expect(config.browser.launchOptions.headless).toBeDefined();
- });
-
- test('explicit headless=true is preserved', async () => {
- const config = await resolveCLIConfigForMCP({ headless: true });
- expect(config.browser.launchOptions.headless).toBe(true);
- });
-
- test('explicit headless=false is preserved', async () => {
- const config = await resolveCLIConfigForMCP({ headless: false });
- expect(config.browser.launchOptions.headless).toBe(false);
- });
-
- test('headless sets default viewport 1280x720', async () => {
- const config = await resolveCLIConfigForMCP({ headless: true });
- expect(config.browser.contextOptions.viewport).toEqual({ width: 1280, height: 720 });
- });
-
- test('headed sets viewport to null', async () => {
- const config = await resolveCLIConfigForMCP({ headless: false });
- expect(config.browser.contextOptions.viewport).toBeNull();
- });
-
- test('explicit viewport is preserved when headless', async () => {
- const config = await resolveCLIConfigForMCP({ headless: true, viewportSize: { width: 800, height: 600 } });
- expect(config.browser.contextOptions.viewport).toEqual({ width: 800, height: 600 });
- });
-
- test('config file viewport is preserved when headless', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- browser: { contextOptions: { viewport: { width: 640, height: 480 } } },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- const config = await resolveCLIConfigForMCP({ config: configFile, headless: true });
- expect(config.browser.contextOptions.viewport).toEqual({ width: 640, height: 480 });
- });
-});
-
-test.describe('resolveCLIConfigForMCP - timeouts', () => {
- test('default timeouts', async () => {
- const config = await resolveCLIConfigForMCP({});
- expect(config.timeouts.action).toBe(5000);
- expect(config.timeouts.navigation).toBe(60000);
- expect(config.timeouts.expect).toBe(5000);
- });
-
- test('cli timeout overrides defaults', async () => {
- const config = await resolveCLIConfigForMCP({ timeoutAction: 10000, timeoutNavigation: 30000 });
- expect(config.timeouts.action).toBe(10000);
- expect(config.timeouts.navigation).toBe(30000);
- expect(config.timeouts.expect).toBe(5000);
- });
-
- test('config file timeouts are used', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 7000 } }));
- const config = await resolveCLIConfigForMCP({ config: configFile });
- expect(config.timeouts.action).toBe(7000);
- expect(config.timeouts.navigation).toBe(60000);
- });
-
- test('cli timeout overrides config file timeout', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- await fs.promises.writeFile(configFile, JSON.stringify({ timeouts: { action: 1000 } }));
- const config = await resolveCLIConfigForMCP({ config: configFile, timeoutAction: 9999 });
- expect(config.timeouts.action).toBe(9999);
- });
-});
-
-test.describe('resolveCLIConfigForMCP - sandbox', () => {
- test('chromium sandbox enabled for chrome channel', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'chrome' });
- expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
- });
-
- test('chromium sandbox for chrome-for-testing channel', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'chromium' });
- expect(config.browser.launchOptions.channel).toBe('chrome-for-testing');
- // On Linux, chrome-for-testing disables sandbox; on other platforms sandbox is always true.
- if (process.platform === 'linux')
- expect(config.browser.launchOptions.chromiumSandbox).toBe(false);
- else
- expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
- });
-
- test('sandbox not set for non-chromium browsers', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'firefox' });
- expect(config.browser.launchOptions.chromiumSandbox).toBeUndefined();
- });
-
- test('explicit --sandbox overrides default', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'chromium', sandbox: true });
- expect(config.browser.launchOptions.chromiumSandbox).toBe(true);
- });
-
- test('explicit --no-sandbox overrides default', async () => {
- const config = await resolveCLIConfigForMCP({ browser: 'chrome', sandbox: false });
- expect(config.browser.launchOptions.chromiumSandbox).toBe(false);
- });
-});
-
-test.describe('resolveCLIConfigForMCP - validation', () => {
- test('isolated + userDataDir throws', async () => {
- await expect(resolveCLIConfigForMCP({ isolated: true, userDataDir: '/tmp/data' }))
- .rejects.toThrow('Browser userDataDir is not supported in isolated mode.');
- });
-});
-
-test.describe('resolveCLIConfigForMCP - merge order', () => {
- test('cli overrides config file', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- timeouts: { action: 1000 },
- browser: { contextOptions: { viewport: { width: 640, height: 480 } } },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- const config = await resolveCLIConfigForMCP({ config: configFile, timeoutAction: 9999 });
- expect(config.timeouts.action).toBe(9999);
- expect(config.browser.contextOptions.viewport).toEqual({ width: 640, height: 480 });
- });
-
- test('config file values preserved when cli does not override', async ({}, testInfo) => {
- const configFile = testInfo.outputPath('config.json');
- const fileConfig: Config = {
- network: { allowedOrigins: ['https://example.com'] },
- browser: { isolated: true },
- };
- await fs.promises.writeFile(configFile, JSON.stringify(fileConfig));
- const config = await resolveCLIConfigForMCP({ config: configFile }) as any;
- expect(config.network?.allowedOrigins).toEqual(['https://example.com']);
- expect(config.browser.isolated).toBe(true);
- });
-});