Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: support config in cli (#38947)
Pavel Feldmandfb228e5b92b
diff --git a/packages/playwright/src/mcp/browser/browserContextFactory.ts b/packages/playwright/src/mcp/browser/browserContextFactory.tsindex 24dae33682..0495136362 100644--- a/packages/playwright/src/mcp/browser/browserContextFactory.ts+++ b/packages/playwright/src/mcp/browser/browserContextFactory.ts@@ -47,11 +47,7 @@ export type BrowserContextFactoryResult = {close: () => Promise<void>;};-export type HeadlessOption = {- forceHeadless?: 'headless' | 'headed';-};--type CreateContextOptions = HeadlessOption & {+type CreateContextOptions = {toolName?: string;};@@ -142,7 +138,6 @@ class IsolatedContextFactory extends BaseContextFactory {...this.config.browser.launchOptions,handleSIGINT: false,handleSIGTERM: false,- ...(options.forceHeadless !== undefined ? { headless: options.forceHeadless === 'headless' } : {}),}).catch(error => {if (error.message.includes('Executable doesn\'t exist'))throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);@@ -224,7 +219,6 @@ class PersistentContextFactory implements BrowserContextFactory {'--disable-extensions',],assistantMode: true,- ...(options.forceHeadless !== undefined ? { headless: options.forceHeadless === 'headless' } : {}),};try {const browserContext = await browserType.launchPersistentContext(userDataDir, launchOptions);diff --git a/packages/playwright/src/mcp/browser/config.ts b/packages/playwright/src/mcp/browser/config.tsindex 81a1f379aa..98dde261b4 100644--- a/packages/playwright/src/mcp/browser/config.ts+++ b/packages/playwright/src/mcp/browser/config.ts@@ -42,6 +42,9 @@ export type CLIOptions = {codegen?: 'typescript' | 'none';config?: string;consoleLevel?: 'error' | 'warning' | 'info' | 'debug';+ daemon?: string;+ daemonDataDir?: string;+ daemonHeaded?: boolean;device?: string;executablePath?: string;grantPermissions?: string[];@@ -104,6 +107,20 @@ export const defaultConfig: FullConfig = {},};+const defaultDaemonConfig = (cliOptions: CLIOptions) => mergeConfig(defaultConfig, {+ browser: {+ userDataDir: '<daemon-data-dir>',+ launchOptions: {+ headless: !cliOptions.daemonHeaded,+ }+ },+ outputMode: 'file',+ codegen: 'none',+ snapshot: {+ mode: 'full',+ },+});+type BrowserUserConfig = NonNullable<Config['browser']>;export type FullConfig = Config & {@@ -137,10 +154,19 @@ export async function resolveCLIConfig(cliOptions: CLIOptions): Promise<FullConfconst configInFile = await loadConfig(cliOptions.config);const envOverrides = configFromEnv();const cliOverrides = configFromCLIOptions(cliOptions);- let result = defaultConfig;+ let result = cliOptions.daemon ? defaultDaemonConfig(cliOptions) : defaultConfig;result = mergeConfig(result, configInFile);result = mergeConfig(result, envOverrides);result = mergeConfig(result, cliOverrides);+ if (cliOptions.daemon)+ result.skillMode = true;++ if (result.browser.userDataDir === '<daemon-data-dir>') {+ // No custom value provided, use the daemon data dir.+ const browserToken = result.browser.launchOptions?.channel ?? result.browser?.browserName;+ result.browser.userDataDir = `${cliOptions.daemonDataDir}-${browserToken}`;+ }+await validateConfig(result);return result;}@@ -196,6 +222,8 @@ export function configFromCLIOptions(cliOptions: CLIOptions): Config {// --no-sandbox was passed, disable the sandboxif (cliOptions.sandbox === false)launchOptions.chromiumSandbox = false;+ if (process.env.CI && process.platform === 'linux')+ launchOptions.chromiumSandbox = false;if (cliOptions.proxyServer) {launchOptions.proxy = {diff --git a/packages/playwright/src/mcp/browser/context.ts b/packages/playwright/src/mcp/browser/context.tsindex 9d87c5bc7e..940d29ec35 100644--- a/packages/playwright/src/mcp/browser/context.ts+++ b/packages/playwright/src/mcp/browser/context.ts@@ -30,7 +30,6 @@ import type { BrowserContextFactory, BrowserContextFactoryResult } from './browsimport type { SessionLog } from './sessionLog';import type { Tracing } from '../../../../playwright-core/src/client/tracing';import type { ClientInfo } from '../sdk/server';-import type { HeadlessOption } from './browserContextFactory';const testDebug = debug('pw:mcp:test');@@ -46,7 +45,6 @@ export class Context {readonly sessionLog: SessionLog | undefined;readonly options: ContextOptions;private _browserContextPromise: Promise<BrowserContextFactoryResult> | undefined;- private _browserContextOption: HeadlessOption | undefined;private _browserContextFactory: BrowserContextFactory;private _tabs: Tab[] = [];private _currentTab: Tab | undefined;@@ -86,7 +84,7 @@ export class Context {}async newTab(): Promise<Tab> {- const { browserContext } = await this._ensureBrowserContext({});+ const { browserContext } = await this._ensureBrowserContext();const page = await browserContext.newPage();this._currentTab = this._tabs.find(t => t.page === page)!;return this._currentTab;@@ -101,8 +99,8 @@ export class Context {return tab;}- async ensureTab(options: HeadlessOption = {}): Promise<Tab> {- const { browserContext } = await this._ensureBrowserContext(options);+ async ensureTab(): Promise<Tab> {+ const { browserContext } = await this._ensureBrowserContext();if (!this._currentTab)await browserContext.newPage();return this._currentTab!;@@ -163,7 +161,6 @@ export class Context {const promise = this._browserContextPromise;this._browserContextPromise = undefined;- this._browserContextOption = undefined;await promise.then(async ({ browserContext, close }) => {if (this.config.saveTrace)@@ -192,33 +189,30 @@ export class Context {}}- async ensureBrowserContext(options: HeadlessOption = {}): Promise<playwright.BrowserContext> {- const { browserContext } = await this._ensureBrowserContext(options);+ async ensureBrowserContext(): Promise<playwright.BrowserContext> {+ const { browserContext } = await this._ensureBrowserContext();return browserContext;}- private _ensureBrowserContext(options: HeadlessOption) {- if (this._browserContextPromise && (options.forceHeadless === undefined || this._browserContextOption?.forceHeadless === options.forceHeadless))+ private _ensureBrowserContext() {+ if (this._browserContextPromise)return this._browserContextPromise;- const closePrework = this._browserContextPromise ? this.closeBrowserContext() : Promise.resolve();- this._browserContextPromise = closePrework.then(() => this._setupBrowserContext(options));+ this._browserContextPromise = this._setupBrowserContext();this._browserContextPromise.catch(() => {this._browserContextPromise = undefined;- this._browserContextOption = undefined;});- this._browserContextOption = options;return this._browserContextPromise;}- private async _setupBrowserContext(options: HeadlessOption): Promise<BrowserContextFactoryResult> {+ private async _setupBrowserContext(): Promise<BrowserContextFactoryResult> {if (this._closeBrowserContextPromise)throw new Error('Another browser context is being closed.');// TODO: move to the browser context factory to make it based on isolation mode.if (this.config.testIdAttribute)selectors.setTestIdAttribute(this.config.testIdAttribute);- const result = await this._browserContextFactory.createContext(this._clientInfo, this._abortController.signal, { toolName: this._runningToolName, ...options });+ const result = await this._browserContextFactory.createContext(this._clientInfo, this._abortController.signal, { toolName: this._runningToolName });const { browserContext } = result;if (!this.config.allowUnrestrictedFileAccess) {(browserContext as any)._setAllowedProtocols(['http:', 'https:', 'about:', 'data:']);diff --git a/packages/playwright/src/mcp/browser/tools/navigate.ts b/packages/playwright/src/mcp/browser/tools/navigate.tsindex 233e8aeb8b..53b5d39f9a 100644--- a/packages/playwright/src/mcp/browser/tools/navigate.ts+++ b/packages/playwright/src/mcp/browser/tools/navigate.ts@@ -49,40 +49,6 @@ const navigate = defineTool({},});-const open = defineTool({- capability: 'core-navigation',- skillOnly: true,- schema: {- name: 'browser_open',- title: 'Open URL',- description: 'Open a URL in the browser',- inputSchema: z.object({- url: z.string().describe('The URL to open'),- headed: z.boolean().optional().describe('Run browser in headed mode'),- }),- type: 'action',- },-- handle: async (context, params, response) => {- const forceHeadless = params.headed ? 'headed' : 'headless';- const tab = await context.ensureTab({ forceHeadless });- let url = params.url;- try {- new URL(url);- } catch (e) {- if (url.startsWith('localhost'))- url = 'http://' + url;- else- url = 'https://' + url;- }-- await tab.navigate(url);-- response.setIncludeSnapshot();- response.addCode(`await page.goto('${params.url}');`);- },-});-const goBack = defineTabTool({capability: 'core-navigation',schema: {@@ -138,7 +104,6 @@ const reload = defineTabTool({export default [navigate,- open,goBack,goForward,reload,diff --git a/packages/playwright/src/mcp/program.ts b/packages/playwright/src/mcp/program.tsindex 274dbb662d..48f5a7775b 100644--- a/packages/playwright/src/mcp/program.ts+++ b/packages/playwright/src/mcp/program.ts@@ -77,6 +77,8 @@ export function decorateCommand(command: Command, version: string) {.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280x720"', resolutionParser.bind(null, '--viewport-size')).addOption(new ProgramOption('--vision', 'Legacy option, use --caps=vision instead').hideHelp()).addOption(new ProgramOption('--daemon <socket>', 'run as daemon').hideHelp())+ .addOption(new ProgramOption('--daemon-data-dir <path>', 'path to the daemon data directory.').hideHelp())+ .addOption(new ProgramOption('--daemon-headed', 'run daemon in headed mode').hideHelp()).action(async options => {setupExitWatchdog();@@ -111,11 +113,6 @@ export function decorateCommand(command: Command, version: string) {}if (options.daemon) {- config.skillMode = true;- config.outputMode = 'file';- config.codegen = 'none';- config.snapshot.mode = 'full';-const serverBackendFactory: mcpServer.ServerBackendFactory = {name: 'Playwright',nameInConfig: 'playwright-daemon',diff --git a/packages/playwright/src/mcp/terminal/commands.ts b/packages/playwright/src/mcp/terminal/commands.tsindex b81042bf30..eab4a6fe07 100644--- a/packages/playwright/src/mcp/terminal/commands.ts+++ b/packages/playwright/src/mcp/terminal/commands.ts@@ -29,11 +29,8 @@ const open = declareCommand({args: z.object({url: z.string().describe('The URL to navigate to'),}),- options: z.object({- headed: z.boolean().default(false).describe('Run browser in headed mode'),- }),- toolName: 'browser_open',- toolParams: ({ url, headed }) => ({ url, headed }),+ toolName: 'browser_navigate',+ toolParams: ({ url }) => ({ url }),});const close = declareCommand({diff --git a/packages/playwright/src/mcp/terminal/helpGenerator.ts b/packages/playwright/src/mcp/terminal/helpGenerator.tsindex 0062d8a628..82829029e4 100644--- a/packages/playwright/src/mcp/terminal/helpGenerator.ts+++ b/packages/playwright/src/mcp/terminal/helpGenerator.ts@@ -93,6 +93,14 @@ export function generateHelp() {for (const command of cc)lines.push(generateHelpEntry(command));}++ 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(' --help [command]', 'print help'));+ lines.push(formatWithGap(' --session', 'run command in the scope of a specific session'));+ lines.push(formatWithGap(' --version', 'print version'));+return lines.join('\n');}diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.tsindex af8a248285..b48f89e776 100644--- a/packages/playwright/src/mcp/terminal/program.ts+++ b/packages/playwright/src/mcp/terminal/program.ts@@ -27,7 +27,6 @@ import path from 'path';import { debug } from 'playwright-core/lib/utilsBundle';import { SocketConnection } from './socketConnection';-import type { SpawnOptions } from 'child_process';import type { Section } from '../browser/response';export type StructuredResponse = {@@ -96,16 +95,24 @@ class Session {}}+type SessionManagerOptions = { config?: string, headed?: boolean };+class SessionManager {+ private _options: SessionManagerOptions;++ constructor(options: SessionManagerOptions) {+ this._options = options;+ }async list(): Promise<{ name: string, live: boolean }[]> {- const dir = daemonSocketDir;+ const dir = daemonProfilesDir;try {const files = await fs.promises.readdir(dir);const sessions: { name: string, live: boolean }[] = [];for (const file of files) {- if (file.endsWith('-user-data')) {- const sessionName = file.slice(0, -'-user-data'.length);+ 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 });}@@ -147,16 +154,29 @@ class SessionManager {}// Delete user data directory- const userDataDir = path.resolve(daemonSocketDir, `${sessionName}-user-data`);- try {- await fs.promises.rm(userDataDir, { recursive: true });- console.log(`Deleted user data for session '${sessionName}'.`);- } catch (e: any) {- if (e.code === 'ENOENT')- console.log(`No user data found for session '${sessionName}'.`);- else- throw e;-+ const dataDirs = await fs.promises.readdir(daemonProfilesDir).catch(() => []);+ const matchingDirs = dataDirs.filter(dir => dir.startsWith(`ud-${sessionName}-`));+ if (matchingDirs.length === 0) {+ console.log(`No user data found for session '${sessionName}'.`);+ return;+ }+ for (const dir of matchingDirs) {+ const userDataDir = path.resolve(daemonProfilesDir, dir);+ for (let i = 0; i < 5; i++) {+ try {+ await fs.promises.rm(userDataDir, { recursive: true });+ console.log(`Deleted user data for session '${sessionName}'.`);+ break;+ } catch (e: any) {+ if (e.code === 'ENOENT') {+ console.log(`No user data found for session '${sessionName}'.`);+ break;+ }+ await new Promise(resolve => setTimeout(resolve, 1000));+ if (i === 4)+ throw e;+ }+ }}// Also try to delete the socket file if it exists@@ -167,7 +187,7 @@ class SessionManager {}private async _connect(sessionName: string): Promise<Session> {- const socketPath = process.env.PLAYWRIGHT_DAEMON_SOCKET_PATH || this._daemonSocketPath(sessionName);+ const socketPath = this._daemonSocketPath(sessionName);debugCli(`Connecting to daemon at ${socketPath}`);const socketExists = await fs.promises.stat(socketPath)@@ -185,17 +205,41 @@ class SessionManager {}}- if (process.env.PLAYWRIGHT_DAEMON_SOCKET_PATH)- throw new Error(`Socket path ${socketPath} does not exist`);-- const userDataDir = path.resolve(daemonSocketDir, `${sessionName}-user-data`);- const child = spawnDaemon(socketPath, userDataDir, {+ await fs.promises.mkdir(daemonProfilesDir, { recursive: true });+ const userDataDir = path.resolve(daemonProfilesDir, `ud-${sessionName}`);+ const cliPath = path.join(__dirname, '../../../cli.js');+ debugCli(`Will launch daemon process: ${cliPath}`);+ const configFile = resolveConfigFile(this._options.config);+ const configArg = configFile !== undefined ? [`--config=${configFile}`] : [];+ const headedArg = this._options.headed ? [`--daemon-headed`] : [];++ const outLog = path.join(daemonProfilesDir, 'out.log');+ const errLog = path.join(daemonProfilesDir, 'err.log');+ const out = fs.openSync(outLog, 'w');+ const err = fs.openSync(errLog, 'w');++ const child = spawn(process.execPath, [+ cliPath,+ 'run-mcp-server',+ `--daemon=${socketPath}`,+ `--daemon-data-dir=${userDataDir}`,+ ...configArg,+ ...headedArg,+ ], {detached: true,- stdio: 'ignore',+ stdio: ['ignore', out, err],cwd: process.cwd(), // Will be used as root.});child.unref();+ console.log(`<!-- Daemon for \`${sessionName}\` session started with pid ${child.pid}.`);+ if (configFile)+ console.log(`- Using config file at \`${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.`);+ console.log('-->');+// Wait for the socket to become available with retries.const maxRetries = 50;const retryDelay = 100; // ms@@ -209,7 +253,16 @@ class SessionManager {debugCli(`Retrying to connect to daemon at ${socketPath} (${i + 1}/${maxRetries})`);}}- throw new Error(`Failed to connect to daemon at ${socketPath} after ${maxRetries * retryDelay}ms`);++ const outData = await fs.promises.readFile(outLog, 'utf-8').catch(() => '');+ const errData = await fs.promises.readFile(errLog, 'utf-8').catch(() => '');++ console.error(`Failed to connect to daemon at ${socketPath} after ${maxRetries * retryDelay}ms`);+ if (outData.length)+ console.log(outData);+ if (errData.length)+ console.error(errData);+ process.exit(1);}private async _connectToSocket(sessionName: string, socketPath: string): Promise<Session> {@@ -247,8 +300,9 @@ class SessionManager {private _daemonSocketPath(sessionName: string): string {const socketName = `${sessionName}.sock`;if (os.platform() === 'win32')- return `\\\\.\\pipe\\${socketDirHash}-${socketName}`;- return path.join(daemonSocketDir, socketName);+ return `\\\\.\\pipe\\${installationDirHash}-${socketName}`;+ const socketsDir = process.env.PLAYWRIGHT_DAEMON_SOCKETS_DIR || path.join(os.tmpdir(), 'playwright-cli');+ return path.join(socketsDir, installationDirHash, socketName);}}@@ -288,13 +342,16 @@ async function handleSessionCommand(sessionManager: SessionManager, args: any):process.exit(1);}-const socketDirHash = (() => {+const installationDirHash = (() => {const hash = crypto.createHash('sha1');- hash.update(require.resolve('../../../package.json'));+ hash.update(process.env.PLAYWRIGHT_DAEMON_INSTALL_DIR || require.resolve('../../../package.json'));return hash.digest('hex').substring(0, 16);})();-const daemonSocketDir = (() => {+const daemonProfilesDir = (() => {+ if (process.env.PLAYWRIGHT_DAEMON_SESSION_DIR)+ return process.env.PLAYWRIGHT_DAEMON_SESSION_DIR;+let localCacheDir: string | undefined;if (process.platform === 'linux')localCacheDir = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');@@ -304,18 +361,17 @@ const daemonSocketDir = (() => {localCacheDir = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');if (!localCacheDir)throw new Error('Unsupported platform: ' + process.platform);- return path.join(localCacheDir, 'ms-playwright', 'daemon', socketDirHash);+ return path.join(localCacheDir, 'ms-playwright', 'daemon', installationDirHash);})();-function spawnDaemon(socketPath: string, userDataDir: string, options: SpawnOptions) {- const cliPath = path.join(__dirname, '../../../cli.js');- debugCli(`Will launch daemon process: ${cliPath}`);- return spawn(process.execPath, [cliPath, 'run-mcp-server', `--daemon=${socketPath}`, `--user-data-dir=${userDataDir}`], options);-}-export async function program(options: { version: string }) {const argv = process.argv.slice(2);- const args = require('minimist')(argv);+ const args = require('minimist')(argv, {+ boolean: ['help', 'version', 'headed'],+ });+ if (!argv.includes('--headed') && !argv.includes('--no-headed'))+ delete args.headed;+const help = require('./help.json');const commandName = args._[0];@@ -340,7 +396,7 @@ export async function program(options: { version: string }) {process.exit(1);}- const sessionManager = new SessionManager();+ const sessionManager = new SessionManager(args);if (commandName.startsWith('session')) {await handleSessionCommand(sessionManager, args);return;@@ -414,3 +470,16 @@ function sanitizeForFilePath(s: string) {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)+ return path.resolve(process.cwd(), configFile);++ try {+ if (fs.existsSync(path.resolve(process.cwd(), 'playwright-cli.json')))+ return path.resolve(process.cwd(), 'playwright-cli.json');+ } catch {+ }+ return undefined;+}diff --git a/tests/mcp/cli-fixtures.ts b/tests/mcp/cli-fixtures.tsindex 3e6536d496..1aa8889fab 100644--- a/tests/mcp/cli-fixtures.ts+++ b/tests/mcp/cli-fixtures.ts@@ -15,88 +15,85 @@*/import fs from 'fs';-import os from 'os';import path from 'path';import { spawn } from 'child_process';import { test as baseTest } from './fixtures';-import { calculateSha1 } from '../../packages/playwright-core/src/server/utils/crypto';--import type { ChildProcess } from 'child_process';export { expect } from './fixtures';export const test = baseTest.extend<{- socketPath: string;- daemon: ChildProcess;- cli: (...args: string[]) => Promise<{ output: string, error: string, snapshot?: string, attachments?: { name: string, data: Buffer | null }[] }>;+ cli: (...args: string[]) => Promise<{+ output: string,+ error: string,+ snapshot?: string,+ attachments?: { name: string, data: Buffer | null }[],+ }>;}>({- socketPath: async ({}, use, testInfo) => {- if (os.platform() === 'win32') {- const hash = calculateSha1(testInfo.outputPath());- await use(`\\\\.\\pipe\\${hash}`);- return;- }- await use(path.join(test.info().outputPath(), 'socket.sock'));- },+ cli: async ({ mcpBrowser, mcpHeadless }, use) => {+ const sessions: { name: string, pid: number }[] = [];- daemon: async ({ socketPath }, use, testInfo) => {- const userDataDir = testInfo.outputPath('user-data-dir');-- const daemonPath = path.resolve(__dirname, '../../packages/playwright/cli.js');- const daemon = spawn(process.execPath, [daemonPath, 'run-mcp-server', `--daemon=${socketPath}`, `--user-data-dir=${userDataDir}`], {- stdio: 'pipe',- cwd: testInfo.outputPath(),- });- let stderr = '';- await new Promise<void>((resolve, reject) => {- daemon.stdout.on('data', () => {});- daemon.stderr.on('data', data => {- stderr += data.toString();- if (stderr.includes('Daemon server listening'))- resolve();- });- daemon.on('close', code => {- if (code === 0)- resolve();- else- reject(new Error(`Daemon exited with code ${code}`));- });- });- await use(daemon);- daemon.kill('SIGTERM');- },-- cli: async ({ socketPath }, use, testInfo) => {await use(async (...args: string[]) => {- const cli = spawn(process.execPath, [require.resolve('../../packages/playwright/lib/mcp/terminal/cli.js'), ...args], {- cwd: testInfo.outputPath(),- stdio: 'pipe',- env: {- ...process.env,- PLAYWRIGHT_DAEMON_SOCKET_PATH: socketPath,- },- });- let stdout = '';- let stderr = '';- cli.stdout.on('data', data => { stdout += data.toString(); });- cli.stderr.on('data', data => { stderr += data.toString(); });- await new Promise<void>((resolve, reject) => {- cli.on('close', code => {- if (code === 0)- resolve();- else- reject(new Error(`CLI exited with code ${code}: ${stderr}`));- });- });- let snapshot: string | undefined;- if (stdout.includes('### Snapshot'))- snapshot = await loadSnapshot(stdout);- const attachments = loadAttachments(stdout);- return { output: stdout.trim(), error: stderr.trim(), snapshot, attachments };+ return await runCli(args, { mcpBrowser, mcpHeadless }, sessions);});++ for (const session of sessions) {+ await runCli(['session-stop', session.name], { mcpBrowser, mcpHeadless }, []);+ try {+ process.kill(session.pid, 'SIGTERM');+ } catch (e) {+ }+ }++ const daemonDir = path.join(test.info().outputDir, 'daemon');+ const userDataDirs = await fs.promises.readdir(daemonDir).catch(() => []);+ for (const dir of userDataDirs.filter(f => f.startsWith('ud-')))+ await fs.promises.rm(path.join(daemonDir, dir), { recursive: true, force: true }).catch(() => {});},});+async function runCli(args: string[], options: { mcpBrowser: string, mcpHeadless: boolean }, sessions: { name: string, pid: number }[]) {+ const testInfo = test.info();+ const cli = spawn(process.execPath, [require.resolve('../../packages/playwright/lib/mcp/terminal/cli.js'), ...args], {+ cwd: testInfo.outputPath(),+ stdio: 'pipe',+ env: {+ ...process.env,+ PLAYWRIGHT_DAEMON_INSTALL_DIR: testInfo.outputPath(),+ PLAYWRIGHT_DAEMON_SESSION_DIR: testInfo.outputPath('daemon'),+ PLAYWRIGHT_DAEMON_SOCKETS_DIR: path.join(testInfo.project.outputDir, 'daemon-sockets'),+ PLAYWRIGHT_MCP_BROWSER: options.mcpBrowser,+ PLAYWRIGHT_MCP_HEADLESS: String(options.mcpHeadless),+ },+ });+ let stdout = '';+ let stderr = '';+ cli.stdout.on('data', data => { stdout += data.toString(); });+ cli.stderr.on('data', data => { stderr += data.toString(); });+ await new Promise<void>((resolve, reject) => {+ cli.on('close', code => {+ if (code === 0)+ resolve();+ else+ reject(new Error(`CLI exited with code ${code}: ${stderr}`));+ });+ });+ let snapshot: string | undefined;+ if (stdout.includes('### Snapshot'))+ snapshot = await loadSnapshot(stdout);+ const attachments = loadAttachments(stdout);++ const matches = stdout.includes('Daemon for') ? stdout.match(/Daemon for `(.+)` session started with pid (\d+)\./) : undefined;+ const [, sessionName, pid] = matches ?? [];+ if (sessionName && pid)+ sessions.push({ name: sessionName, pid: +pid });+ return {+ output: stdout.trim(),+ error: stderr.trim(),+ snapshot,+ attachments+ };+}+export function loadAttachments(output: string) {// attachments look like md links - [Page as pdf](.playwright-cli/page-2026-01-22T23-13-56-347Z.pdf)const match = output.match(/- \[(.+)\]\((.+)\)/g);diff --git a/tests/mcp/cli.spec.ts b/tests/mcp/cli.spec.tsindex 8fd4e4469a..7445a94ffc 100644--- a/tests/mcp/cli.spec.ts+++ b/tests/mcp/cli.spec.ts@@ -14,10 +14,9 @@* limitations under the License.*/+import fs from 'fs';import { test, expect, eventsPage } from './cli-fixtures';-test.skip(({}) => process.platform === 'win32');-test.describe('help', () => {test('prints help by default', async ({ cli }) => {const { output } = await cli('--help');@@ -31,7 +30,7 @@ test.describe('help', () => {});test.describe('core', () => {- test('open', async ({ cli, daemon, server }) => {+ test('open', async ({ cli, server }) => {const { output, snapshot } = await cli('open', server.HELLO_WORLD);expect(output).toContain(`### Page- Page URL: ${server.HELLO_WORLD}@@ -40,13 +39,13 @@ test.describe('core', () => {expect(snapshot).toContain(`- generic [active] [ref=e1]: Hello, world!`);});- test('close', async ({ cli, daemon, server }) => {+ test('close', async ({ cli, server }) => {await cli('open', server.HELLO_WORLD);const { output } = await cli('close');expect(output).toContain(`No open tabs. Navigate to a URL to create one.`);});- test('click button', async ({ cli, daemon, server }) => {+ test('click button', async ({ cli, server }) => {server.setContent('/', `<button>Submit</button>`, 'text/html');const { snapshot } = await cli('open', server.PREFIX);@@ -56,7 +55,7 @@ test.describe('core', () => {expect(clickSnapshot).toBeTruthy();});- test('click link', async ({ cli, daemon, server }) => {+ test('click link', async ({ cli, server }) => {server.setContent('/', `<a href="/hello-world">Hello, world!</a>`, 'text/html');const { snapshot } = await cli('open', server.PREFIX);@@ -69,14 +68,14 @@ test.describe('core', () => {expect(clickSnapshot).toContain('Hello, world!');});- test('dblclick', async ({ cli, daemon, server }) => {+ test('dblclick', async ({ cli, server }) => {server.setContent('/', eventsPage, 'text/html');await cli('open', server.PREFIX);const { snapshot } = await cli('dblclick', 'e2');expect(snapshot).toContain('dblclick 0');});- test('type', async ({ cli, daemon, server }) => {+ test('type', async ({ cli, server }) => {server.setContent('/', `<input type=text>`, 'text/html');const { snapshot } = await cli('open', server.PREFIX);expect(snapshot).toContain(`- textbox [ref=e2]`);@@ -85,7 +84,7 @@ test.describe('core', () => {expect(typeSnapshot).toBe(`- textbox [ref=e2]`);});- test('fill', async ({ cli, daemon, server }) => {+ test('fill', async ({ cli, server }) => {server.setContent('/', `<input type=text>`, 'text/html');const { snapshot } = await cli('open', server.PREFIX);expect(snapshot).toContain(`- textbox [ref=e2]`);@@ -94,7 +93,7 @@ test.describe('core', () => {expect(fillSnapshot).toBe(`- textbox [active] [ref=e2]: Hello, world!`);});- test('hover', async ({ cli, daemon, server }) => {+ test('hover', async ({ cli, server }) => {server.setContent('/', eventsPage, 'text/html');await cli('open', server.PREFIX);await cli('hover', 'e2');@@ -102,7 +101,7 @@ test.describe('core', () => {expect(snapshot).toContain('mouse move 50 50');});- test('select', async ({ cli, daemon, server }) => {+ test('select', async ({ cli, server }) => {server.setContent('/', `<select><option value="1">One</option><option value="2">Two</option></select>`, 'text/html');await cli('open', server.PREFIX);await cli('select', 'e2', 'Two');@@ -110,42 +109,44 @@ test.describe('core', () => {expect(snapshot).toContain('- option "Two" [selected]');});- test('check', async ({ cli, daemon, server }) => {+ test('check', async ({ cli, server, mcpBrowser }) => {+ const active = mcpBrowser === 'webkit' && process.platform === 'darwin' ? '' : '[active] ';server.setContent('/', `<input type="checkbox">`, 'text/html');await cli('open', server.PREFIX);await cli('check', 'e2');const { snapshot } = await cli('snapshot');- expect(snapshot).toContain('- checkbox [checked] [active] [ref=e2]');+ expect(snapshot).toContain(`- checkbox [checked] ${active}[ref=e2]`);});- test('uncheck', async ({ cli, daemon, server }) => {+ test('uncheck', async ({ cli, server, mcpBrowser }) => {+ const active = mcpBrowser === 'webkit' && process.platform === 'darwin' ? '' : '[active] ';server.setContent('/', `<input type="checkbox" checked>`, 'text/html');await cli('open', server.PREFIX);await cli('uncheck', 'e2');const { snapshot } = await cli('snapshot');- expect(snapshot).toContain('- checkbox [active] [ref=e2]');+ expect(snapshot).toContain(`- checkbox ${active}[ref=e2]`);});- test('eval', async ({ cli, daemon, server }) => {+ test('eval', async ({ cli, server }) => {await cli('open', server.HELLO_WORLD);const { output } = await cli('eval', '() => document.title');expect(output).toContain('"Title"');});- test('eval no arrow', async ({ cli, daemon, server }) => {+ test('eval no arrow', async ({ cli, server }) => {await cli('open', server.HELLO_WORLD);const { output } = await cli('eval', 'document.title');expect(output).toContain('"Title"');});- test('eval <ref>', async ({ cli, daemon, server }) => {+ test('eval <ref>', async ({ cli, server }) => {server.setContent('/', `<button>Submit</button>`, 'text/html');await cli('open', server.PREFIX);const { output } = await cli('eval', 'element => element.nodeName', 'e2');expect(output).toContain('"BUTTON"');});- test('dialog-accept', async ({ cli, daemon, server }) => {+ test('dialog-accept', async ({ cli, server }) => {server.setContent('/', `<button onclick="alert('MyAlert')">Button</button>`, 'text/html');await cli('open', server.PREFIX);const { output } = await cli('click', 'e2');@@ -156,7 +157,7 @@ test.describe('core', () => {expect(snapshot).not.toContain('MyAlert');});- test('dialog-dismiss', async ({ cli, daemon, server }) => {+ test('dialog-dismiss', async ({ cli, server }) => {server.setContent('/', `<button onclick="alert('MyAlert')">Button</button>`, 'text/html');await cli('open', server.PREFIX);const { output } = await cli('click', 'e2');@@ -166,7 +167,7 @@ test.describe('core', () => {expect(snapshot).not.toContain('MyAlert');});- test('dialog-accept <prompt>', async ({ cli, daemon, server }) => {+ test('dialog-accept <prompt>', async ({ cli, server }) => {server.setContent('/', `<button onclick="document.body.textContent = prompt('MyAlert')">Button</button>`, 'text/html');await cli('open', server.PREFIX);await cli('click', 'e2');@@ -175,7 +176,7 @@ test.describe('core', () => {expect(snapshot).toContain('my reply');});- test('resize', async ({ cli, daemon, server }) => {+ test('resize', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('resize', '480', '320');const { output } = await cli('eval', '() => window.innerWidth + "x" + window.innerHeight');@@ -184,7 +185,7 @@ test.describe('core', () => {});test.describe('navigation', () => {- test('go-back', async ({ cli, daemon, server }) => {+ test('go-back', async ({ cli, server }) => {await cli('open', server.HELLO_WORLD);await cli('open', server.PREFIX);const { output } = await cli('go-back');@@ -193,7 +194,7 @@ test.describe('navigation', () => {- Page Title: Title`);});- test('go-forward', async ({ cli, daemon, server }) => {+ test('go-forward', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('open', server.HELLO_WORLD);await cli('go-back');@@ -205,7 +206,7 @@ test.describe('navigation', () => {});test.describe('keyboard', () => {- test('press', async ({ cli, daemon, server }) => {+ test('press', async ({ cli, server }) => {server.setContent('/', `<input type=text>`, 'text/html');await cli('open', server.PREFIX);await cli('click', 'e2');@@ -214,7 +215,7 @@ test.describe('keyboard', () => {expect(snapshot).toBe(`- textbox [active] [ref=e2]: h`);});- test('keydown keyup', async ({ cli, daemon, server }) => {+ test('keydown keyup', async ({ cli, server }) => {server.setContent('/', `<input type=text>`, 'text/html');await cli('open', server.PREFIX);await cli('click', 'e2');@@ -226,7 +227,7 @@ test.describe('keyboard', () => {});test.describe('mouse', () => {- test('mousemove', async ({ cli, daemon, server }) => {+ test('mousemove', async ({ cli, server }) => {server.setContent('/', eventsPage, 'text/html');await cli('open', server.PREFIX);await cli('mousemove', '45', '35');@@ -234,7 +235,7 @@ test.describe('mouse', () => {expect(snapshot).toContain('mouse move 45 35');});- test('mousedown mouseup', async ({ cli, daemon, server }) => {+ test('mousedown mouseup', async ({ cli, server }) => {server.setContent('/', eventsPage, 'text/html');await cli('open', server.PREFIX);await cli('mousemove', '45', '35');@@ -245,9 +246,14 @@ test.describe('mouse', () => {expect(snapshot).toContain('mouse up');});- test('mousewheel', async ({ cli, daemon, server }) => {+ test('mousewheel', async ({ cli, server }) => {server.setContent('/', eventsPage, 'text/html');await cli('open', server.PREFIX);+ // click to focus+ await cli('mousemove', '50', '50');+ await cli('mousedown');+ await cli('mouseup');+await cli('mousewheel', '10', '5');const { snapshot } = await cli('snapshot');expect(snapshot).toContain('wheel 5 10');@@ -256,14 +262,14 @@ test.describe('mouse', () => {test.describe('save as', () => {- test('screenshot', async ({ cli, daemon, server, mcpBrowser }) => {+ test('screenshot', async ({ cli, server, mcpBrowser }) => {await cli('open', server.HELLO_WORLD);const { attachments } = await cli('screenshot');expect(attachments[0].name).toEqual('Screenshot of viewport');expect(attachments[0].data).toEqual(expect.any(Buffer));});- test('screenshot <ref>', async ({ cli, daemon, server, mcpBrowser }) => {+ test('screenshot <ref>', async ({ cli, server, mcpBrowser }) => {server.setContent('/', `<div id="square" style="width: 100px; height: 100px; background-color: red;"></div>`, 'text/html');await cli('open', server.PREFIX);const { attachments } = await cli('screenshot', 'e2');@@ -271,14 +277,14 @@ test.describe('save as', () => {expect(attachments[0].data).toEqual(expect.any(Buffer));});- test('screenshot --full-page', async ({ cli, daemon, server, mcpBrowser }) => {+ test('screenshot --full-page', async ({ cli, server, mcpBrowser }) => {await cli('open', server.HELLO_WORLD);const { attachments } = await cli('screenshot', '--full-page');expect(attachments[0].name).toEqual('Screenshot of full page');expect(attachments[0].data).toEqual(expect.any(Buffer));});- test('pdf', async ({ cli, daemon, server, mcpBrowser }) => {+ test('pdf', async ({ cli, server, mcpBrowser }) => {test.skip(mcpBrowser !== 'chromium' && mcpBrowser !== 'chrome', 'PDF is only supported in Chromium and Chrome');await cli('open', server.HELLO_WORLD);const { attachments } = await cli('pdf');@@ -288,7 +294,7 @@ test.describe('save as', () => {});test.describe('devtools', () => {- test('console', async ({ cli, daemon, server }) => {+ test('console', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('eval', 'console.log("Hello, world!")');const { attachments } = await cli('console');@@ -296,7 +302,7 @@ test.describe('devtools', () => {expect(attachments[0].data.toString()).toContain('Hello, world!');});- test('console error', async ({ cli, daemon, server }) => {+ test('console error', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('eval', 'console.log("log-level")');await cli('eval', 'console.error("error-level")');@@ -306,7 +312,7 @@ test.describe('devtools', () => {expect(attachments[0].data.toString()).toContain('error-level');});- test('console --clear', async ({ cli, daemon, server }) => {+ test('console --clear', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('eval', 'console.log("log-level")');await cli('console', '--clear');@@ -315,7 +321,7 @@ test.describe('devtools', () => {expect(attachments[0].data.toString()).not.toContain('log-level');});- test('network', async ({ cli, daemon, server }) => {+ test('network', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('eval', '() => fetch("/hello-world")');const { attachments } = await cli('network');@@ -324,14 +330,14 @@ test.describe('devtools', () => {expect(attachments[0].data.toString()).toContain(`[GET] ${`${server.PREFIX}/hello-world`} => [200] OK`);});- test('network --static', async ({ cli, daemon, server }) => {+ test('network --static', async ({ cli, server }) => {await cli('open', server.PREFIX);const { attachments } = await cli('network', '--static');expect(attachments[0].name).toEqual('Network');expect(attachments[0].data.toString()).toContain(`[GET] ${`${server.PREFIX}/`} => [200] OK`);});- test('network --clear', async ({ cli, daemon, server }) => {+ test('network --clear', async ({ cli, server }) => {await cli('open', server.PREFIX);await cli('eval', '() => fetch("/hello-world")');await cli('network', '--clear');@@ -340,13 +346,13 @@ test.describe('devtools', () => {expect(attachments[0].data.toString()).not.toContain(`[GET] ${`${server.PREFIX}/hello-world`} => [200] OK`);});- test('run-code', async ({ cli, daemon, server }) => {+ test('run-code', async ({ cli, server }) => {await cli('open', server.HELLO_WORLD);const { output } = await cli('run-code', '() => page.title()');expect(output).toContain('"Title"');});- test('tracing-start-stop', async ({ cli, daemon, server }) => {+ test('tracing-start-stop', async ({ cli, server }) => {await cli('open', server.HELLO_WORLD);const { output } = await cli('tracing-start');expect(output).toContain('Tracing started, saving to');@@ -355,3 +361,120 @@ test.describe('devtools', () => {expect(tracingStopOutput).toContain('Tracing stopped.');});});++test.describe('config', () => {+ test('user-data-dir', async ({ cli, server }, testInfo) => {+ const config = {+ browser: {+ userDataDir: testInfo.outputPath('my-data-dir'),+ },+ };+ await fs.promises.writeFile(testInfo.outputPath('config.json'), JSON.stringify(config, null, 2));+ await cli('open', `--config=config.json`, server.PREFIX);+ expect(fs.existsSync(testInfo.outputPath('my-data-dir'))).toBe(true);+ });++ test('context options', async ({ cli, server }, testInfo) => {+ const config = {+ browser: {+ contextOptions: {+ viewport: { width: 800, height: 600 },+ },+ },+ };+ await fs.promises.writeFile(testInfo.outputPath('playwright-cli.json'), JSON.stringify(config, null, 2));+ await cli('open', server.PREFIX);+ const { output } = await cli('eval', 'window.innerWidth + "x" + window.innerHeight');+ expect(output).toContain('800x600');+ });++ test('isolated', async ({ cli, server }, testInfo) => {+ const config = {+ browser: {+ isolated: true,+ },+ };+ await fs.promises.writeFile(testInfo.outputPath('playwright-cli.json'), JSON.stringify(config, null, 2));+ await cli('open', server.PREFIX);+ expect(fs.existsSync(testInfo.outputPath('daemon', 'default-user-data'))).toBe(false);+ });+});++test.describe('session', () => {+ test('session-list', async ({ cli, server }) => {+ const { output: emptyOutput } = await cli('session-list');+ expect(emptyOutput).toContain('Sessions:');+ expect(emptyOutput).toContain('(no sessions)');++ await cli('open', server.HELLO_WORLD);++ const { output: listOutput } = await cli('session-list');+ expect(listOutput).toContain('Sessions:');+ expect(listOutput).toContain('default (live)');+ });++ test('session-stop', async ({ cli, server }) => {+ await cli('open', server.HELLO_WORLD);++ const { output } = await cli('session-stop');+ expect(output).toContain(`Session 'default' stopped.`);++ const { output: listOutput } = await cli('session-list');+ expect(listOutput).not.toContain('(live)');+ });++ test('session-stop named session', async ({ cli, server }) => {+ await cli('open', '--session=mysession', server.HELLO_WORLD);++ const { output } = await cli('session-stop', 'mysession');+ expect(output).toContain(`Session 'mysession' stopped.`);+ });++ test('session-stop non-running session', async ({ cli }) => {+ const { output } = await cli('session-stop', 'nonexistent');+ expect(output).toContain(`Session 'nonexistent' is not running.`);+ });++ test('session-stop-all', async ({ cli, server }) => {+ await cli('open', '--session=session1', server.HELLO_WORLD);+ await cli('open', '--session=session2', server.HELLO_WORLD);++ const { output: listBefore } = await cli('session-list');+ expect(listBefore).toContain('session1 (live)');+ expect(listBefore).toContain('session2 (live)');++ await cli('session-stop-all');++ const { output: listAfter } = await cli('session-list');+ expect(listAfter).not.toContain('(live)');+ });++ test('session-delete', async ({ cli, server, mcpBrowser }, testInfo) => {+ await cli('open', server.HELLO_WORLD);++ const dataDir = testInfo.outputPath('daemon', 'ud-default-' + mcpBrowser);+ expect(fs.existsSync(dataDir)).toBe(true);++ const { output } = await cli('session-delete');+ expect(output).toContain(`Deleted user data for session 'default'.`);++ expect(fs.existsSync(dataDir)).toBe(false);+ });++ test('session-delete named session', async ({ cli, server, mcpBrowser }, testInfo) => {+ await cli('open', '--session=mysession', server.HELLO_WORLD);++ const dataDir = testInfo.outputPath('daemon', 'ud-mysession-' + mcpBrowser);+ expect(fs.existsSync(dataDir)).toBe(true);++ const { output } = await cli('session-delete', 'mysession');+ expect(output).toContain(`Deleted user data for session 'mysession'.`);++ expect(fs.existsSync(dataDir)).toBe(false);+ });++ test('session-delete non-existent session', async ({ cli }) => {+ const { output } = await cli('session-delete', 'nonexistent');+ expect(output).toContain(`No user data found for session 'nonexistent'.`);+ });+});diff --git a/tests/mcp/generator.spec.ts b/tests/mcp/generator.spec.tsindex 5504c00dd5..9e593fd0ff 100644--- a/tests/mcp/generator.spec.ts+++ b/tests/mcp/generator.spec.ts@@ -44,7 +44,6 @@ test('generator tools intent', async ({ startClient }) => {expect(toolsWithIntent).toContain('browser_mouse_move_xy');expect(toolsWithIntent).toContain('browser_navigate');expect(toolsWithIntent).toContain('browser_navigate_back');- expect(toolsWithIntent).toContain('browser_open');expect(toolsWithIntent).toContain('browser_press_key');expect(toolsWithIntent).toContain('browser_press_sequentially');expect(toolsWithIntent).toContain('browser_resize');diff --git a/tests/mcp/snapshot-mode.spec.ts b/tests/mcp/snapshot-mode.spec.tsindex 65be63d38a..46a3265d9d 100644--- a/tests/mcp/snapshot-mode.spec.ts+++ b/tests/mcp/snapshot-mode.spec.ts@@ -55,7 +55,8 @@ test('should respect --snapshot-mode=full', async ({ startClient, server }) => {});});-test('should respect --snapshot-mode=incremental', async ({ startClient, server }) => {+test('should respect --snapshot-mode=incremental', async ({ startClient, server, mcpBrowser }) => {+ test.fixme(mcpBrowser === 'webkit', 'Active handling?');server.setContent('/', `<button>Button 1</button>`, 'text/html');const { client } = await startClient({diff --git a/utils/build/build.js b/utils/build/build.jsindex 67ec71c8b6..6a71fa3225 100644--- a/utils/build/build.js+++ b/utils/build/build.js@@ -555,6 +555,7 @@ for (const webPackage of ['html-reporter', 'recorder', 'trace-viewer']) {onChanges.push({inputs: ['packages/playwright/src/mcp/terminal/commands.ts',+ 'packages/playwright/src/mcp/terminal/helpGenerator.ts','utils/generate_cli_help.js',],script: 'utils/generate_cli_help.js',