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): initialize seed test in the first project (#37639)

Yury Semikhatsky1248ee7755d5
diff --git a/packages/playwright/src/agents/DEPS.list b/packages/playwright/src/agents/DEPS.list
new file mode 100644
index 0000000000..75300109df
--- /dev/null
+++ b/packages/playwright/src/agents/DEPS.list
@@ -0,0 +1,3 @@
+[generateAgents.ts]
+../mcp/test/
+../common/
diff --git a/packages/playwright/src/agents/generateAgents.ts b/packages/playwright/src/agents/generateAgents.ts
index e30a366719..2fa18e3ebb 100644
--- a/packages/playwright/src/agents/generateAgents.ts
+++ b/packages/playwright/src/agents/generateAgents.ts
@@ -17,6 +17,8 @@
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;
@@ -217,8 +219,7 @@ const vscodeToolMap = new Map<string, string[]>([
['write', ['createFile', 'createDirectory']],
]);
const vscodeToolsOrder = ['createFile', 'createDirectory', 'editFiles', 'fileSearch', 'textSearch', 'listDirectory', 'readFile'];
-const vscodeToolPrefix = 'test_'; // FIXME: this is ugly, fix VSCode!
-
+const vscodeToolPrefix = 'test_'; // FIXME: Remove this once VSCode rolls fix for https://github.com/microsoft/vscode/issues/267811.
function saveAsVSCodeChatmode(agent: Agent): string {
function asVscodeTool(tool: string): string | string[] {
const [first, second] = tool.split('/');
@@ -282,6 +283,17 @@ 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/testTools.ts b/packages/playwright/src/mcp/test/testTools.ts
index 747c49f4c2..a02bd5da5d 100644
--- a/packages/playwright/src/mcp/test/testTools.ts
+++ b/packages/playwright/src/mcp/test/testTools.ts
@@ -17,7 +17,7 @@
import fs from 'fs';
import path from 'path';
-import { noColors, escapeRegExp } from 'playwright-core/lib/utils';
+import { noColors, escapeRegExp, mkdirIfNeeded } from 'playwright-core/lib/utils';
import { z } from '../sdk/bundle';
import { terminalScreen } from '../../reporters/base';
@@ -30,6 +30,7 @@ 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: {
@@ -110,6 +111,28 @@ 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',
@@ -128,21 +151,11 @@ export const setupPage = defineTestTool({
const reporter = new ListReporter({ configDir, screen });
const testRunner = await context.createTestRunner();
const config = await testRunner.loadConfig();
- const project = params.project ? config.projects.find(p => p.project.name === params.project) : findTopLevelProjects(config)[0];
- const testDir = project?.project.testDir || configDir;
+ const testDir = resolveTestDir(config, params.project);
let seedFile: string | undefined;
if (!params.seedFile) {
- seedFile = path.resolve(testDir, 'seed.spec.ts');
- await fs.promises.mkdir(path.dirname(seedFile), { recursive: true });
- await fs.promises.writeFile(seedFile, `import { test, expect } from '@playwright/test';
-
-test.describe('Test group', () => {
- test('seed', async ({ page }) => {
- // generate code here.
- });
-});
-`);
+ seedFile = await createDefaultSeedFile(config, params.project);
} else {
const candidateFiles: string[] = [];
candidateFiles.push(path.resolve(testDir, params.seedFile));