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): --config and --project options for init-agents (#37642)

Yury Semikhatsky4182ae184431
diff --git a/packages/playwright/src/agents/generateAgents.ts b/packages/playwright/src/agents/generateAgents.ts
index 2fa18e3ebb..2ce295f8ca 100644
--- a/packages/playwright/src/agents/generateAgents.ts
+++ b/packages/playwright/src/agents/generateAgents.ts
@@ -17,8 +17,6 @@
import fs from 'fs';
import path from 'path';
import { yaml } from 'playwright-core/lib/utilsBundle';
-import { loadConfigFromFile, resolveConfigLocation } from '../common/configLoader';
-import { createDefaultSeedFile } from '../mcp/test/testTools';
interface AgentHeader {
name: string;
@@ -283,17 +281,6 @@ export async function initVSCodeRepo() {
env: { 'PLAYWRIGHT_MCP_TOOL_PREFIX': vscodeToolPrefix },
};
await writeFile(mcpJsonPath, JSON.stringify(mcpJson, null, 2));
-
- try {
- const configLocation = resolveConfigLocation(undefined);
- const config = await loadConfigFromFile(configLocation.resolvedConfigFile);
- const seedFile = await createDefaultSeedFile(config);
- // eslint-disable-next-line no-console
- console.log(`Writing file: ${path.relative(process.cwd(), seedFile)}`);
- } catch (error) {
- // eslint-disable-next-line no-console
- console.error('Could not create seed.spec.ts:', error);
- }
}
export async function initOpencodeRepo() {
diff --git a/packages/playwright/src/mcp/test/seed.ts b/packages/playwright/src/mcp/test/seed.ts
new file mode 100644
index 0000000000..849315c438
--- /dev/null
+++ b/packages/playwright/src/mcp/test/seed.ts
@@ -0,0 +1,60 @@
+/**
+ * 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 fs from 'fs';
+import path from 'path';
+
+import { mkdirIfNeeded } from 'playwright-core/lib/utils';
+
+import { collectFilesForProject, findTopLevelProjects } from '../../runner/projectUtils';
+
+import type { FullConfigInternal } from '../../common/config';
+
+export function seedProject(config: FullConfigInternal, projectName?: string) {
+ if (!projectName)
+ return findTopLevelProjects(config)[0];
+ const project = config.projects.find(p => p.project.name === projectName);
+ if (!project)
+ throw new Error(`Project ${projectName} not found`);
+ return project;
+}
+
+export async function ensureSeedTest(config: FullConfigInternal, projectName: string | undefined, logNew: boolean) {
+ const project = seedProject(config, projectName);
+ const files = await collectFilesForProject(project);
+ const seed = files.find(file => path.basename(file).includes('seed'));
+ if (seed)
+ return seed;
+
+ const testDir = project.project.testDir;
+ const seedFile = path.resolve(testDir, 'seed.spec.ts');
+
+ if (logNew) {
+ // eslint-disable-next-line no-console
+ console.log(`Writing file: ${path.relative(process.cwd(), seedFile)}`);
+ }
+
+ await mkdirIfNeeded(seedFile);
+ await fs.promises.writeFile(seedFile, `import { test, expect } from '@playwright/test';
+
+test.describe('Test group', () => {
+ test('seed', async ({ page }) => {
+ // generate code here.
+ });
+});
+`);
+ return seedFile;
+}
diff --git a/packages/playwright/src/mcp/test/testTools.ts b/packages/playwright/src/mcp/test/testTools.ts
index a02bd5da5d..01cd9a1cb4 100644
--- a/packages/playwright/src/mcp/test/testTools.ts
+++ b/packages/playwright/src/mcp/test/testTools.ts
@@ -17,20 +17,18 @@
import fs from 'fs';
import path from 'path';
-import { noColors, escapeRegExp, mkdirIfNeeded } from 'playwright-core/lib/utils';
+import { noColors, escapeRegExp } from 'playwright-core/lib/utils';
import { z } from '../sdk/bundle';
import { terminalScreen } from '../../reporters/base';
import ListReporter from '../../reporters/list';
import ListModeReporter from '../../reporters/listModeReporter';
-import { findTopLevelProjects } from '../../runner/projectUtils';
-
+import { ensureSeedTest, seedProject } from './seed';
import { defineTestTool } from './testTool';
import { StringWriteStream } from './streams';
import { fileExistsAsync } from '../../util';
import type { ProgressCallback } from '../sdk/server';
-import type { FullConfigInternal } from '../../common/config';
export const listTests = defineTestTool({
schema: {
@@ -111,28 +109,6 @@ export const debugTest = defineTestTool({
},
});
-function resolveTestDir(config: FullConfigInternal, projectName?: string) {
- const project = projectName ? config.projects.find(p => p.project.name === projectName) : findTopLevelProjects(config)[0];
- return project?.project.testDir || config.configDir;
-}
-
-export async function createDefaultSeedFile(config: FullConfigInternal, projectName?: string) {
- const testDir = resolveTestDir(config, projectName);
- const seedFile = path.resolve(testDir, 'seed.spec.ts');
- if (!await fileExistsAsync(seedFile)) {
- await mkdirIfNeeded(seedFile);
- await fs.promises.writeFile(seedFile, `import { test, expect } from '@playwright/test';
-
-test.describe('Test group', () => {
- test('seed', async ({ page }) => {
- // generate code here.
- });
-});
-`);
- }
- return seedFile;
-}
-
export const setupPage = defineTestTool({
schema: {
name: 'test_setup_page',
@@ -151,13 +127,13 @@ export const setupPage = defineTestTool({
const reporter = new ListReporter({ configDir, screen });
const testRunner = await context.createTestRunner();
const config = await testRunner.loadConfig();
- const testDir = resolveTestDir(config, params.project);
let seedFile: string | undefined;
if (!params.seedFile) {
- seedFile = await createDefaultSeedFile(config, params.project);
+ seedFile = await ensureSeedTest(config, params.project, false);
} else {
const candidateFiles: string[] = [];
+ const testDir = seedProject(config, params.project).project.testDir;
candidateFiles.push(path.resolve(testDir, params.seedFile));
candidateFiles.push(path.resolve(configDir, params.seedFile));
candidateFiles.push(path.resolve(context.rootPath, params.seedFile));
diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.ts
index 96bd20baab..79d93a8ce1 100644
--- a/packages/playwright/src/program.ts
+++ b/packages/playwright/src/program.ts
@@ -35,6 +35,7 @@ import { runAllTestsWithConfig, TestRunner } from './runner/testRunner';
import { createErrorCollectingReporter } from './runner/reporters';
import { ServerBackendFactory, runMainBackend } from './mcp/sdk/exports';
import { TestServerBackend } from './mcp/test/testBackend';
+import { ensureSeedTest } from './mcp/test/seed';
import { decorateCommand } from './mcp/program';
import { setupExitWatchdog } from './mcp/browser/watchdog';
import { initClaudeCodeRepo, initOpencodeRepo, initVSCodeRepo } from './agents/generateAgents';
@@ -180,15 +181,21 @@ function addInitAgentsCommand(program: Command) {
const option = command.createOption('--loop <loop>', 'Agentic loop provider');
option.choices(['vscode', 'claude', 'opencode']);
command.addOption(option);
+ command.option('-c, --config <file>', `Configuration file to find a project to use for seed test`);
+ command.option('--project <project>', 'Project to use for seed test');
command.action(async opts => {
- if (opts.loop === 'opencode')
+ if (opts.loop === 'opencode') {
await initOpencodeRepo();
- else if (opts.loop === 'vscode')
+ } else if (opts.loop === 'vscode') {
await initVSCodeRepo();
- else if (opts.loop === 'claude')
+ } else if (opts.loop === 'claude') {
await initClaudeCodeRepo();
- else
+ } else {
command.help();
+ return;
+ }
+ const config = await loadConfigFromFile(opts.config);
+ await ensureSeedTest(config, opts.project, true);
});
}