Skip to content

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

chore: plumb screen via progress in test tools (#37590)

Pavel Feldman9deb6d82142e
diff --git a/packages/playwright/src/mcp/sdk/mdb.ts b/packages/playwright/src/mcp/sdk/mdb.ts
index 960b87622c..2a1d23f12e 100644
--- a/packages/playwright/src/mcp/sdk/mdb.ts
+++ b/packages/playwright/src/mcp/sdk/mdb.ts
@@ -85,13 +85,17 @@ export class MDBBackend implements mcpServer.ServerBackend {
},
}).then(result => {
resultPromise.resolve(result as mcpServer.CallToolResult);
- }).catch(e => resultPromise.reject(e));
+ }).catch(e => {
+ resultPromise.resolve({ content: [{ type: 'text', text: String(e) }], isError: true });
+ });
+
const result = await Promise.race([interruptPromise, resultPromise]);
if (interruptPromise.isDone())
mdbDebug('client call intercepted', result);
else
mdbDebug('client call result', result);
result.content.unshift(...this._progress);
+ this._progress.length = 0;
return result;
}
diff --git a/packages/playwright/src/mcp/sdk/server.ts b/packages/playwright/src/mcp/sdk/server.ts
index fbf2ed9ead..7169f79e93 100644
--- a/packages/playwright/src/mcp/sdk/server.ts
+++ b/packages/playwright/src/mcp/sdk/server.ts
@@ -199,13 +199,13 @@ function mergeTextParts(result: CallToolResult): CallToolResult {
continue;
}
if (testParts.length > 0) {
- content.push({ type: 'text', text: testParts.join('\n\n') });
+ content.push({ type: 'text', text: testParts.join('\n') });
testParts.length = 0;
}
content.push(part);
}
if (testParts.length > 0)
- content.push({ type: 'text', text: testParts.join('\n\n') });
+ content.push({ type: 'text', text: testParts.join('\n') });
return {
...result,
content,
diff --git a/packages/playwright/src/mcp/test/streams.ts b/packages/playwright/src/mcp/test/streams.ts
index d02664e103..f78eb82e3f 100644
--- a/packages/playwright/src/mcp/test/streams.ts
+++ b/packages/playwright/src/mcp/test/streams.ts
@@ -16,15 +16,20 @@
import { Writable } from 'stream';
+import type { ProgressCallback } from '../sdk/server';
+
export class StringWriteStream extends Writable {
- private _chunks: string[] = [];
+ private _progress: ProgressCallback;
- override _write(chunk: any, encoding: any, callback: any) {
- this._chunks.push(chunk.toString());
- callback();
+ constructor(progress: ProgressCallback) {
+ super();
+ this._progress = progress;
}
- content() {
- return this._chunks.join('');
+ override _write(chunk: any, encoding: any, callback: any) {
+ const text = chunk.toString();
+ // Progress wraps these as individual messages.
+ this._progress({ message: text.endsWith('\n') ? text.slice(0, -1) : text });
+ callback();
}
}
diff --git a/packages/playwright/src/mcp/test/testTools.ts b/packages/playwright/src/mcp/test/testTools.ts
index 74fdaf7c38..57e4662c68 100644
--- a/packages/playwright/src/mcp/test/testTools.ts
+++ b/packages/playwright/src/mcp/test/testTools.ts
@@ -29,6 +29,8 @@ import { defineTestTool } from './testTool';
import { StringWriteStream } from './streams';
import { fileExistsAsync } from '../../util';
+import type { ProgressCallback } from '../sdk/server';
+
export const listTests = defineTestTool({
schema: {
name: 'test_list',
@@ -38,15 +40,13 @@ export const listTests = defineTestTool({
type: 'readOnly',
},
- handle: async context => {
- const { screen, stream } = createScreen();
+ handle: async (context, _, progress) => {
+ const { screen } = createScreen(progress);
const reporter = new ListModeReporter({ screen, includeTestId: true });
const testRunner = await context.createTestRunner();
await testRunner.listTests(reporter, {});
- return {
- content: [{ type: 'text', text: stream.content() }],
- };
+ return { content: [] };
},
});
@@ -62,10 +62,10 @@ export const runTests = defineTestTool({
type: 'readOnly',
},
- handle: async (context, params) => {
- const { screen, stream } = createScreen();
+ handle: async (context, params, progress) => {
+ const { screen } = createScreen(progress);
const configDir = context.configLocation.configDir;
- const reporter = new ListReporter({ configDir, screen, includeTestId: true });
+ const reporter = new ListReporter({ configDir, screen, includeTestId: true, prefixStdio: 'out' });
const testRunner = await context.createTestRunner();
await testRunner.runTests(reporter, {
locations: params.locations,
@@ -73,12 +73,7 @@ export const runTests = defineTestTool({
disableConfigReporters: true,
});
- const text = stream.content();
- return {
- content: [
- { type: 'text', text },
- ]
- };
+ return { content: [] };
},
});
@@ -96,12 +91,12 @@ export const debugTest = defineTestTool({
type: 'readOnly',
},
- handle: async (context, params) => {
- const { screen, stream } = createScreen();
+ handle: async (context, params, progress) => {
+ const { screen } = createScreen(progress);
const configDir = context.configLocation.configDir;
- const reporter = new ListReporter({ configDir, screen });
+ const reporter = new ListReporter({ configDir, screen, includeTestId: true, prefixStdio: 'out' });
const testRunner = await context.createTestRunner();
- const result = await testRunner.runTests(reporter, {
+ await testRunner.runTests(reporter, {
headed: !context.options?.headless,
testIds: [params.test.id],
// For automatic recovery
@@ -111,13 +106,7 @@ export const debugTest = defineTestTool({
disableConfigReporters: true,
});
- const text = stream.content();
- return {
- content: [
- { type: 'text', text },
- ],
- isError: result.status !== 'passed',
- };
+ return { content: [] };
},
});
@@ -134,7 +123,7 @@ export const setupPage = defineTestTool({
},
handle: async (context, params, progress) => {
- const { screen, stream } = createScreen();
+ const { screen } = createScreen(progress);
const configDir = context.configLocation.configDir;
const reporter = new ListReporter({ configDir, screen });
const testRunner = await context.createTestRunner();
@@ -195,16 +184,14 @@ ${seedFileContent}
throw new Error('seed test not found.');
if (result.status !== 'passed')
- throw new Error(stream.content());
+ throw new Error('Errors while running the seed test.');
- return {
- content: [{ type: 'text', text: 'Done.' }],
- };
+ return { content: [] };
},
});
-function createScreen() {
- const stream = new StringWriteStream();
+function createScreen(progress: ProgressCallback) {
+ const stream = new StringWriteStream(progress);
const screen = {
...terminalScreen,
isTTY: false,
diff --git a/packages/playwright/src/reporters/list.ts b/packages/playwright/src/reporters/list.ts
index 9a08476da1..591883e3bf 100644
--- a/packages/playwright/src/reporters/list.ts
+++ b/packages/playwright/src/reporters/list.ts
@@ -38,10 +38,12 @@ class ListReporter extends TerminalReporter {
private _stepIndex = new Map<TestStep, string>();
private _needNewLine = false;
private _printSteps: boolean;
+ private _prefixStdio?: string;
- constructor(options?: ListReporterOptions & CommonReporterOptions & TerminalReporterOptions) {
+ constructor(options?: ListReporterOptions & CommonReporterOptions & TerminalReporterOptions & { prefixStdio?: string }) {
super(options);
this._printSteps = getAsBooleanFromENV('PLAYWRIGHT_LIST_PRINT_STEPS', options?.printSteps);
+ this._prefixStdio = options?.prefixStdio;
}
override onBegin(suite: Suite) {
@@ -68,12 +70,12 @@ class ListReporter extends TerminalReporter {
override onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
super.onStdOut(chunk, test, result);
- this._dumpToStdio(test, chunk, this.screen.stdout);
+ this._dumpToStdio(test, chunk, this.screen.stdout, 'out');
}
override onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
super.onStdErr(chunk, test, result);
- this._dumpToStdio(test, chunk, this.screen.stderr);
+ this._dumpToStdio(test, chunk, this.screen.stderr, 'err');
}
private getStepIndex(testIndex: string, result: TestResult, step: TestStep): string {
@@ -157,12 +159,15 @@ class ListReporter extends TerminalReporter {
}
}
- private _dumpToStdio(test: TestCase | undefined, chunk: string | Buffer, stream: NodeJS.WriteStream) {
+ private _dumpToStdio(test: TestCase | undefined, chunk: string | Buffer, stream: NodeJS.WriteStream, stdio: 'out' | 'err') {
if (this.config.quiet)
return;
const text = chunk.toString('utf-8');
this._updateLineCountAndNewLineFlagForOutput(text);
- stream.write(chunk);
+ if (this._prefixStdio)
+ stream.write(`[${stdio}] ${chunk}`);
+ else
+ stream.write(chunk);
}
override onTestEnd(test: TestCase, result: TestResult) {
diff --git a/tests/mcp/fixtures.ts b/tests/mcp/fixtures.ts
index a5efa2fa33..92ab0dd34d 100644
--- a/tests/mcp/fixtures.ts
+++ b/tests/mcp/fixtures.ts
@@ -191,7 +191,6 @@ async function createTransport(mcpServerType: TestOptions['mcpServerType'], args
stderr: 'pipe',
env: {
...env,
- DEBUG: process.env.DEBUG ? `${process.env.DEBUG},pw:mcp:test` : 'pw:mcp:test',
DEBUG_COLORS: '0',
DEBUG_HIDE_DATE: '1',
PWMCP_PROFILES_DIR_FOR_TEST: profilesDir,
diff --git a/tests/mcp/launch.spec.ts b/tests/mcp/launch.spec.ts
index 03a637b998..2d81dcc7d5 100644
--- a/tests/mcp/launch.spec.ts
+++ b/tests/mcp/launch.spec.ts
@@ -19,7 +19,11 @@ import fs from 'fs';
import { test, expect, formatOutput } from './fixtures';
test('test reopen browser', async ({ startClient, server }) => {
- const { client, stderr } = await startClient();
+ const { client, stderr } = await startClient({
+ env: {
+ DEBUG: 'pw:mcp:test',
+ }
+ });
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
diff --git a/tests/mcp/test-debug.spec.ts b/tests/mcp/test-debug.spec.ts
new file mode 100644
index 0000000000..772f6b325b
--- /dev/null
+++ b/tests/mcp/test-debug.spec.ts
@@ -0,0 +1,292 @@
+/**
+ * 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 { test, expect, writeFiles, prepareDebugTest } from './fixtures';
+
+test.use({ mcpServerType: 'test-mcp' });
+
+test('test_debug (passed)', async ({ startClient }) => {
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('pass', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `
+ });
+
+ const { client } = await startClient();
+ const listResult = await client.callTool({
+ name: 'test_list',
+ });
+ const [, id] = listResult.content[0].text.match(/\[id=([^\]]+)\]/);
+
+ expect(await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'pass' },
+ },
+ })).toHaveTextResponse(`
+Running 1 test using 1 worker
+ ok 1 [id=<ID>] a.test.ts:3:11 › pass (XXms)
+ 1 passed (XXms)`);
+});
+
+test('test_debug (pause/resume)', async ({ startClient }) => {
+ const { client, id } = await prepareDebugTest(startClient);
+
+ expect(await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ })).toHaveTextResponse(`
+Running 1 test using 1 worker
+### Paused on error:
+Error: expect(locator).toBeVisible() failed
+
+Locator: getByRole('button', { name: 'Missing' })
+Expected: visible
+Timeout: 1000ms
+Error: element(s) not found
+
+Call log:
+ - Expect "toBeVisible" with timeout 1000ms
+ - waiting for getByRole('button', { name: 'Missing' })
+
+
+### Page state
+- Page URL: about:blank
+- Page Title:
+- Page Snapshot:
+\`\`\`yaml
+- button "Submit" [ref=e2]
+\`\`\`
+
+### Task
+Try recovering from the error prior to continuing`);
+
+ expect(await client.callTool({
+ name: 'test_run',
+ arguments: {
+ locations: ['a.test.ts'],
+ },
+ })).toHaveTextResponse(expect.stringContaining(`1) [id=<ID>] a.test.ts:3:11 › fail`));
+});
+
+test('test_debug (browser_snapshot/network/console)', async ({ startClient, server }) => {
+ const { client, id } = await prepareDebugTest(startClient, `
+ import { test, expect } from '@playwright/test';
+ test('fail', async ({ page }) => {
+ await page.goto(${JSON.stringify(server.HELLO_WORLD)});
+ await page.evaluate(() => {
+ console.log('hello from console');
+ setTimeout(() => { throw new Error('error from page'); }, 0);
+ });
+ await expect(page.getByRole('button', { name: 'Missing' })).toBeVisible({ timeout: 1000 });
+ });
+ `);
+ await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ });
+ await expect.poll(() => client.callTool({
+ name: 'browser_network_requests',
+ })).toHaveResponse({
+ result: expect.stringContaining(`[GET] ${server.HELLO_WORLD} => [200] OK`),
+ });
+ expect(await client.callTool({
+ name: 'browser_console_messages',
+ })).toHaveResponse({
+ result: expect.stringMatching(/\[LOG\] hello from console.*\nError: error from page/),
+ });
+ expect(await client.callTool({
+ name: 'browser_snapshot',
+ })).toHaveResponse({
+ pageState: expect.stringContaining(`generic [active] [ref=e1]: Hello, world!`),
+ });
+});
+
+test('test_debug (multiple pages and custom errors)', async ({ startClient, server }) => {
+ const { client, id } = await prepareDebugTest(startClient, `
+ import { test, expect } from '@playwright/test';
+ test('fail', async ({ page }) => {
+ const page2 = await page.context().newPage();
+ await page.goto(${JSON.stringify(server.PREFIX + '/frames/frame.html')});
+ await page2.goto(${JSON.stringify(server.PREFIX + '/wrappedlink.html')});
+ throw new Error('non-api error');
+ });
+ `);
+ expect(await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ })).toHaveTextResponse(`
+Running 1 test using 1 worker
+### Paused on error:
+Error: non-api error
+
+### Page 1 of 2
+- Page URL: ${server.PREFIX + '/frames/frame.html'}
+- Page Title:
+- Page Snapshot:
+\`\`\`yaml
+- generic [ref=e2]: Hi, I'm frame
+\`\`\`
+
+### Page 2 of 2
+- Page URL: ${server.PREFIX + '/wrappedlink.html'}
+- Page Title:
+- Page Snapshot:
+\`\`\`yaml
+- link "123321" [ref=e3] [cursor=pointer]:
+ - /url: "#clicked"
+\`\`\`
+
+### Task
+Try recovering from the error prior to continuing`);
+});
+
+test('test_debug (pause/snapshot/resume)', async ({ startClient }) => {
+ const { client, id } = await prepareDebugTest(startClient);
+
+ expect(await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ })).toHaveTextResponse(`
+Running 1 test using 1 worker
+### Paused on error:
+Error: expect(locator).toBeVisible() failed
+
+Locator: getByRole('button', { name: 'Missing' })
+Expected: visible
+Timeout: 1000ms
+Error: element(s) not found
+
+Call log:
+ - Expect "toBeVisible" with timeout 1000ms
+ - waiting for getByRole('button', { name: 'Missing' })
+
+
+### Page state
+- Page URL: about:blank
+- Page Title:
+- Page Snapshot:
+\`\`\`yaml
+- button "Submit" [ref=e2]
+\`\`\`
+
+### Task
+Try recovering from the error prior to continuing`);
+
+ expect(await client.callTool({
+ name: 'browser_snapshot',
+ })).toHaveResponse({
+ pageState: expect.stringContaining(`- button \"Submit\" [ref=e2]`),
+ });
+
+ expect(await client.callTool({
+ name: 'test_run',
+ arguments: {
+ locations: ['a.test.ts'],
+ },
+ })).toHaveTextResponse(expect.stringContaining(`1) [id=<ID>] a.test.ts:3:11 › fail`));
+});
+
+test('test_debug / evaluate', async ({ startClient }) => {
+ const { client, id } = await prepareDebugTest(startClient);
+ await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ });
+ expect(await client.callTool({
+ name: 'browser_evaluate',
+ arguments: {
+ function: '() => 21+21',
+ },
+ })).toHaveResponse({
+ result: `42`,
+ });
+});
+
+test('test_debug / evaluate (with element)', async ({ startClient }) => {
+ const { client, id } = await prepareDebugTest(startClient);
+ await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ });
+ expect(await client.callTool({
+ name: 'browser_evaluate',
+ arguments: {
+ function: 'element => element.textContent',
+ element: 'button',
+ ref: 'e2',
+ },
+ })).toHaveResponse({
+ result: `"Submit"`,
+ });
+});
+
+test('test_debug / generate_locator', async ({ startClient }) => {
+ const { client, id } = await prepareDebugTest(startClient);
+ await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ });
+ expect(await client.callTool({
+ name: 'browser_generate_locator',
+ arguments: {
+ element: 'button',
+ ref: 'e2',
+ },
+ })).toHaveResponse({
+ result: `getByRole('button', { name: 'Submit' })`,
+ });
+});
+
+test('test_debug w/ console.log in test', async ({ startClient }) => {
+ const { client, id } = await prepareDebugTest(startClient, `
+ import { test, expect } from '@playwright/test';
+ test('fail', async ({ page }) => {
+ console.log('console.log');
+ console.error('console.error');
+ await expect(page.getByRole('button', { name: 'Missing' })).toBeVisible({ timeout: 1000 });
+ });
+ `);
+
+ expect(await client.callTool({
+ name: 'test_debug',
+ arguments: {
+ test: { id, title: 'fail' },
+ },
+ })).toHaveTextResponse(expect.stringContaining(`
+Running 1 test using 1 worker
+[out] console.log
+[err] console.error
+### Paused on error:
+Error: expect(locator).toBeVisible() failed`));
+});
diff --git a/tests/mcp/test-list.spec.ts b/tests/mcp/test-list.spec.ts
new file mode 100644
index 0000000000..af94d470b8
--- /dev/null
+++ b/tests/mcp/test-list.spec.ts
@@ -0,0 +1,47 @@
+/**
+ * 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 { test, expect, writeFiles } from './fixtures';
+
+test.use({ mcpServerType: 'test-mcp' });
+
+test('test_list', async ({ startClient }) => {
+ await writeFiles({
+ 'playwright.config.ts': `
+ module.exports = { projects: [{ name: 'foo' }, {}] };
+ `,
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('example1', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ test('example2', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `
+ });
+
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_list',
+ arguments: {},
+ })).toHaveTextResponse(`Listing tests:
+ [id=<ID>] [project=foo] › a.test.ts:3:11 › example1
+ [id=<ID>] [project=foo] › a.test.ts:6:11 › example2
+ [id=<ID>] a.test.ts:3:11 › example1
+ [id=<ID>] a.test.ts:6:11 › example2
+Total: 4 tests in 1 file`);
+});
diff --git a/tests/mcp/test-run.spec.ts b/tests/mcp/test-run.spec.ts
new file mode 100644
index 0000000000..291548066f
--- /dev/null
+++ b/tests/mcp/test-run.spec.ts
@@ -0,0 +1,166 @@
+/**
+ * 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 { test, expect, writeFiles } from './fixtures';
+
+test.use({ mcpServerType: 'test-mcp' });
+
+test('test_run', async ({ startClient }) => {
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('passes', () => {});
+ test('fails', () => { expect(1).toBe(2); });
+ test.describe('suite', () => {
+ test('inner passes', () => {});
+ test('inner fails', () => { expect(1).toBe(2); });
+ });
+ `,
+ 'b.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('passes', () => {});
+ test('fails', () => { expect(1).toBe(2); });
+ `,
+ 'c.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('passes', () => {});
+ test.skip('skipped', () => {});
+ `,
+ });
+
+ const { client } = await startClient();
+ const response = await client.callTool({
+ name: 'test_run',
+ });
+
+ const text = response.content[0].text;
+
+ expect(text).toContain(`3 failed`);
+ expect(text).toContain(`1 skipped`);
+ expect(text).toContain(`4 passed`);
+
+ expect(text).toContain(`a.test.ts:3:11 › passes`);
+ expect(text).toContain(`c.test.ts:3:11 › passes`);
+ expect(text).toContain(`c.test.ts:4:12 › skipped`);
+ expect(text).toContain(`b.test.ts:3:11 › passes`);
+ expect(text).toContain(`a.test.ts:4:11 › fails`);
+ expect(text).toContain(`b.test.ts:4:11 › fails`);
+ expect(text).toContain(`a.test.ts:6:13 › suite › inner passes`);
+ expect(text).toContain(`a.test.ts:7:13 › suite › inner fails`);
+
+ expect(text).not.toContain(`../../test-results`);
+});
+
+test('test_run for a failed tests is not an error', async ({ startClient }) => {
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('fails', () => { expect(1).toBe(2); });
+ `,
+ });
+
+ const { client } = await startClient();
+ const response = await client.callTool({
+ name: 'test_run',
+ });
+
+ const text = response.content[0].text;
+ // The tool run has succeeded, even though the test has failed.
+ expect(response.isError).toBeFalsy();
+
+ expect(text).toContain(`1 failed`);
+});
+
+test('test_run filters', async ({ startClient }) => {
+ await writeFiles({
+ 'playwright.config.ts': `
+ module.exports = { projects: [{ name: 'foo' }, { name: 'bar' }] };
+ `,
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('example1', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ test('example2', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `,
+ 'b.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('example1', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ test('example2', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `
+ });
+
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_run',
+ arguments: {
+ locations: ['b.test.ts'],
+ projects: ['foo'],
+ },
+ })).toHaveTextResponse(`
+Running 2 tests using 1 worker
+ ok 1 [id=<ID>] [project=foo] › b.test.ts:3:11 › example1 (XXms)
+ ok 2 [id=<ID>] [project=foo] › b.test.ts:6:11 › example2 (XXms)
+ 2 passed (XXms)`);
+});
+
+test('test_run should include dependencies', async ({ startClient }) => {
+ await writeFiles({
+ 'playwright.config.ts': `
+ module.exports = {
+ projects: [
+ { name: 'setup', testMatch: /.*setup\\.ts/ },
+ { name: 'chromium', dependencies: ['setup'] },
+ ],
+ };
+ `,
+ 'auth.setup.ts': `
+ import { test as setup, expect } from '@playwright/test';
+ setup('auth', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `,
+ 'example.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('example1', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ test('example2', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `
+ });
+
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_run',
+ arguments: {
+ locations: ['example.test.ts'],
+ projects: ['chromium'],
+ },
+ })).toHaveTextResponse(`
+Running 3 tests using 1 worker
+ ok 1 [id=<ID>] [project=setup] › auth.setup.ts:3:12 › auth (XXms)
+ ok 2 [id=<ID>] [project=chromium] › example.test.ts:3:11 › example1 (XXms)
+ ok 3 [id=<ID>] [project=chromium] › example.test.ts:6:11 › example2 (XXms)
+ 3 passed (XXms)`);
+});
diff --git a/tests/mcp/test-setup-page.spec.ts b/tests/mcp/test-setup-page.spec.ts
new file mode 100644
index 0000000000..cb34d60835
--- /dev/null
+++ b/tests/mcp/test-setup-page.spec.ts
@@ -0,0 +1,248 @@
+/**
+ * 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 { test, expect, writeFiles } from './fixtures';
+
+import fs from 'fs';
+import path from 'path';
+
+test.use({ mcpServerType: 'test-mcp' });
+
+test('test_setup_page', async ({ startClient }) => {
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test.beforeEach(async ({ page }) => {
+ await page.setContent('<button>Submit</button>');
+ });
+ test('template', async ({ page }) => {
+ });
+ `,
+ });
+
+ const { client } = await startClient();
+ const response = await client.callTool({
+ name: 'test_setup_page',
+ arguments: {
+ seedFile: 'a.test.ts',
+ },
+ });
+
+ expect(response).toHaveTextResponse(expect.stringContaining(`### Seed test\nFile: `));
+ expect(response).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction
+
+### Page state
+- Page URL: about:blank
+- Page Title:
+- Page Snapshot:
+\`\`\`yaml
+- button "Submit" [ref=e2]
+\`\`\`
+`));
+
+ expect(await client.callTool({
+ name: 'browser_click',
+ arguments: {
+ element: 'Submit button',
+ ref: 'e2',
+ },
+ })).toHaveResponse({
+ code: `await page.getByRole('button', { name: 'Submit' }).click();`,
+ pageState: expect.stringContaining(`- button "Submit"`),
+ });
+});
+
+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': `
+ module.exports = {
+ projects: [
+ { name: 'setup', testMatch: /.*setup\\.ts/ },
+ { name: 'chromium', dependencies: ['setup'] },
+ { name: 'ignored', dependencies: ['chromium'] },
+ ],
+ };
+ `,
+ 'auth.setup.ts': `
+ import { test as setup, expect } from '@playwright/test';
+ setup('auth', async ({ page }, testInfo) => {
+ require('fs').writeFileSync(testInfo.outputPath('auth.txt'), 'done');
+ });
+ `,
+ 'seed.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('template', async ({ page }, testInfo) => {
+ require('fs').writeFileSync(testInfo.outputPath('template.txt'), 'done');
+ });
+ `,
+ });
+
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_setup_page',
+ arguments: {
+ seedFile: 'seed.test.ts',
+ project: 'chromium',
+ },
+ })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction`));
+
+ // 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', '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({
+ 'seed.test.ts': `
+ throw new Error('loading error');
+ `,
+ });
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_setup_page',
+ arguments: {
+ seedFile: 'seed.test.ts',
+ },
+ })).toHaveTextResponse(expect.stringContaining('Error: loading error'));
+});
+
+test('test_setup_page (wrong test location)', async ({ startClient }) => {
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_setup_page',
+ arguments: {
+ seedFile: 'a.test.ts',
+ },
+ })).toHaveTextResponse(`### Result
+Error: seed test not found.`);
+});
+
+test('test_setup_page (no test location)', async ({ startClient }) => {
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_setup_page',
+ arguments: {},
+ })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction`));
+});
+
+test('test_setup_page chooses top-level project', async ({ startClient }) => {
+ const baseDir = await writeFiles({
+ 'playwright.config.ts': `
+ module.exports = {
+ projects: [
+ { name: 'one', testDir: './one' },
+ { name: 'two', testDir: './two', dependencies: ['one'] },
+ ],
+ };
+ `,
+ });
+
+ const { client } = await startClient();
+ expect(await client.callTool({
+ name: 'test_setup_page',
+ arguments: {},
+ })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction`));
+
+ 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 }) => {
+ 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(expect.stringContaining(`### Paused at end of test. ready for interaction`));
+
+ expect(fs.existsSync(test.info().outputPath('tests', 'seed.spec.ts'))).toBe(true);
+});
diff --git a/tests/mcp/test-tools.spec.ts b/tests/mcp/test-tools.spec.ts
deleted file mode 100644
index e60cb22f69..0000000000
--- a/tests/mcp/test-tools.spec.ts
+++ /dev/null
@@ -1,677 +0,0 @@
-/**
- * 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 { test, expect, writeFiles, prepareDebugTest } from './fixtures';
-
-import fs from 'fs';
-import path from 'path';
-
-test.use({ mcpServerType: 'test-mcp' });
-
-test('test_list', async ({ startClient }) => {
- await writeFiles({
- 'playwright.config.ts': `
- module.exports = { projects: [{ name: 'foo' }, {}] };
- `,
- 'a.test.ts': `
- import { test, expect } from '@playwright/test';
- test('example1', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- test('example2', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- `
- });
-
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_list',
- arguments: {},
- })).toHaveTextResponse(`Listing tests:
- [id=<ID>] [project=foo] › a.test.ts:3:11 › example1
- [id=<ID>] [project=foo] › a.test.ts:6:11 › example2
- [id=<ID>] a.test.ts:3:11 › example1
- [id=<ID>] a.test.ts:6:11 › example2
-Total: 4 tests in 1 file
-`);
-});
-
-test('test_run', async ({ startClient }) => {
- await writeFiles({
- 'a.test.ts': `
- import { test, expect } from '@playwright/test';
- test('passes', () => {});
- test('fails', () => { expect(1).toBe(2); });
- test.describe('suite', () => {
- test('inner passes', () => {});
- test('inner fails', () => { expect(1).toBe(2); });
- });
- `,
- 'b.test.ts': `
- import { test, expect } from '@playwright/test';
- test('passes', () => {});
- test('fails', () => { expect(1).toBe(2); });
- `,
- 'c.test.ts': `
- import { test, expect } from '@playwright/test';
- test('passes', () => {});
- test.skip('skipped', () => {});
- `,
- });
-
- const { client } = await startClient();
- const response = await client.callTool({
- name: 'test_run',
- });
-
- const text = response.content[0].text;
-
- expect(text).toContain(`3 failed`);
- expect(text).toContain(`1 skipped`);
- expect(text).toContain(`4 passed`);
-
- expect(text).toContain(`a.test.ts:3:11 › passes`);
- expect(text).toContain(`c.test.ts:3:11 › passes`);
- expect(text).toContain(`c.test.ts:4:12 › skipped`);
- expect(text).toContain(`b.test.ts:3:11 › passes`);
- expect(text).toContain(`a.test.ts:4:11 › fails`);
- expect(text).toContain(`b.test.ts:4:11 › fails`);
- expect(text).toContain(`a.test.ts:6:13 › suite › inner passes`);
- expect(text).toContain(`a.test.ts:7:13 › suite › inner fails`);
-
- expect(text).not.toContain(`../../test-results`);
-});
-
-test('test_run for a failed tests is not an error', async ({ startClient }) => {
- await writeFiles({
- 'a.test.ts': `
- import { test, expect } from '@playwright/test';
- test('fails', () => { expect(1).toBe(2); });
- `,
- });
-
- const { client } = await startClient();
- const response = await client.callTool({
- name: 'test_run',
- });
-
- const text = response.content[0].text;
- // The tool run has succeeded, even though the test has failed.
- expect(response.isError).toBeFalsy();
-
- expect(text).toContain(`1 failed`);
-});
-
-test('test_run filters', async ({ startClient }) => {
- await writeFiles({
- 'playwright.config.ts': `
- module.exports = { projects: [{ name: 'foo' }, { name: 'bar' }] };
- `,
- 'a.test.ts': `
- import { test, expect } from '@playwright/test';
- test('example1', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- test('example2', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- `,
- 'b.test.ts': `
- import { test, expect } from '@playwright/test';
- test('example1', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- test('example2', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- `
- });
-
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_run',
- arguments: {
- locations: ['b.test.ts'],
- projects: ['foo'],
- },
- })).toHaveTextResponse(`
-Running 2 tests using 1 worker
-
- ok 1 [id=<ID>] [project=foo] › b.test.ts:3:11 › example1 (XXms)
- ok 2 [id=<ID>] [project=foo] › b.test.ts:6:11 › example2 (XXms)
-
- 2 passed (XXms)
-`);
-});
-
-test('test_run should include dependencies', async ({ startClient }) => {
- await writeFiles({
- 'playwright.config.ts': `
- module.exports = {
- projects: [
- { name: 'setup', testMatch: /.*setup\\.ts/ },
- { name: 'chromium', dependencies: ['setup'] },
- ],
- };
- `,
- 'auth.setup.ts': `
- import { test as setup, expect } from '@playwright/test';
- setup('auth', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- `,
- 'example.test.ts': `
- import { test, expect } from '@playwright/test';
- test('example1', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- test('example2', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- `
- });
-
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_run',
- arguments: {
- locations: ['example.test.ts'],
- projects: ['chromium'],
- },
- })).toHaveTextResponse(`
-Running 3 tests using 1 worker
-
- ok 1 [id=<ID>] [project=setup] › auth.setup.ts:3:12 › auth (XXms)
- ok 2 [id=<ID>] [project=chromium] › example.test.ts:3:11 › example1 (XXms)
- ok 3 [id=<ID>] [project=chromium] › example.test.ts:6:11 › example2 (XXms)
-
- 3 passed (XXms)
-`);
-});
-
-test('test_debug (passed)', async ({ startClient }) => {
- await writeFiles({
- 'a.test.ts': `
- import { test, expect } from '@playwright/test';
- test('pass', async ({}) => {
- expect(1 + 1).toBe(2);
- });
- `
- });
-
- const { client } = await startClient();
- const listResult = await client.callTool({
- name: 'test_list',
- });
- const [, id] = listResult.content[0].text.match(/\[id=([^\]]+)\]/);
-
- expect(await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'pass' },
- },
- })).toHaveTextResponse(`
-Running 1 test using 1 worker
-
- ok 1 a.test.ts:3:11 › pass (XXms)
-
- 1 passed (XXms)
-`);
-});
-
-test('test_debug (pause/resume)', async ({ startClient }) => {
- const { client, id } = await prepareDebugTest(startClient);
-
- expect(await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'fail' },
- },
- })).toHaveTextResponse(`### Paused on error:
-Error: expect(locator).toBeVisible() failed
-
-Locator: getByRole('button', { name: 'Missing' })
-Expected: visible
-Timeout: 1000ms
-Error: element(s) not found
-
-Call log:
- - Expect "toBeVisible" with timeout 1000ms
- - waiting for getByRole('button', { name: 'Missing' })
-
-
-### Page state
-- Page URL: about:blank
-- Page Title:
-- Page Snapshot:
-\`\`\`yaml
-- button "Submit" [ref=e2]
-\`\`\`
-
-### Task
-Try recovering from the error prior to continuing`);
-
- expect(await client.callTool({
- name: 'test_run',
- arguments: {
- locations: ['a.test.ts'],
- },
- })).toHaveTextResponse(expect.stringContaining(`1) [id=<ID>] a.test.ts:3:11 › fail`));
-});
-
-test('test_debug (browser_snapshot/network/console)', async ({ startClient, server }) => {
- const { client, id } = await prepareDebugTest(startClient, `
- import { test, expect } from '@playwright/test';
- test('fail', async ({ page }) => {
- await page.goto(${JSON.stringify(server.HELLO_WORLD)});
- await page.evaluate(() => {
- console.log('hello from console');
- setTimeout(() => { throw new Error('error from page'); }, 0);
- });
- await expect(page.getByRole('button', { name: 'Missing' })).toBeVisible({ timeout: 1000 });
- });
- `);
- await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'fail' },
- },
- });
- await expect.poll(() => client.callTool({
- name: 'browser_network_requests',
- })).toHaveResponse({
- result: expect.stringContaining(`[GET] ${server.HELLO_WORLD} => [200] OK`),
- });
- expect(await client.callTool({
- name: 'browser_console_messages',
- })).toHaveResponse({
- result: expect.stringMatching(/\[LOG\] hello from console.*\nError: error from page/),
- });
- expect(await client.callTool({
- name: 'browser_snapshot',
- })).toHaveResponse({
- pageState: expect.stringContaining(`generic [active] [ref=e1]: Hello, world!`),
- });
-});
-
-test('test_debug (multiple pages and custom errors)', async ({ startClient, server }) => {
- const { client, id } = await prepareDebugTest(startClient, `
- import { test, expect } from '@playwright/test';
- test('fail', async ({ page }) => {
- const page2 = await page.context().newPage();
- await page.goto(${JSON.stringify(server.PREFIX + '/frames/frame.html')});
- await page2.goto(${JSON.stringify(server.PREFIX + '/wrappedlink.html')});
- throw new Error('non-api error');
- });
- `);
- expect(await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'fail' },
- },
- })).toHaveTextResponse(`### Paused on error:
-Error: non-api error
-
-### Page 1 of 2
-- Page URL: ${server.PREFIX + '/frames/frame.html'}
-- Page Title:
-- Page Snapshot:
-\`\`\`yaml
-- generic [ref=e2]: Hi, I'm frame
-\`\`\`
-
-### Page 2 of 2
-- Page URL: ${server.PREFIX + '/wrappedlink.html'}
-- Page Title:
-- Page Snapshot:
-\`\`\`yaml
-- link "123321" [ref=e3] [cursor=pointer]:
- - /url: "#clicked"
-\`\`\`
-
-### Task
-Try recovering from the error prior to continuing`);
-});
-
-test('test_debug (pause/snapshot/resume)', async ({ startClient }) => {
- const { client, id } = await prepareDebugTest(startClient);
-
- expect(await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'fail' },
- },
- })).toHaveTextResponse(`### Paused on error:
-Error: expect(locator).toBeVisible() failed
-
-Locator: getByRole('button', { name: 'Missing' })
-Expected: visible
-Timeout: 1000ms
-Error: element(s) not found
-
-Call log:
- - Expect "toBeVisible" with timeout 1000ms
- - waiting for getByRole('button', { name: 'Missing' })
-
-
-### Page state
-- Page URL: about:blank
-- Page Title:
-- Page Snapshot:
-\`\`\`yaml
-- button "Submit" [ref=e2]
-\`\`\`
-
-### Task
-Try recovering from the error prior to continuing`);
-
- expect(await client.callTool({
- name: 'browser_snapshot',
- })).toHaveResponse({
- pageState: expect.stringContaining(`- button \"Submit\" [ref=e2]`),
- });
-
- expect(await client.callTool({
- name: 'test_run',
- arguments: {
- locations: ['a.test.ts'],
- },
- })).toHaveTextResponse(expect.stringContaining(`1) [id=<ID>] a.test.ts:3:11 › fail`));
-});
-
-test('test_debug / evaluate', async ({ startClient }) => {
- const { client, id } = await prepareDebugTest(startClient);
- await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'fail' },
- },
- });
- expect(await client.callTool({
- name: 'browser_evaluate',
- arguments: {
- function: '() => 21+21',
- },
- })).toHaveResponse({
- result: `42`,
- });
-});
-
-test('test_debug / evaluate (with element)', async ({ startClient }) => {
- const { client, id } = await prepareDebugTest(startClient);
- await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'fail' },
- },
- });
- expect(await client.callTool({
- name: 'browser_evaluate',
- arguments: {
- function: 'element => element.textContent',
- element: 'button',
- ref: 'e2',
- },
- })).toHaveResponse({
- result: `"Submit"`,
- });
-});
-
-test('test_debug / generate_locator', async ({ startClient }) => {
- const { client, id } = await prepareDebugTest(startClient);
- await client.callTool({
- name: 'test_debug',
- arguments: {
- test: { id, title: 'fail' },
- },
- });
- expect(await client.callTool({
- name: 'browser_generate_locator',
- arguments: {
- element: 'button',
- ref: 'e2',
- },
- })).toHaveResponse({
- result: `getByRole('button', { name: 'Submit' })`,
- });
-});
-
-test('test_setup_page', async ({ startClient }) => {
- await writeFiles({
- 'a.test.ts': `
- import { test, expect } from '@playwright/test';
- test.beforeEach(async ({ page }) => {
- await page.setContent('<button>Submit</button>');
- });
- test('template', async ({ page }) => {
- });
- `,
- });
-
- const { client } = await startClient();
- const response = await client.callTool({
- name: 'test_setup_page',
- arguments: {
- seedFile: 'a.test.ts',
- },
- });
-
- expect(response).toHaveTextResponse(expect.stringContaining(`### Seed test\nFile: `));
- expect(response).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction
-
-### Page state
-- Page URL: about:blank
-- Page Title:
-- Page Snapshot:
-\`\`\`yaml
-- button "Submit" [ref=e2]
-\`\`\`
-`));
-
- expect(await client.callTool({
- name: 'browser_click',
- arguments: {
- element: 'Submit button',
- ref: 'e2',
- },
- })).toHaveResponse({
- code: `await page.getByRole('button', { name: 'Submit' }).click();`,
- pageState: expect.stringContaining(`- button "Submit"`),
- });
-});
-
-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': `
- module.exports = {
- projects: [
- { name: 'setup', testMatch: /.*setup\\.ts/ },
- { name: 'chromium', dependencies: ['setup'] },
- { name: 'ignored', dependencies: ['chromium'] },
- ],
- };
- `,
- 'auth.setup.ts': `
- import { test as setup, expect } from '@playwright/test';
- setup('auth', async ({ page }, testInfo) => {
- require('fs').writeFileSync(testInfo.outputPath('auth.txt'), 'done');
- });
- `,
- 'seed.test.ts': `
- import { test, expect } from '@playwright/test';
- test('template', async ({ page }, testInfo) => {
- require('fs').writeFileSync(testInfo.outputPath('template.txt'), 'done');
- });
- `,
- });
-
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_setup_page',
- arguments: {
- seedFile: 'seed.test.ts',
- project: 'chromium',
- },
- })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction`));
-
- // 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', '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({
- 'seed.test.ts': `
- throw new Error('loading error');
- `,
- });
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_setup_page',
- arguments: {
- seedFile: 'seed.test.ts',
- },
- })).toHaveTextResponse(expect.stringContaining('Error: loading error'));
-});
-
-test('test_setup_page (wrong test location)', async ({ startClient }) => {
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_setup_page',
- arguments: {
- seedFile: 'a.test.ts',
- },
- })).toHaveTextResponse(`### Result
-Error: seed test not found.`);
-});
-
-test('test_setup_page (no test location)', async ({ startClient }) => {
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_setup_page',
- arguments: {},
- })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction`));
-});
-
-test('test_setup_page chooses top-level project', async ({ startClient }) => {
- const baseDir = await writeFiles({
- 'playwright.config.ts': `
- module.exports = {
- projects: [
- { name: 'one', testDir: './one' },
- { name: 'two', testDir: './two', dependencies: ['one'] },
- ],
- };
- `,
- });
-
- const { client } = await startClient();
- expect(await client.callTool({
- name: 'test_setup_page',
- arguments: {},
- })).toHaveTextResponse(expect.stringContaining(`### Paused at end of test. ready for interaction`));
-
- 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 }) => {
- 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(expect.stringContaining(`### Paused at end of test. ready for interaction`));
-
- expect(fs.existsSync(test.info().outputPath('tests', 'seed.spec.ts'))).toBe(true);
-});