Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions tests/mcp/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type StartClient = (options?: {
rootsResponseDelay?: number,
env?: NodeJS.ProcessEnv,
noTimeoutForTest?: boolean,
}) => Promise<{ client: Client, stderr: () => string }>;
}) => Promise<{ client: Client, stderr: () => string, closeTransport: () => Promise<void> }>;


type TestFixtures = {
Expand Down Expand Up @@ -95,6 +95,7 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
startClient: async ({ mcpHeadless, mcpBrowser, mcpArgs, mcpServerType, mcpCaps }, use, testInfo) => {
const configDir = path.dirname(test.info().config.configFile!);
const clients: Client[] = [];
const disconnectedClients = new Set<Client>();

await use(async options => {
let args: string[] = mcpArgs ?? [];
Expand Down Expand Up @@ -151,10 +152,24 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
clients.push(client);
await client.connect(transport);
await client.ping();
return { client, stderr: () => stderrBuffer };
return {
client,
stderr: () => stderrBuffer,
closeTransport: async () => {
disconnectedClients.add(client);
await transport.close();
},
};
});

await Promise.all(clients.map(client => client.close()));
await Promise.all(clients.map(async client => {
try {
await client.close();
} catch (e) {
if (!disconnectedClients.has(client))
throw e;
}
}));
},

wsEndpoint: async ({ }, use) => {
Expand Down
23 changes: 23 additions & 0 deletions tests/mcp/launch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ test('isolated context', async ({ startClient, server }) => {
});
});

test('isolated stdio server closes browser on client transport disconnect', async ({ startClient, server }) => {
const { client, stderr, closeTransport } = await startClient({
args: [`--isolated`],
env: { DEBUG: 'pw:mcp:test' },
});

expect(await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
})).toHaveResponse({
snapshot: expect.stringContaining(`Hello, world!`),
});

await closeTransport();

await expect.poll(() => formatLog(stderr())).toEqual({
'create browser (isolated)': 1,
'create context': 1,
'close browser': 1,
'gracefully closing 1': 1,
});
});

test('isolated context relaunches the browser after it dies', async ({ startClient, server, mcpBrowser }, testInfo) => {
test.skip(!['chrome', 'msedge', 'chromium'].includes(mcpBrowser!), 'The test kills the browser over CDP');

Expand Down
Loading