Skip to content

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

chore(cli): remove the notion of stopped session (#39143)

Pavel Feldman08752e9a9be0
diff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.ts
index e9dac222a3..c66bdbf3d8 100644
--- a/packages/playwright/src/mcp/terminal/program.ts
+++ b/packages/playwright/src/mcp/terminal/program.ts
@@ -95,14 +95,7 @@ to restart the session daemon.`);
return;
}
- await this._send('stop').catch(e => {
- if (e.message !== 'Session closed')
- throw e;
- });
- this.disconnect();
-
- if (os.platform() !== 'win32')
- await fs.promises.unlink(this._config.socketPath).catch(() => {});
+ await this._stopDaemon();
console.log(`Session '${this.name}' stopped.`);
}
@@ -215,11 +208,15 @@ to restart the session daemon.`);
}
}
+ private _sessionConfigFile() {
+ return path.resolve(this._clientInfo.daemonProfilesDir, `${this.name}.session`);
+ }
+
private async _startDaemon(): Promise<net.Socket> {
await fs.promises.mkdir(this._clientInfo.daemonProfilesDir, { recursive: true });
const cliPath = path.join(__dirname, '../../../cli.js');
- const sessionConfigFile = path.resolve(this._clientInfo.daemonProfilesDir, `${this.name}.session`);
+ const sessionConfigFile = this._sessionConfigFile();
this._config.version = this._clientInfo.version;
await fs.promises.writeFile(sessionConfigFile, JSON.stringify(this._config, null, 2));
@@ -278,6 +275,21 @@ to restart the session daemon.`);
console.error(errData);
process.exit(1);
}
+
+ private async _stopDaemon(): Promise<void> {
+ let error: Error | undefined;
+ await this._send('stop').catch(e => { error = e; });
+ this.disconnect();
+ await this.deleteSession();
+ if (error && !error?.message?.includes('Session closed'))
+ throw error;
+ }
+
+ async deleteSession() {
+ if (os.platform() !== 'win32')
+ await fs.promises.unlink(this._config.socketPath).catch(() => {});
+ await fs.promises.rm(this._sessionConfigFile()).catch(() => {});
+ }
}
class SessionManager {
@@ -491,9 +503,14 @@ export async function program(packageLocation: string) {
const sessions = sessionManager.sessions;
console.log('Sessions:');
for (const session of sessions.values()) {
- const liveMarker = await session.canConnect() ? `[running] ` : '[stopped] ';
- const restartMarker = !session.isCompatible() ? ` - v${session.config().version}, needs restart` : '';
- console.log(` ${liveMarker}${session.name}${restartMarker}`);
+ 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}`);
+ }
}
if (sessions.size === 0)
console.log(' (no sessions)');
diff --git a/tests/mcp/cli-isolated.spec.ts b/tests/mcp/cli-isolated.spec.ts
index 3c160d7b5c..cb134f4104 100644
--- a/tests/mcp/cli-isolated.spec.ts
+++ b/tests/mcp/cli-isolated.spec.ts
@@ -34,7 +34,7 @@ test('should not save user data by default (in-memory mode)', async ({ cli, serv
const { output: listOutput } = await cli('session-list');
expect(listOutput).toContain('Sessions:');
- expect(listOutput).toContain(' [running] default');
+ expect(listOutput).toContain(' default');
});
test('should save user data with --persistent flag', async ({ cli, server, mcpBrowser }, testInfo) => {
diff --git a/tests/mcp/cli-session.spec.ts b/tests/mcp/cli-session.spec.ts
index 15808ad065..9603e2cb9d 100644
--- a/tests/mcp/cli-session.spec.ts
+++ b/tests/mcp/cli-session.spec.ts
@@ -26,7 +26,7 @@ test('session-list', async ({ cli, server }) => {
const { output: listOutput } = await cli('session-list');
expect(listOutput).toContain('Sessions:');
- expect(listOutput).toContain(' [running] default');
+ expect(listOutput).toContain(' default');
});
test('close', async ({ cli, server }) => {
@@ -36,7 +36,7 @@ test('close', async ({ cli, server }) => {
expect(output).toContain(`Session 'default' stopped.`);
const { output: listOutput } = await cli('session-list');
- expect(listOutput).toContain('[stopped] default');
+ expect(listOutput).toContain('(no sessions)');
});
test('close named session', async ({ cli, server }) => {
@@ -56,13 +56,13 @@ test('session-close-all', async ({ cli, server }) => {
await cli('--session=session2', 'open', server.HELLO_WORLD);
const { output: listBefore } = await cli('session-list');
- expect(listBefore).toContain('[running] session1');
- expect(listBefore).toContain('[running] session2');
+ expect(listBefore).toContain('session1');
+ expect(listBefore).toContain('session2');
await cli('session-close-all');
const { output: listAfter } = await cli('session-list');
- expect(listAfter).not.toContain('[running]');
+ expect(listAfter).not.toContain('session1');
});
test('delete-data', async ({ cli, server, mcpBrowser }, testInfo) => {
@@ -98,12 +98,15 @@ test('session stops when browser exits', async ({ cli, server }) => {
await cli('open', server.HELLO_WORLD);
const { output: listBefore } = await cli('session-list');
- expect(listBefore).toContain('[running] default');
+ expect(listBefore).toContain('default');
// Close the browser - this will cause the daemon to exit so the command may fail
await cli('run-code', '() => page.context().browser().close()').catch(() => {});
- await expect.poll(() => cli('session-list').then(r => r.output)).toContain('[stopped]');
+ 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)');
});
test('session reopen with different config', async ({ cli, server }, testInfo) => {