Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(mcp): fallback to cwd when resolving test config (#37757)

Pavel Feldmanef257ecffde6
diff --git a/packages/playwright/src/mcp/test/testBackend.ts b/packages/playwright/src/mcp/test/testBackend.ts
index 3338c08b94..8ac54f001c 100644
--- a/packages/playwright/src/mcp/test/testBackend.ts
+++ b/packages/playwright/src/mcp/test/testBackend.ts
@@ -58,7 +58,7 @@ export class TestServerBackend implements mcp.ServerBackend {
return;
}
- throw new Error('No config option or MCP root path provided');
+ this._context.initialize(rootPath, resolveConfigLocation(undefined));
}
async listTools(): Promise<mcp.Tool[]> {
diff --git a/tests/mcp/fixtures.ts b/tests/mcp/fixtures.ts
index 28e296e324..9f55b0c643 100644
--- a/tests/mcp/fixtures.ts
+++ b/tests/mcp/fixtures.ts
@@ -48,6 +48,8 @@ type CDPServer = {
export type StartClient = (options?: {
clientName?: string,
args?: string[],
+ omitArgs?: string[],
+ cwd?: string,
config?: Config,
roots?: { name: string, uri: string }[],
rootsResponseDelay?: number,
@@ -84,14 +86,14 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
const clients: Client[] = [];
await use(async options => {
- const args: string[] = mcpArgs ?? [];
+ let args: string[] = mcpArgs ?? [];
if (mcpHeadless)
args.push('--headless');
if (mcpServerType === 'test-mcp') {
if (!options?.args?.some(arg => arg.startsWith('--config')))
- args.push('--config', test.info().outputPath());
+ args.push(`--config=${test.info().outputPath()}`);
} else {
if (process.env.CI && process.platform === 'linux')
args.push('--no-sandbox');
@@ -106,6 +108,8 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
if (options?.args)
args.push(...options.args);
+ if (options?.omitArgs)
+ args = args.filter(arg => !options.omitArgs?.includes(arg));
const client = new Client({ name: options?.clientName ?? 'test', version: '1.0.0' }, options?.roots ? { capabilities: { roots: {} } } : undefined);
if (options?.roots) {
@@ -118,7 +122,7 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
});
}
const env = { ...process.env, ...options?.env };
- const { transport, stderr } = await createTransport(mcpServerType, args, env);
+ const { transport, stderr } = await createTransport(mcpServerType, { args, env, cwd: options?.cwd || test.info().outputPath() });
let stderrBuffer = '';
stderr?.on('data', data => {
if (process.env.PWDEBUGIMPL)
@@ -182,18 +186,18 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
mcpServerType: ['mcp', { option: true }],
});
-async function createTransport(mcpServerType: TestOptions['mcpServerType'], args: string[], env: NodeJS.ProcessEnv): Promise<{
+async function createTransport(mcpServerType: TestOptions['mcpServerType'], options: { args: string[], env: NodeJS.ProcessEnv, cwd: string }): Promise<{
transport: Transport,
stderr: Stream | null,
}> {
const profilesDir = test.info().outputPath('ms-playwright');
const transport = new StdioClientTransport({
command: 'node',
- args: [...(mcpServerType === 'test-mcp' ? testMcpServerPath : mcpServerPath), ...args],
- cwd: test.info().outputPath(),
+ args: [...(mcpServerType === 'test-mcp' ? testMcpServerPath : mcpServerPath), ...options.args],
+ cwd: options.cwd,
stderr: 'pipe',
env: {
- ...env,
+ ...options.env,
DEBUG_COLORS: '0',
DEBUG_HIDE_DATE: '1',
PWMCP_PROFILES_DIR_FOR_TEST: profilesDir,
diff --git a/tests/mcp/test-list.spec.ts b/tests/mcp/test-list.spec.ts
index af94d470b8..561e9bea84 100644
--- a/tests/mcp/test-list.spec.ts
+++ b/tests/mcp/test-list.spec.ts
@@ -45,3 +45,21 @@ test('test_list', async ({ startClient }) => {
[id=<ID>] a.test.ts:6:11 › example2
Total: 4 tests in 1 file`);
});
+
+test('test_list config in cwd', async ({ startClient }) => {
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('passes', () => {});
+ `,
+ });
+
+ const { client } = await startClient({
+ omitArgs: ['--config'],
+ });
+ expect(await client.callTool({
+ name: 'test_list',
+ })).toHaveTextResponse(`Listing tests:
+ [id=<ID>] a.test.ts:3:11 › passes
+Total: 1 test in 1 file`);
+});