Skip to content

Commit 164b57e

Browse files
authored
chore: pick session to attach from env (microsoft#39650)
1 parent 75b14a3 commit 164b57e

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

packages/playwright-core/src/tools/cli-client/program.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,26 @@ async function listSessions(registry: Registry, clientInfo: ClientInfo, all: boo
344344
return;
345345
}
346346

347+
const runningSessions = new Set<string>();
347348
if (entries.size)
348349
console.log('### Browsers');
349350
for (const [workspace, list] of entries)
350-
await gcAndPrintSessions(clientInfo, list.map(entry => new Session(entry)), `${path.relative(process.cwd(), workspace) || '/'}:`);
351+
await gcAndPrintSessions(clientInfo, list.map(entry => new Session(entry)), `${path.relative(process.cwd(), workspace) || '/'}:`, runningSessions);
352+
353+
// Filter out server entries that already have an attached session.
354+
const filteredServerEntries = new Map<string, BrowserDescriptor[]>();
355+
for (const [workspace, list] of serverEntries) {
356+
const unattached = list.filter(d => !runningSessions.has(d.title));
357+
if (unattached.length)
358+
filteredServerEntries.set(workspace, unattached);
359+
}
351360

352-
if (serverEntries.size) {
361+
if (filteredServerEntries.size) {
353362
if (entries.size)
354363
console.log('');
355364
console.log('### Browser servers available for attach');
356365
}
357-
for (const [workspace, list] of serverEntries)
366+
for (const [workspace, list] of filteredServerEntries)
358367
await gcAndPrintBrowserSessions(workspace, list);
359368
} else {
360369
console.log('### Browsers');
@@ -363,14 +372,15 @@ async function listSessions(registry: Registry, clientInfo: ClientInfo, all: boo
363372
}
364373
}
365374

366-
async function gcAndPrintSessions(clientInfo: ClientInfo, sessions: Session[], header?: string) {
375+
async function gcAndPrintSessions(clientInfo: ClientInfo, sessions: Session[], header?: string, runningSessions?: Set<string>) {
367376
const running: Session[] = [];
368377
const stopped: Session[] = [];
369378

370379
for (const session of sessions) {
371380
const canConnect = await session.canConnect();
372381
if (canConnect) {
373382
running.push(session);
383+
runningSessions?.add(session.name);
374384
} else {
375385
if (session.config.cli.persistent)
376386
stopped.push(session);

packages/playwright-core/src/tools/cli-client/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ export class Session {
147147
args.push(`--profile=${cliArgs.profile}`);
148148
if (cliArgs.config)
149149
args.push(`--config=${cliArgs.config}`);
150-
if (cliArgs.attach)
151-
args.push(`--attach=${cliArgs.attach}`);
150+
if (cliArgs.attach || process.env.PLAYWRIGHT_CLI_SESSION)
151+
args.push(`--attach=${cliArgs.attach || process.env.PLAYWRIGHT_CLI_SESSION}`);
152152

153153
const child = spawn(process.execPath, args, {
154154
detached: true,

tests/mcp/cli-session.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,26 @@ workspace1:
332332
expect(error).toContain('Error: unable to connect to a browser that does not have any contexts');
333333
});
334334

335+
test('attach via PLAYWRIGHT_CLI_SESSION env', async ({ cli, mcpBrowser }) => {
336+
const browserName = mcpBrowser.replace('chrome', 'chromium');
337+
await using browser = await playwright[browserName].launch({ headless: true });
338+
const page = await browser.newPage();
339+
await page.setContent('<title>Env Page</title><h1>Hello from env</h1>');
340+
await (browser as any)._register('foobar', { workspaceDir: 'workspace1' });
341+
const { output: openOutput, snapshot } = await cli('open', { env: { PLAYWRIGHT_CLI_SESSION: 'foobar' } });
342+
expect(openOutput).toContain('### Browser `foobar` opened with pid');
343+
expect(openOutput).toContain('Env Page');
344+
expect(snapshot).toContain('Hello from env');
345+
const { output: listOutput } = await cli('list', '--all');
346+
expect(listOutput).toBe(`### Browsers
347+
/:
348+
- foobar:
349+
- status: open
350+
- browser-type: ${mcpBrowser.replace('chrome', 'chromium')}
351+
- user-data-dir: <in-memory>
352+
- headed: true`);
353+
});
354+
335355
test('detach from browser server', async ({ cli, mcpBrowser }) => {
336356
const browserName = mcpBrowser.replace('chrome', 'chromium');
337357
await using browser = await playwright[browserName].launch({ headless: true });

0 commit comments

Comments
 (0)