Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: various seed file resolutions (#37584)
Pavel Feldmanc3a933e9adfe
diff --git a/packages/playwright/src/mcp/test/testTools.ts b/packages/playwright/src/mcp/test/testTools.tsindex 42e42b2821..be61b38bbf 100644--- a/packages/playwright/src/mcp/test/testTools.ts+++ b/packages/playwright/src/mcp/test/testTools.ts@@ -27,6 +27,7 @@ import { findTopLevelProjects } from '../../runner/projectUtils';import { defineTestTool } from './testTool';import { StringWriteStream } from './streams';+import { fileExistsAsync } from '../../util';export const listTests = defineTestTool({schema: {@@ -124,10 +125,10 @@ export const setupPage = defineTestTool({schema: {name: 'test_setup_page',title: 'Setup page',- description: 'Setup the page for test',+ description: 'Setup the page for test.',inputSchema: z.object({project: z.string().optional().describe('Project to use for setup. For example: "chromium", if no project is provided uses the first project in the config.'),- testLocation: z.string().optional().describe('Location of the seed test to use for setup. For example: "tests/seed.spec.ts" or "tests/seed.spec.ts:20".'),+ seedFile: z.string().optional().describe('A seed file contains a single test that is used to setup the page for testing, for example: "tests/seed.spec.ts". If no seed file is provided, a default seed file is created.'),}),type: 'readOnly',},@@ -137,26 +138,40 @@ export const setupPage = defineTestTool({const configDir = context.configLocation.configDir;const reporter = new ListReporter({ configDir, screen });const testRunner = await context.createTestRunner();-- let testLocation = params.testLocation;- if (!testLocation) {- testLocation = 'default.seed.spec.ts';- 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 seedFile = path.join(testDir, testLocation);- if (!fs.existsSync(seedFile)) {- await fs.promises.mkdir(path.dirname(seedFile), { recursive: true });- await fs.promises.writeFile(seedFile, `import { test, expect } from '@playwright/test';--test('seed', async ({ page }) => {});+ 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;++ 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.+ });+});`);+ } else {+ const candidateFiles: string[] = [];+ candidateFiles.push(path.resolve(testDir, params.seedFile));+ candidateFiles.push(path.resolve(configDir, params.seedFile));+ candidateFiles.push(path.resolve(process.cwd(), params.seedFile));+ for (const candidateFile of candidateFiles) {+ if (await fileExistsAsync(candidateFile)) {+ seedFile = candidateFile;+ break;+ }}+ if (!seedFile)+ throw new Error('seed test not found.');}const result = await testRunner.runTests(reporter, {headed: !context.options?.headless,- locations: [testLocation],+ locations: [seedFile],projects: params.project ? [params.project] : undefined,timeout: 0,workers: 1,@@ -167,17 +182,14 @@ test('seed', async ({ page }) => {});// Ideally, we should check that page was indeed created and browser mcp has kicked in.// However, that is handled in the upper layer, so hard to check here.- if (result.status === 'passed' && !reporter.suite?.allTests().length) {- return {- content: [{ type: 'text', text: 'Error: seed test not found.' }],- isError: true,- };- }+ if (result.status === 'passed' && !reporter.suite?.allTests().length)+ throw new Error('seed test not found.');++ if (result.status !== 'passed')+ throw new Error(stream.content());- const text = stream.content();return {- content: [{ type: 'text', text }],- isError: result.status !== 'passed',+ content: [{ type: 'text', text: 'Done.' }],};},});diff --git a/tests/mcp/fixtures.ts b/tests/mcp/fixtures.tsindex c17a0030cc..a5efa2fa33 100644--- a/tests/mcp/fixtures.ts+++ b/tests/mcp/fixtures.ts@@ -87,7 +87,8 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixturesargs.push('--headless');if (mcpServerType === 'test-mcp') {- args.push('--config', test.info().outputPath());+ if (!options?.args?.some(arg => arg.startsWith('--config')))+ args.push('--config', test.info().outputPath());} else {if (process.env.CI && process.platform === 'linux')args.push('--no-sandbox');diff --git a/tests/mcp/test-tools.spec.ts b/tests/mcp/test-tools.spec.tsindex 1ec6ac2ecb..44a6b2fa8f 100644--- a/tests/mcp/test-tools.spec.ts+++ b/tests/mcp/test-tools.spec.ts@@ -466,7 +466,7 @@ test('test_setup_page', async ({ startClient }) => {expect(await client.callTool({name: 'test_setup_page',arguments: {- testLocation: 'a.test.ts:6',+ seedFile: 'a.test.ts',},})).toHaveTextResponse(`### Paused at end of test. ready for interaction@@ -491,6 +491,67 @@ test('test_setup_page', async ({ startClient }) => {});});+test('test_setup_page seed resolution', async ({ startClient }) => {+ await writeFiles({+ 'playwright.config.ts': `+ module.exports = {+ testDir: './tests',+ };+ `,+ 'tests/seed.test.ts': `+ import { test, expect } from '@playwright/test';+ test('template', async ({ page }) => {+ await page.setContent('<button>Submit</button>');+ });+ `,+ });++ const { client } = await startClient();++ // Relative to test dir.+ expect(await client.callTool({+ name: 'test_setup_page',+ arguments: {+ seedFile: 'seed.test.ts',+ },+ })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test.`));++ // Relative to config dir.+ expect(await client.callTool({+ name: 'test_setup_page',+ arguments: {+ seedFile: 'tests/seed.test.ts',+ },+ })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test.`));+});++test('test_setup_page seed resolution - cwd', async ({ startClient }) => {+ await writeFiles({+ 'configs/playwright.config.ts': `+ module.exports = {+ testDir: '../tests',+ };+ `,+ 'tests/seed.test.ts': `+ import { test, expect } from '@playwright/test';+ test('template', async ({ page }) => {+ await page.setContent('<button>Submit</button>');+ });+ `,+ });++ const { client } = await startClient({+ args: ['--config=configs/playwright.config.ts'],+ });++ expect(await client.callTool({+ name: 'test_setup_page',+ arguments: {+ seedFile: 'tests/seed.test.ts',+ },+ })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test.`));+});+test('test_setup_page with dependencies', async ({ startClient }) => {const baseDir = await writeFiles({'playwright.config.ts': `@@ -508,7 +569,7 @@ test('test_setup_page with dependencies', async ({ startClient }) => {require('fs').writeFileSync(testInfo.outputPath('auth.txt'), 'done');});`,- 'template.test.ts': `+ 'seed.test.ts': `import { test, expect } from '@playwright/test';test('template', async ({ page }, testInfo) => {require('fs').writeFileSync(testInfo.outputPath('template.txt'), 'done');@@ -520,7 +581,7 @@ test('test_setup_page with dependencies', async ({ startClient }) => {expect(await client.callTool({name: 'test_setup_page',arguments: {- testLocation: 'template.test.ts',+ seedFile: 'seed.test.ts',project: 'chromium',},})).toHaveTextResponse(`### Paused at end of test. ready for interaction@@ -536,13 +597,13 @@ test('test_setup_page with dependencies', async ({ startClient }) => {// Should pause at the target test, not in a dependency or any other stray project.expect(fs.existsSync(path.join(baseDir, 'test-results', 'auth.setup.ts-auth-setup', 'auth.txt'))).toBe(true);- expect(fs.existsSync(path.join(baseDir, 'test-results', 'template-template-chromium', 'template.txt'))).toBe(true);- expect(fs.existsSync(path.join(baseDir, 'test-results', 'template-template-ignored', 'template.txt'))).toBe(false);+ expect(fs.existsSync(path.join(baseDir, 'test-results', 'seed-template-chromium', 'template.txt'))).toBe(true);+ expect(fs.existsSync(path.join(baseDir, 'test-results', 'seed-template-ignored', 'template.txt'))).toBe(false);});test('test_setup_page (loading error)', async ({ startClient }) => {await writeFiles({- 'template.test.ts': `+ 'seed.test.ts': `throw new Error('loading error');`,});@@ -550,7 +611,7 @@ test('test_setup_page (loading error)', async ({ startClient }) => {expect(await client.callTool({name: 'test_setup_page',arguments: {- testLocation: 'template.test.ts',+ seedFile: 'seed.test.ts',},})).toHaveTextResponse(expect.stringContaining('Error: loading error'));});@@ -560,9 +621,10 @@ test('test_setup_page (wrong test location)', async ({ startClient }) => {expect(await client.callTool({name: 'test_setup_page',arguments: {- testLocation: 'a.test.ts:6',+ seedFile: 'a.test.ts',},- })).toHaveTextResponse(`Error: seed test not found.`);+ })).toHaveTextResponse(`### Result+Error: seed test not found.`);});test('test_setup_page (no test location)', async ({ startClient }) => {@@ -609,8 +671,8 @@ test('test_setup_page chooses top-level project', async ({ startClient }) => {\`\`\``);- expect(fs.existsSync(path.join(baseDir, 'one', 'default.seed.spec.ts'))).toBe(false);- expect(fs.existsSync(path.join(baseDir, 'two', 'default.seed.spec.ts'))).toBe(true);+ expect(fs.existsSync(path.join(baseDir, 'one', 'seed.spec.ts'))).toBe(false);+ expect(fs.existsSync(path.join(baseDir, 'two', 'seed.spec.ts'))).toBe(true);});test('test_setup_page without location respects testsDir', async ({ startClient }) => {@@ -643,5 +705,5 @@ test('test_setup_page without location respects testsDir', async ({ startClient\`\`\``);- expect(fs.existsSync(test.info().outputPath('tests', 'default.seed.spec.ts'))).toBe(true);+ expect(fs.existsSync(test.info().outputPath('tests', 'seed.spec.ts'))).toBe(true);});