Skip to content

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

chore: write .template.spec.ts to tests dir (#37439)

Yury Semikhatsky6e81bf0167f6
diff --git a/packages/playwright/src/mcp/test/testTools.ts b/packages/playwright/src/mcp/test/testTools.ts
index faa0c8ac10..d5a832ca0d 100644
--- a/packages/playwright/src/mcp/test/testTools.ts
+++ b/packages/playwright/src/mcp/test/testTools.ts
@@ -139,7 +139,10 @@ export const setupPage = defineTestTool({
let testLocation = params.testLocation;
if (!testLocation) {
testLocation = '.template.spec.ts';
- const templateFile = path.join(configDir, testLocation);
+ const config = await testRunner.loadConfig();
+ const project = params.project ? config.projects.find(p => p.project.name === params.project) : config.projects[0];
+ const testDir = project?.project.testDir || configDir;
+ const templateFile = path.join(testDir, testLocation);
if (!fs.existsSync(templateFile)) {
await fs.promises.writeFile(templateFile, `
import { test, expect } from '@playwright/test';
diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.ts
index cb24a6d357..b6d1b4fa6d 100644
--- a/packages/playwright/src/runner/testRunner.ts
+++ b/packages/playwright/src/runner/testRunner.ts
@@ -139,6 +139,13 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
await registry.install(executables, false);
}
+ async loadConfig() {
+ const { config, error } = await this._loadConfig(this._configCLIOverrides);
+ if (config)
+ return config;
+ throw new Error('Failed to load config: ' + (error ? error.message : 'Unknown error'));
+ }
+
async runGlobalSetup(userReporters: AnyReporter[]): Promise<{ status: FullResultStatus }> {
await this.runGlobalTeardown();
diff --git a/tests/mcp/test-tools.spec.ts b/tests/mcp/test-tools.spec.ts
index 37de4d325a..4dc8fb4de8 100644
--- a/tests/mcp/test-tools.spec.ts
+++ b/tests/mcp/test-tools.spec.ts
@@ -16,6 +16,8 @@
import { test, expect, writeFiles, StartClient } from './fixtures';
+import fs from 'fs';
+
test.use({ mcpServerType: 'test-mcp' });
test('test_list', async ({ startClient }) => {
@@ -359,6 +361,33 @@ test('test_setup_page (no test location)', async ({ startClient }) => {
`);
});
+test('test_setup_page without location respects testsDir', async ({ startClient }) => {
+ await writeFiles({
+ 'playwright.config.ts': `
+ module.exports = {
+ testDir: './tests',
+ projects: [{ name: 'foo' }]
+ };
+ `,
+
+ 'tests/a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('existing', async ({ page }) => {
+ });
+ `,
+ });
+
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_setup_page',
+ arguments: {},
+ })).toHaveTextResponse(`### Paused at end of test. ready for interaction
+
+### Current page snapshot:
+`);
+ expect(fs.existsSync(test.info().outputPath('tests', '.template.spec.ts'))).toBe(true);
+});
+
async function prepareDebugTest(startClient: StartClient) {
await writeFiles({
'a.test.ts': `