playwright-mcp/tests/fixtures.ts

162 lines
4.6 KiB
TypeScript
Raw Normal View History

2025-03-21 10:58:58 -07:00
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import path from 'path';
import { chromium } from 'playwright';
2025-03-21 10:58:58 -07:00
2025-03-27 16:50:43 -07:00
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';
2025-03-21 10:58:58 -07:00
2025-04-17 14:03:13 -07:00
type TestFixtures = {
2025-03-27 16:50:43 -07:00
client: Client;
visionClient: Client;
startClient: (options?: { args?: string[] }) => Promise<Client>;
wsEndpoint: string;
cdpEndpoint: string;
2025-04-17 14:03:13 -07:00
};
2025-04-17 14:03:13 -07:00
type WorkerFixtures = {
mcpHeadless: boolean;
mcpBrowser: string | undefined;
};
2025-03-21 10:58:58 -07:00
2025-04-17 14:03:13 -07:00
export const test = baseTest.extend<TestFixtures, WorkerFixtures>({
2025-03-27 16:50:43 -07:00
client: async ({ startClient }, use) => {
await use(await startClient());
},
2025-03-27 16:50:43 -07:00
visionClient: async ({ startClient }, use) => {
await use(await startClient({ args: ['--vision'] }));
2025-03-27 17:13:06 +01:00
},
startClient: async ({ mcpHeadless, mcpBrowser }, use, testInfo) => {
const userDataDir = testInfo.outputPath('user-data-dir');
2025-03-27 16:50:43 -07:00
let client: StdioClientTransport | undefined;
use(async options => {
const args = ['--user-data-dir', userDataDir];
if (mcpHeadless)
args.push('--headless');
if (mcpBrowser)
args.push(`--browser=${mcpBrowser}`);
if (options?.args)
args.push(...options.args);
2025-03-27 16:50:43 -07:00
const transport = new StdioClientTransport({
command: 'node',
args: [path.join(__dirname, '../cli.js'), ...args],
});
2025-03-27 16:50:43 -07:00
const client = new Client({ name: 'test', version: '1.0.0' });
await client.connect(transport);
await client.ping();
return client;
2025-03-21 10:58:58 -07:00
});
2025-03-27 16:50:43 -07:00
await client?.close();
},
wsEndpoint: async ({ }, use) => {
const browserServer = await chromium.launchServer();
await use(browserServer.wsEndpoint());
await browserServer.close();
2025-03-21 10:58:58 -07:00
},
cdpEndpoint: async ({ }, use, testInfo) => {
const port = 3200 + (+process.env.TEST_PARALLEL_INDEX!);
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}`);
browserProcess.kill();
},
2025-04-17 14:03:13 -07:00
mcpHeadless: [async ({ headless }, use) => {
await use(headless);
2025-04-17 14:03:13 -07:00
}, { scope: 'worker' }],
2025-04-17 14:03:13 -07:00
mcpBrowser: ['chromium', { option: true, scope: 'worker' }],
2025-03-21 10:58:58 -07:00
});
2025-03-27 16:50:43 -07:00
type Response = Awaited<ReturnType<Client['callTool']>>;
export const expect = baseExpect.extend({
toHaveTextContent(response: Response, content: string | RegExp) {
2025-03-27 16:50:43 -07:00
const isNot = this.isNot;
try {
const text = (response.content as any)[0].text;
2025-04-03 19:24:17 -07:00
if (typeof content === 'string') {
if (isNot)
baseExpect(text.trim()).not.toBe(content.trim());
else
baseExpect(text.trim()).toBe(content.trim());
} else {
if (isNot)
baseExpect(text).not.toMatch(content);
else
baseExpect(text).toMatch(content);
}
2025-03-27 16:50:43 -07:00
} catch (e) {
return {
pass: isNot,
message: () => e.message,
};
}
return {
pass: !isNot,
message: () => ``,
};
},
toContainTextContent(response: Response, content: string | string[]) {
const isNot = this.isNot;
try {
content = Array.isArray(content) ? content : [content];
const texts = (response.content as any).map(c => c.text);
for (let i = 0; i < texts.length; i++) {
if (isNot)
expect(texts[i]).not.toContain(content[i]);
else
expect(texts[i]).toContain(content[i]);
}
} catch (e) {
return {
pass: isNot,
message: () => e.message,
};
}
return {
pass: !isNot,
message: () => ``,
};
},
});