chore: allow reusing tab over cdp (#170)

Fixes https://github.com/microsoft/playwright-mcp/issues/164
This commit is contained in:
Pavel Feldman
2025-04-14 16:39:58 -07:00
committed by GitHub
parent e729494bd9
commit 606b898a71
7 changed files with 63 additions and 21 deletions

View File

@@ -20,6 +20,7 @@ import { chromium } from 'playwright';
import { test as baseTest, expect as baseExpect } from '@playwright/test';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { spawn } from 'child_process';
type Fixtures = {
client: Client;
@@ -68,12 +69,25 @@ export const test = baseTest.extend<Fixtures>({
cdpEndpoint: async ({ }, use, testInfo) => {
const port = 3200 + (+process.env.TEST_PARALLEL_INDEX!);
const browser = await chromium.launchPersistentContext(testInfo.outputPath('user-data-dir'), {
channel: 'chrome',
args: [`--remote-debugging-port=${port}`],
const executablePath = chromium.executablePath();
const browserProcess = spawn(executablePath, [
`--user-data-dir=${testInfo.outputPath('user-data-dir')}`,
`--remote-debugging-port=${port}`,
`--no-first-run`,
`--no-sandbox`,
`--headless`,
`data:text/html,hello world`,
], {
stdio: 'pipe',
});
await new Promise<void>(resolve => {
browserProcess.stderr.on('data', data => {
if (data.toString().includes('DevTools listening on '))
resolve();
});
});
await use(`http://localhost:${port}`);
await browser.close();
browserProcess.kill();
},
});