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(cli): keep stopped persistent sessions in the session list (#39153)
Pavel Feldman605d93d732c5
diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.tsindex 291495ffe0..1f3d35d12f 100644--- a/packages/playwright/src/mcp/terminal/program.ts+++ b/packages/playwright/src/mcp/terminal/program.ts@@ -283,15 +283,17 @@ to restart the session daemon.`);private async _stopDaemon(): Promise<void> {let error: Error | undefined;await this._send('stop').catch(e => { error = e; });+ if (os.platform() !== 'win32')+ await fs.promises.unlink(this._config.socketPath).catch(() => {});+this.disconnect();- await this.deleteSession();+ if (!this._config.cli.persistent)+ await this.deleteSessionConfig();if (error && !error?.message?.includes('Session closed'))throw error;}- async deleteSession() {- if (os.platform() !== 'win32')- await fs.promises.unlink(this._config.socketPath).catch(() => {});+ async deleteSessionConfig() {await fs.promises.rm(this._sessionConfigFile()).catch(() => {});}}@@ -663,28 +665,14 @@ async function killAllDaemons(): Promise<void> {async function listSessions(sessionManager: SessionManager): Promise<void> {const sessions = sessionManager.sessions;console.log('Sessions:');- for (const session of sessions.values()) {- const canConnect = await session.canConnect();- if (!canConnect) {- console.log(` ${session.name} is stale, removing`);- await session.deleteSession();- } else {- const restartMarker = !session.isCompatible() ? ` - v${session.config().version}, please reopen` : '';- console.log(` ${session.name}${restartMarker}`);- const config = session.config();- configToFormattedArgs(config.cli).forEach(arg => console.log(` ${arg}`));- }- }- if (sessions.size === 0)- console.log(' (no sessions)');+ await gcAndPrintSessions([...sessions.values()]);}async function listAllSessions(clientInfo: ClientInfo): Promise<void> {const hashes = await fs.promises.readdir(baseDaemonDir).catch(() => []);// Group sessions by workspace folder- const sessionsByWorkspace = new Map<string, { name: string, config: SessionConfig, canConnect: boolean, isCompatible: boolean }[]>();-+ const sessionsByWorkspace = new Map<string, Session[]>();for (const hash of hashes) {const hashDir = path.join(baseDaemonDir, hash);const stat = await fs.promises.stat(hashDir).catch(() => null);@@ -699,14 +687,10 @@ async function listAllSessions(clientInfo: ClientInfo): Promise<void> {const sessionName = path.basename(file, '.session');const sessionConfig = await fs.promises.readFile(path.join(hashDir, file), 'utf-8').then(data => JSON.parse(data)) as SessionConfig;const session = new Session(clientInfo, sessionName, sessionConfig);- const canConnect = await session.canConnect();- const isCompatible = session.isCompatible();-- // Use workspace folder from config, or empty string if not set (installation folder case)- const workspaceKey = sessionConfig.workspaceDir || '';+ const workspaceKey = sessionConfig.workspaceDir || '<global>';if (!sessionsByWorkspace.has(workspaceKey))sessionsByWorkspace.set(workspaceKey, []);- sessionsByWorkspace.get(workspaceKey)!.push({ name: sessionName, config: sessionConfig, canConnect, isCompatible });+ sessionsByWorkspace.get(workspaceKey)!.push(session);} catch {}}@@ -717,30 +701,52 @@ async function listAllSessions(clientInfo: ClientInfo): Promise<void> {return;}- // Sort workspace keys: empty string (no workspace) last, others alphabetically- const sortedWorkspaces = [...sessionsByWorkspace.keys()].sort((a, b) => {- if (a === '' && b !== '')- return 1;- if (a !== '' && b === '')- return -1;- return a.localeCompare(b);- });+ const sortedWorkspaces = [...sessionsByWorkspace.keys()].sort();for (const workspace of sortedWorkspaces) {const sessions = sessionsByWorkspace.get(workspace)!;- // Only print workspace folder if it's set- if (workspace)- console.log(`${workspace}:`);- for (const { name, config, canConnect, isCompatible } of sessions) {- if (!canConnect) {- console.log(` ${name} (stale)`);- } else {- const restartMarker = !isCompatible ? ` - v${config.version}, please reopen` : '';- console.log(` ${name}${restartMarker}`);- configToFormattedArgs(config.cli).forEach(arg => console.log(` ${arg}`));- }+ console.log(`${workspace}:`);+ await gcAndPrintSessions(sessions);+ }+}++async function gcAndPrintSessions(sessions: Session[]) {+ const running: Session[] = [];+ const stopped: Session[] = [];++ for (const session of sessions.values()) {+ const canConnect = await session.canConnect();+ if (canConnect) {+ running.push(session);+ } else {+ if (session.config().cli.persistent)+ stopped.push(session);+ else+ await session.deleteSessionConfig();}}++ for (const session of running)+ console.log(await renderSessionStatus(session));+ for (const session of stopped)+ console.log(await renderSessionStatus(session));++ if (running.length === 0 && stopped.length === 0)+ console.log(' (no sessions)');++}++async function renderSessionStatus(session: Session) {+ const text: string[] = [];+ const config = session.config();+ const canConnect = await session.canConnect();+ const isPersistent = config.cli.persistent;+ const statusMarker = canConnect ? '[running]' : '[stopped]';+ const persistentMarker = isPersistent ? ' [persistent]' : '';+ const restartMarker = canConnect && !session.isCompatible() ? ` - v${config.version}, please reopen` : '';+ text.push(` ${session.name} ${statusMarker}${persistentMarker}${restartMarker}`);+ configToFormattedArgs(config.cli).forEach(arg => text.push(` ${arg}`));+ return text.join('\n');}function formatWithGap(prefix: string, text: string, threshold: number = 40) {diff --git a/tests/mcp/cli-isolated.spec.ts b/tests/mcp/cli-isolated.spec.tsindex 41ca657b3f..e1d3646091 100644--- a/tests/mcp/cli-isolated.spec.ts+++ b/tests/mcp/cli-isolated.spec.ts@@ -28,6 +28,7 @@ test('should not save user data by default (in-memory mode)', async ({ cli, servsocketPath: expect.any(String),userDataDirPrefix: expect.any(String),version: expect.any(String),+ workspaceDir: testInfo.outputPath(),});const { output: listOutput } = await cli('session-list');@@ -47,6 +48,7 @@ test('should save user data with --persistent flag', async ({ cli, server, mcpBrsocketPath: expect.any(String),userDataDirPrefix: expect.any(String),version: expect.any(String),+ workspaceDir: testInfo.outputPath(),});});@@ -63,5 +65,6 @@ test('should use custom user data dir with --profile=<dir>', async ({ cli, servesocketPath: expect.any(String),userDataDirPrefix: expect.any(String),version: expect.any(String),+ workspaceDir: testInfo.outputPath(),});});diff --git a/tests/mcp/cli-session.spec.ts b/tests/mcp/cli-session.spec.tsindex 90b88f17ab..ce6daaac6b 100644--- a/tests/mcp/cli-session.spec.ts+++ b/tests/mcp/cli-session.spec.ts@@ -52,6 +52,18 @@ test('close non-running session', async ({ cli }) => {expect(output).toContain(`Session 'nonexistent' is not running.`);});+test('stopped persistent session shows in session-list', async ({ cli, server }) => {+ await cli('open', server.HELLO_WORLD, '--persistent');++ const { output: listBefore } = await cli('session-list');+ expect(listBefore).toContain('default [running] [persistent]');++ await cli('close');++ const { output: listAfter } = await cli('session-list');+ expect(listAfter).toContain('default [stopped] [persistent]');+});+test('session-close-all', async ({ cli, server }) => {await cli('--session=session1', 'open', server.HELLO_WORLD);await cli('--session=session2', 'open', server.HELLO_WORLD);@@ -104,7 +116,6 @@ test('session stops when browser exits', async ({ cli, server }) => {// Close the browser - this will cause the daemon to exit so the command may failawait cli('run-code', '() => page.context().browser().close()').catch(() => {});- await expect.poll(() => cli('session-list').then(r => r.output)).toContain('default is stale, removing');await cli('close');const { output: listAfter } = await cli('session-list');expect(listAfter).toContain('(no sessions)');@@ -157,7 +168,7 @@ test('workspace isolation - sessions in different workspaces are isolated', asynconst { output: list1 } = await cli('session-list', { cwd: workspace1 });expect(list1).toContain('default');const { output: list2 } = await cli('session-list', { cwd: workspace2 });- expect(list2).toContain('default');+ expect(list2).toContain(' default');await cli('close', { cwd: workspace1 });