Skip to content

test: verify the log in close/navigate test #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2025
Merged
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
2 changes: 1 addition & 1 deletion tests/capabilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test('test vision tool list', async ({ visionClient }) => {
});

test('test capabilities', async ({ startClient }) => {
const client = await startClient({
const { client } = await startClient({
args: ['--caps="core"'],
});
const { tools } = await client.listTools();
Expand Down
6 changes: 3 additions & 3 deletions tests/cdp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { test, expect } from './fixtures.js';

test('cdp server', async ({ cdpServer, startClient, server }) => {
await cdpServer.start();
const client = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });
const { client } = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });
expect(await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
Expand All @@ -27,7 +27,7 @@ test('cdp server', async ({ cdpServer, startClient, server }) => {

test('cdp server reuse tab', async ({ cdpServer, startClient, server }) => {
const browserContext = await cdpServer.start();
const client = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });
const { client } = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });

const [page] = browserContext.pages();
await page.goto(server.HELLO_WORLD);
Expand Down Expand Up @@ -58,7 +58,7 @@ test('cdp server reuse tab', async ({ cdpServer, startClient, server }) => {
});

test('should throw connection error and allow re-connecting', async ({ cdpServer, startClient, server }) => {
const client = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });
const { client } = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });

server.setContent('/', `
<title>Title</title>
Expand Down
4 changes: 2 additions & 2 deletions tests/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('config user data dir', async ({ startClient, server }, testInfo) => {
const configPath = testInfo.outputPath('config.json');
await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2));

const client = await startClient({ args: ['--config', configPath] });
const { client } = await startClient({ args: ['--config', configPath] });
expect(await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
Expand All @@ -54,7 +54,7 @@ test.describe(() => {
const configPath = testInfo.outputPath('config.json');
await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2));

const client = await startClient({ args: ['--config', configPath] });
const { client } = await startClient({ args: ['--config', configPath] });
expect(await client.callTool({
name: 'browser_navigate',
arguments: { url: 'data:text/html,<script>document.title = navigator.userAgent</script>' },
Expand Down
2 changes: 1 addition & 1 deletion tests/device.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { test, expect } from './fixtures.js';

test('--device should work', async ({ startClient, server }) => {
const client = await startClient({
const { client } = await startClient({
args: ['--device', 'iPhone 15'],
});

Expand Down
4 changes: 2 additions & 2 deletions tests/files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ The tool "browser_file_upload" can only be used when there is related modal stat
});

test('clicking on download link emits download', async ({ startClient, server }, testInfo) => {
const client = await startClient({
const { client } = await startClient({
config: { outputDir: testInfo.outputPath('output') },
});

Expand All @@ -125,7 +125,7 @@ test('clicking on download link emits download', async ({ startClient, server },
});

test('navigating to download link emits download', async ({ startClient, server, mcpBrowser }, testInfo) => {
const client = await startClient({
const { client } = await startClient({
config: { outputDir: testInfo.outputPath('output') },
});

Expand Down
26 changes: 21 additions & 5 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type CDPServer = {
type TestFixtures = {
client: Client;
visionClient: Client;
startClient: (options?: { clientName?: string, args?: string[], config?: Config }) => Promise<Client>;
startClient: (options?: { clientName?: string, args?: string[], config?: Config }) => Promise<{ client: Client, stderr: () => string }>;
wsEndpoint: string;
cdpServer: CDPServer;
server: TestServer;
Expand All @@ -55,11 +55,13 @@ type WorkerFixtures = {
export const test = baseTest.extend<TestFixtures & TestOptions, WorkerFixtures>({

client: async ({ startClient }, use) => {
await use(await startClient());
const { client } = await startClient();
await use(client);
},

visionClient: async ({ startClient }, use) => {
await use(await startClient({ args: ['--vision'] }));
const { client } = await startClient({ args: ['--vision'] });
await use(client);
},

startClient: async ({ mcpHeadless, mcpBrowser, mcpMode }, use, testInfo) => {
Expand All @@ -85,9 +87,13 @@ export const test = baseTest.extend<TestFixtures & TestOptions, WorkerFixtures>(

client = new Client({ name: options?.clientName ?? 'test', version: '1.0.0' });
const transport = createTransport(args, mcpMode);
let stderr = '';
transport.stderr?.on('data', data => {
stderr += data.toString();
});
await client.connect(transport);
await client.ping();
return client;
return { client, stderr: () => stderr };
});

await client?.close();
Expand Down Expand Up @@ -168,7 +174,13 @@ function createTransport(args: string[], mcpMode: TestOptions['mcpMode']) {
command: 'node',
args: [path.join(path.dirname(__filename), '../cli.js'), ...args],
cwd: path.join(path.dirname(__filename), '..'),
env: process.env as Record<string, string>,
stderr: 'pipe',
env: {
...process.env,
DEBUG: 'pw:mcp:test',
DEBUG_COLORS: '0',
DEBUG_HIDE_DATE: '1',
},
});
}

Expand Down Expand Up @@ -225,3 +237,7 @@ export const expect = baseExpect.extend({
};
},
});

export function formatOutput(output: string): string[] {
return output.split('\n').map(line => line.replace(/^pw:mcp:test /, '').replace(/test-results.*/, '').trim()).filter(Boolean);
}
42 changes: 32 additions & 10 deletions tests/launch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import fs from 'fs';

import { test, expect } from './fixtures.js';
import { test, expect, formatOutput } from './fixtures.js';

test('test reopen browser', async ({ client, server }) => {
test('test reopen browser', async ({ startClient, server }) => {
const { client, stderr } = await startClient();
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
Expand All @@ -32,10 +33,31 @@ test('test reopen browser', async ({ client, server }) => {
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
})).toContainTextContent(`- generic [ref=e1]: Hello, world!`);

await client.close();

if (process.platform === 'win32')
return;

await expect.poll(() => formatOutput(stderr()), { timeout: 0 }).toEqual([
'create context',
'create browser context (persistent)',
'lock user data dir',
'close context',
'close browser context (persistent)',
'release user data dir',
'close browser context complete (persistent)',
'create browser context (persistent)',
'lock user data dir',
'close context',
'close browser context (persistent)',
'release user data dir',
'close browser context complete (persistent)',
]);
});

test('executable path', async ({ startClient, server }) => {
const client = await startClient({ args: [`--executable-path=bogus`] });
const { client } = await startClient({ args: [`--executable-path=bogus`] });
const response = await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
Expand All @@ -53,7 +75,7 @@ test('persistent context', async ({ startClient, server }) => {
</script>
`, 'text/html');

const client = await startClient();
const { client } = await startClient();
const response = await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
Expand All @@ -66,7 +88,7 @@ test('persistent context', async ({ startClient, server }) => {
name: 'browser_close',
});

const client2 = await startClient();
const { client: client2 } = await startClient();
const response2 = await client2.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
Expand All @@ -85,18 +107,18 @@ test('isolated context', async ({ startClient, server }) => {
</script>
`, 'text/html');

const client = await startClient({ args: [`--isolated`] });
const response = await client.callTool({
const { client: client1 } = await startClient({ args: [`--isolated`] });
const response = await client1.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});
expect(response).toContainTextContent(`Storage: NO`);

await client.callTool({
await client1.callTool({
name: 'browser_close',
});

const client2 = await startClient({ args: [`--isolated`] });
const { client: client2 } = await startClient({ args: [`--isolated`] });
const response2 = await client2.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
Expand All @@ -123,7 +145,7 @@ test('isolated context with storage state', async ({ startClient, server }, test
</script>
`, 'text/html');

const client = await startClient({ args: [
const { client } = await startClient({ args: [
`--isolated`,
`--storage-state=${storageStatePath}`,
] });
Expand Down
6 changes: 3 additions & 3 deletions tests/pdf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import fs from 'fs';
import { test, expect } from './fixtures.js';

test('save as pdf unavailable', async ({ startClient, server }) => {
const client = await startClient({ args: ['--caps="no-pdf"'] });
const { client } = await startClient({ args: ['--caps="no-pdf"'] });
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
Expand All @@ -31,7 +31,7 @@ test('save as pdf unavailable', async ({ startClient, server }) => {
});

test('save as pdf', async ({ startClient, mcpBrowser, server }, testInfo) => {
const client = await startClient({
const { client } = await startClient({
config: { outputDir: testInfo.outputPath('output') },
});

Expand All @@ -51,7 +51,7 @@ test('save as pdf', async ({ startClient, mcpBrowser, server }, testInfo) => {
test('save as pdf (filename: output.pdf)', async ({ startClient, mcpBrowser, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');
test.skip(!!mcpBrowser && !['chromium', 'chrome', 'msedge'].includes(mcpBrowser), 'Save as PDF is only supported in Chromium.');
const client = await startClient({
const { client } = await startClient({
config: { outputDir },
});

Expand Down
10 changes: 5 additions & 5 deletions tests/request-blocking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('default to allow all', async ({ server, client }) => {
});

test('blocked works', async ({ startClient }) => {
const client = await startClient({
const { client } = await startClient({
args: ['--blocked-origins', 'microsoft.com;example.com;playwright.dev']
});
const result = await fetchPage(client, 'https://example.com/');
Expand All @@ -46,15 +46,15 @@ test('blocked works', async ({ startClient }) => {

test('allowed works', async ({ server, startClient }) => {
server.setContent('/ppp', 'content:PPP', 'text/html');
const client = await startClient({
const { client } = await startClient({
args: ['--allowed-origins', `microsoft.com;${new URL(server.PREFIX).host};playwright.dev`]
});
const result = await fetchPage(client, server.PREFIX + 'ppp');
expect(result).toContain('content:PPP');
});

test('blocked takes precedence', async ({ startClient }) => {
const client = await startClient({
const { client } = await startClient({
args: [
'--blocked-origins', 'example.com',
'--allowed-origins', 'example.com',
Expand All @@ -65,7 +65,7 @@ test('blocked takes precedence', async ({ startClient }) => {
});

test('allowed without blocked blocks all non-explicitly specified origins', async ({ startClient }) => {
const client = await startClient({
const { client } = await startClient({
args: ['--allowed-origins', 'playwright.dev'],
});
const result = await fetchPage(client, 'https://example.com/');
Expand All @@ -74,7 +74,7 @@ test('allowed without blocked blocks all non-explicitly specified origins', asyn

test('blocked without allowed allows non-explicitly specified origins', async ({ server, startClient }) => {
server.setContent('/ppp', 'content:PPP', 'text/html');
const client = await startClient({
const { client } = await startClient({
args: ['--blocked-origins', 'example.com'],
});
const result = await fetchPage(client, server.PREFIX + 'ppp');
Expand Down
14 changes: 7 additions & 7 deletions tests/screenshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import fs from 'fs';
import { test, expect } from './fixtures.js';

test('browser_take_screenshot (viewport)', async ({ startClient, server }, testInfo) => {
const client = await startClient({
const { client } = await startClient({
config: { outputDir: testInfo.outputPath('output') },
});
expect(await client.callTool({
Expand All @@ -45,7 +45,7 @@ test('browser_take_screenshot (viewport)', async ({ startClient, server }, testI
});

test('browser_take_screenshot (element)', async ({ startClient, server }, testInfo) => {
const client = await startClient({
const { client } = await startClient({
config: { outputDir: testInfo.outputPath('output') },
});
expect(await client.callTool({
Expand Down Expand Up @@ -76,7 +76,7 @@ test('browser_take_screenshot (element)', async ({ startClient, server }, testIn

test('--output-dir should work', async ({ startClient, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');
const client = await startClient({
const { client } = await startClient({
config: { outputDir },
});
expect(await client.callTool({
Expand All @@ -98,7 +98,7 @@ for (const raw of [undefined, true]) {
test(`browser_take_screenshot (raw: ${raw})`, async ({ startClient, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');
const ext = raw ? 'png' : 'jpeg';
const client = await startClient({
const { client } = await startClient({
config: { outputDir },
});
expect(await client.callTool({
Expand Down Expand Up @@ -138,7 +138,7 @@ for (const raw of [undefined, true]) {

test('browser_take_screenshot (filename: "output.jpeg")', async ({ startClient, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');
const client = await startClient({
const { client } = await startClient({
config: { outputDir },
});
expect(await client.callTool({
Expand Down Expand Up @@ -174,7 +174,7 @@ test('browser_take_screenshot (filename: "output.jpeg")', async ({ startClient,

test('browser_take_screenshot (imageResponses=omit)', async ({ startClient, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');
const client = await startClient({
const { client } = await startClient({
config: {
outputDir,
imageResponses: 'omit',
Expand Down Expand Up @@ -205,7 +205,7 @@ test('browser_take_screenshot (imageResponses=omit)', async ({ startClient, serv
test('browser_take_screenshot (cursor)', async ({ startClient, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');

const client = await startClient({
const { client } = await startClient({
clientName: 'cursor:vscode',
config: { outputDir },
});
Expand Down
2 changes: 1 addition & 1 deletion tests/tabs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ test('reuse first tab when navigating', async ({ startClient, cdpServer, server
const browserContext = await cdpServer.start();
const pages = browserContext.pages();

const client = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });
const { client } = await startClient({ args: [`--cdp-endpoint=${cdpServer.endpoint}`] });
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
Expand Down
2 changes: 1 addition & 1 deletion tests/trace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { test, expect } from './fixtures.js';
test('check that trace is saved', async ({ startClient, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');

const client = await startClient({
const { client } = await startClient({
args: ['--save-trace', `--output-dir=${outputDir}`],
});

Expand Down