Skip to content

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

chore(test mcp): simplify pause handling in test backend (#38205)

Dmitry Gozmand18d7d9890ff
diff --git a/packages/playwright/src/mcp/sdk/tool.ts b/packages/playwright/src/mcp/sdk/tool.ts
index 6441ff787e..0f8ccaa0cb 100644
--- a/packages/playwright/src/mcp/sdk/tool.ts
+++ b/packages/playwright/src/mcp/sdk/tool.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { z as zod, zodToJsonSchema } from '../sdk/bundle';
+import { zodToJsonSchema } from '../sdk/bundle';
import type { z } from 'zod';
import type * as mcpServer from './server';
@@ -27,17 +27,12 @@ export type ToolSchema<Input extends z.Schema> = {
type: 'input' | 'assertion' | 'action' | 'readOnly';
};
-const typesWithIntent = ['action', 'assertion', 'input'];
-
-export function toMcpTool(tool: ToolSchema<any>, options?: { addIntent?: boolean }): mcpServer.Tool {
- const inputSchema = options?.addIntent && typesWithIntent.includes(tool.type) ? tool.inputSchema.extend({
- intent: zod.string().describe('The intent of the call, for example the test step description plan idea')
- }) : tool.inputSchema;
+export function toMcpTool(tool: ToolSchema<any>): mcpServer.Tool {
const readOnly = tool.type === 'readOnly' || tool.type === 'assertion';
return {
name: tool.name,
description: tool.description,
- inputSchema: zodToJsonSchema(inputSchema, { strictUnions: true }) as mcpServer.Tool['inputSchema'],
+ inputSchema: zodToJsonSchema(tool.inputSchema, { strictUnions: true }) as mcpServer.Tool['inputSchema'],
annotations: {
title: tool.title,
readOnlyHint: readOnly,
diff --git a/packages/playwright/src/mcp/test/DEPS.list b/packages/playwright/src/mcp/test/DEPS.list
index 23b556217d..385682de8c 100644
--- a/packages/playwright/src/mcp/test/DEPS.list
+++ b/packages/playwright/src/mcp/test/DEPS.list
@@ -5,6 +5,7 @@
../browser/response.ts
../browser/tab.ts
../browser/tools/
+../log.ts
../../reporters
../../runner
../../transform
diff --git a/packages/playwright/src/mcp/test/generatorTools.ts b/packages/playwright/src/mcp/test/generatorTools.ts
index fbb6be740c..57eec9f478 100644
--- a/packages/playwright/src/mcp/test/generatorTools.ts
+++ b/packages/playwright/src/mcp/test/generatorTools.ts
@@ -34,11 +34,11 @@ export const setupPage = defineTestTool({
type: 'readOnly',
},
- handle: async (context, params, progress) => {
+ handle: async (context, params) => {
const seed = await context.getOrCreateSeedFile(params.seedFile, params.project);
context.generatorJournal = new GeneratorJournal(context.rootPath, params.plan, seed);
- await context.runSeedTest(seed.file, seed.projectName, progress);
- return { content: [] };
+ const { output, status } = await context.runSeedTest(seed.file, seed.projectName);
+ return { content: [{ type: 'text', text: output }], isError: status !== 'paused' };
},
});
diff --git a/packages/playwright/src/mcp/test/plannerTools.ts b/packages/playwright/src/mcp/test/plannerTools.ts
index e405adc9aa..45fa39fd52 100644
--- a/packages/playwright/src/mcp/test/plannerTools.ts
+++ b/packages/playwright/src/mcp/test/plannerTools.ts
@@ -32,10 +32,10 @@ export const setupPage = defineTestTool({
type: 'readOnly',
},
- handle: async (context, params, progress) => {
+ handle: async (context, params) => {
const seed = await context.getOrCreateSeedFile(params.seedFile, params.project);
- await context.runSeedTest(seed.file, seed.projectName, progress);
- return { content: [] };
+ const { output, status } = await context.runSeedTest(seed.file, seed.projectName);
+ return { content: [{ type: 'text', text: output }], isError: status !== 'paused' };
},
});
diff --git a/packages/playwright/src/mcp/test/streams.ts b/packages/playwright/src/mcp/test/streams.ts
index 4513411016..4b8f3cae61 100644
--- a/packages/playwright/src/mcp/test/streams.ts
+++ b/packages/playwright/src/mcp/test/streams.ts
@@ -18,22 +18,22 @@ import { Writable } from 'stream';
import { stripAnsiEscapes } from '../../util';
-import type { ProgressCallback } from '../sdk/server';
-
export class StringWriteStream extends Writable {
- private _progress: ProgressCallback;
+ private _output: string[];
private _prefix: string;
- constructor(progress: ProgressCallback, stdio: 'stdout' | 'stderr') {
+ constructor(output: string[], stdio: 'stdout' | 'stderr') {
super();
- this._progress = progress;
+ this._output = output;
this._prefix = stdio === 'stdout' ? '' : '[err] ';
}
override _write(chunk: any, encoding: any, callback: any) {
- const text = stripAnsiEscapes(chunk.toString());
- // Progress wraps these as individual messages.
- this._progress({ message: `${this._prefix}${text.endsWith('\n') ? text.slice(0, -1) : text}` });
+ let text = stripAnsiEscapes(chunk.toString());
+ if (text.endsWith('\n'))
+ text = text.slice(0, -1);
+ if (text)
+ this._output.push(this._prefix + text);
callback();
}
}
diff --git a/packages/playwright/src/mcp/test/testBackend.ts b/packages/playwright/src/mcp/test/testBackend.ts
index 0f87aa5ba0..e49c032201 100644
--- a/packages/playwright/src/mcp/test/testBackend.ts
+++ b/packages/playwright/src/mcp/test/testBackend.ts
@@ -14,22 +14,16 @@
* limitations under the License.
*/
-import { debug } from 'playwright-core/lib/utilsBundle';
-import { ManualPromise } from 'playwright-core/lib/utils';
-
import * as mcp from '../sdk/exports';
import { TestContext } from './testContext';
import * as testTools from './testTools.js';
import * as generatorTools from './generatorTools.js';
import * as plannerTools from './plannerTools.js';
import { browserTools } from '../browser/tools';
-import { resolveConfigLocation } from '../../common/configLoader';
-import { parseResponse } from '../browser/response';
+import { z as zod } from '../sdk/bundle';
import type { TestTool } from './testTool';
-import type { BrowserMCPRequest, BrowserMCPResponse } from './browserBackend';
-
-const errorsDebug = debug('pw:mcp:errors');
+import type { Tool as BrowserTool } from '../browser/tools/tool';
export class TestServerBackend implements mcp.ServerBackend {
readonly name = 'Playwright';
@@ -44,103 +38,55 @@ export class TestServerBackend implements mcp.ServerBackend {
testTools.listTests,
testTools.runTests,
testTools.debugTest,
+ ...browserTools.map(tool => wrapBrowserTool(tool)),
];
- private _context: TestContext;
+ private _options: { muteConsole?: boolean, headless?: boolean };
+ private _context: TestContext | undefined;
private _configOption: string | undefined;
- private _clientInfo: mcp.ClientInfo | undefined;
- private _onPauseClient: { sendMessage: (request: BrowserMCPRequest) => Promise<BrowserMCPResponse>, tools: mcp.Tool[] } | undefined;
- private _interruptPromise: ManualPromise<mcp.CallToolResult> | undefined;
- private _progress: mcp.CallToolResult['content'] = [];
- private _progressCallback: mcp.ProgressCallback;
constructor(configOption: string | undefined, options?: { muteConsole?: boolean, headless?: boolean }) {
- this._context = new TestContext(this._pushClient.bind(this), options);
+ this._options = options || {};
this._configOption = configOption;
- this._progressCallback = (params: mcp.ProgressParams) => {
- if (params.message)
- this._progress.push({ type: 'text', text: params.message });
- };
- }
-
- private async _pushClient(sendMessage: (request: BrowserMCPRequest) => Promise<BrowserMCPResponse>) {
- try {
- const initializeResponse = await sendMessage({ initialize: { clientInfo: this._clientInfo! } });
- const listToolsResponse = await sendMessage({ listTools: {} });
- const tools = listToolsResponse.listTools!;
- this._onPauseClient = { sendMessage, tools };
- this._interruptPromise?.resolve({
- content: [{
- type: 'text',
- text: initializeResponse.initialize!.pausedMessage,
- }],
- });
- this._interruptPromise = undefined;
- } catch {
- }
}
async initialize(clientInfo: mcp.ClientInfo): Promise<void> {
- this._clientInfo = clientInfo;
- const rootPath = mcp.firstRootPath(clientInfo);
-
- if (this._configOption) {
- this._context.initialize(rootPath, resolveConfigLocation(this._configOption));
- return;
- }
-
- if (rootPath) {
- this._context.initialize(rootPath, resolveConfigLocation(rootPath));
- return;
- }
-
- this._context.initialize(rootPath, resolveConfigLocation(undefined));
+ this._context = new TestContext(clientInfo, this._configOption, this._options);
}
async listTools(): Promise<mcp.Tool[]> {
- return [
- ...this._tools.map(tool => mcp.toMcpTool(tool.schema)),
- ...browserTools.map(tool => mcp.toMcpTool(tool.schema, { addIntent: true })),
- ];
+ return this._tools.map(tool => mcp.toMcpTool(tool.schema));
}
async callTool(name: string, args: mcp.CallToolRequest['params']['arguments']): Promise<mcp.CallToolResult> {
- if (this._onPauseClient?.tools.find(tool => tool.name === name)) {
- const callToolRespone = await this._onPauseClient.sendMessage({ callTool: { name, arguments: args } });
- const result = callToolRespone.callTool!;
- const response = parseResponse(result);
- if (response && !response.isError && response.code && typeof args?.['intent'] === 'string')
- this._context.generatorJournal?.logStep(args['intent'], response.code);
- return result;
- }
-
- await this._onPauseClient?.sendMessage({ close: {} }).catch(errorsDebug);
- this._onPauseClient = undefined;
-
- const resultPromise = new ManualPromise<mcp.CallToolResult>();
- const interruptPromise = new ManualPromise<mcp.CallToolResult>();
- this._interruptPromise = interruptPromise;
-
- this._callTestTool(name, args).then(result => {
- resultPromise.resolve(result);
- }).catch(e => {
- resultPromise.resolve({ content: [{ type: 'text', text: String(e) }], isError: true });
- });
-
- const result = await Promise.race([interruptPromise, resultPromise]);
- result.content.unshift(...this._progress);
- this._progress.length = 0;
- return result;
- }
-
- private async _callTestTool(name: string, args: mcp.CallToolRequest['params']['arguments']): Promise<mcp.CallToolResult> {
const tool = this._tools.find(tool => tool.schema.name === name);
if (!tool)
throw new Error(`Tool not found: ${name}. Available tools: ${this._tools.map(tool => tool.schema.name).join(', ')}`);
- const parsedArguments = tool.schema.inputSchema.parse(args || {});
- return await tool.handle(this._context!, parsedArguments, this._progressCallback);
+ try {
+ return await tool.handle(this._context!, tool.schema.inputSchema.parse(args || {}));
+ } catch (e) {
+ return { content: [{ type: 'text', text: String(e) }], isError: true };
+ }
}
serverClosed() {
void this._context!.close();
}
}
+
+const typesWithIntent = ['action', 'assertion', 'input'];
+
+function wrapBrowserTool(tool: BrowserTool): TestTool {
+ const inputSchema = typesWithIntent.includes(tool.schema.type) ? (tool.schema.inputSchema as any).extend({
+ intent: zod.string().describe('The intent of the call, for example the test step description plan idea')
+ }) : tool.schema.inputSchema;
+ return {
+ schema: {
+ ...tool.schema,
+ inputSchema,
+ },
+ handle: async (context: TestContext, params: any) => {
+ const response = await context.sendMessageToPausedTest({ callTool: { name: tool.schema.name, arguments: params } });
+ return response.callTool!;
+ },
+ };
+}
diff --git a/packages/playwright/src/mcp/test/testContext.ts b/packages/playwright/src/mcp/test/testContext.ts
index 63d5aa0319..dc17cd2133 100644
--- a/packages/playwright/src/mcp/test/testContext.ts
+++ b/packages/playwright/src/mcp/test/testContext.ts
@@ -18,7 +18,7 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
-import { noColors, escapeRegExp } from 'playwright-core/lib/utils';
+import { noColors, escapeRegExp, ManualPromise } from 'playwright-core/lib/utils';
import { terminalScreen } from '../../reporters/base';
import ListReporter from '../../reporters/list';
@@ -26,9 +26,16 @@ import { StringWriteStream } from './streams';
import { fileExistsAsync } from '../../util';
import { TestRunner, TestRunnerEvent } from '../../runner/testRunner';
import { ensureSeedFile, seedProject } from './seed';
+import { firstRootPath } from '../sdk/exports';
+import { resolveConfigLocation } from '../../common/configLoader';
+import { parseResponse } from '../browser/response';
+import { logUnhandledError } from '../log';
+import type { TerminalScreen } from '../../reporters/base';
+import type { FullResultStatus, RunTestsParams } from '../../runner/testRunner';
import type { ConfigLocation } from '../../common/config';
-import type { ProgressCallback } from '../sdk/exports';
+import type { ClientInfo } from '../sdk/exports';
+import type { BrowserMCPRequest, BrowserMCPResponse } from './browserBackend';
export type SeedFile = {
file: string;
@@ -71,55 +78,77 @@ ${step.code}
}
}
-type PushClientCallback = (sendMessage: (request: any) => Promise<any>) => Promise<void>;
+type TestRunnerAndScreen = {
+ testRunner: TestRunner;
+ screen: TerminalScreen;
+ claimStdio: () => void;
+ releaseStdio: () => void;
+ output: string[];
+ waitForTestPaused: () => Promise<void>;
+ sendMessageToPausedTest?: (params: { request: BrowserMCPRequest }) => Promise<{ response: BrowserMCPResponse, error?: any }>;
+};
export class TestContext {
- private _pushClient: PushClientCallback;
- private _testRunner: TestRunner | undefined;
- readonly options?: { muteConsole?: boolean, headless?: boolean };
+ private _clientInfo: ClientInfo;
+ private _testRunnerAndScreen: TestRunnerAndScreen | undefined;
readonly computedHeaded: boolean;
- configLocation!: ConfigLocation;
- rootPath!: string;
+ private readonly _configLocation: ConfigLocation;
+ readonly rootPath: string;
generatorJournal: GeneratorJournal | undefined;
- constructor(pushClient: PushClientCallback, options?: { muteConsole?: boolean, headless?: boolean }) {
- this._pushClient = pushClient;
- this.options = options;
+ constructor(clientInfo: ClientInfo, configPath: string | undefined, options?: { muteConsole?: boolean, headless?: boolean }) {
+ this._clientInfo = clientInfo;
+
+ const rootPath = firstRootPath(clientInfo);
+ this._configLocation = resolveConfigLocation(configPath || rootPath);
+ this.rootPath = rootPath || this._configLocation.configDir;
+
if (options?.headless !== undefined)
this.computedHeaded = !options.headless;
else
this.computedHeaded = !process.env.CI && !(os.platform() === 'linux' && !process.env.DISPLAY);
}
- initialize(rootPath: string | undefined, configLocation: ConfigLocation) {
- this.configLocation = configLocation;
- this.rootPath = rootPath || configLocation.configDir;
+ existingTestRunner(): TestRunner | undefined {
+ return this._testRunnerAndScreen?.testRunner;
}
- existingTestRunner(): TestRunner | undefined {
- return this._testRunner;
+ private async _cleanupTestRunner() {
+ if (!this._testRunnerAndScreen)
+ return;
+ await this._testRunnerAndScreen.testRunner.stopTests();
+ this._testRunnerAndScreen.claimStdio();
+ try {
+ await this._testRunnerAndScreen.testRunner.runGlobalTeardown();
+ } finally {
+ this._testRunnerAndScreen.releaseStdio();
+ this._testRunnerAndScreen = undefined;
+ }
}
- async createTestRunner(): Promise<TestRunner> {
- if (this._testRunner)
- await this._testRunner.stopTests();
- const testRunner = new TestRunner(this.configLocation!, {});
+ async createTestRunner() {
+ await this._cleanupTestRunner();
+
+ const testRunner = new TestRunner(this._configLocation, {});
await testRunner.initialize({});
+ const testPaused = new ManualPromise<void>();
+ const testRunnerAndScreen: TestRunnerAndScreen = {
+ ...createScreen(),
+ testRunner,
+ waitForTestPaused: () => testPaused,
+ };
+ this._testRunnerAndScreen = testRunnerAndScreen;
+
testRunner.on(TestRunnerEvent.TestPaused, params => {
- void this._pushClient(async (request: any) => {
- const response = await params.sendMessage({ request });
- if (response.error)
- throw new Error(response.error.message);
- return response.response;
- });
+ testRunnerAndScreen.sendMessageToPausedTest = params.sendMessage;
+ testPaused.resolve();
});
- this._testRunner = testRunner;
- return testRunner;
+ return testRunnerAndScreen;
}
async getOrCreateSeedFile(seedFile: string | undefined, projectName: string | undefined) {
- const configDir = this.configLocation.configDir;
- const testRunner = await this.createTestRunner();
+ const configDir = this._configLocation.configDir;
+ const { testRunner } = await this.createTestRunner();
const config = await testRunner.loadConfig();
const project = seedProject(config, projectName);
@@ -151,63 +180,101 @@ export class TestContext {
};
}
- async runSeedTest(seedFile: string, projectName: string, progress: ProgressCallback) {
- await this.runWithGlobalSetup(async (testRunner, reporter) => {
- const result = await testRunner.runTests(reporter, {
- headed: this.computedHeaded,
- locations: ['/' + escapeRegExp(seedFile) + '/'],
- projects: [projectName],
- timeout: 0,
- workers: 1,
- pauseAtEnd: true,
- disableConfigReporters: true,
- failOnLoadErrors: true,
- });
- // 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)
- throw new Error('seed test not found.');
-
- if (result.status !== 'passed')
- throw new Error('Errors while running the seed test.');
- }, progress);
+ async runSeedTest(seedFile: string, projectName: string): Promise<{ output: string, status: FullResultStatus | 'paused' }> {
+ const result = await this.runTestsWithGlobalSetupAndPossiblePause({
+ headed: this.computedHeaded,
+ locations: ['/' + escapeRegExp(seedFile) + '/'],
+ projects: [projectName],
+ timeout: 0,
+ workers: 1,
+ pauseAtEnd: true,
+ disableConfigReporters: true,
+ failOnLoadErrors: true,
+ });
+ if (result.status === 'passed')
+ result.output += '\nError: seed test not found.';
+ else if (result.status !== 'paused')
+ result.output += '\nError while running the seed test.';
+ return result;
}
- async runWithGlobalSetup(
- callback: (testRunner: TestRunner, reporter: ListReporter) => Promise<void>,
- progress: ProgressCallback): Promise<void> {
- const { screen, claimStdio, releaseStdio } = createScreen(progress);
- const configDir = this.configLocation.configDir;
- const testRunner = await this.createTestRunner();
+ async runTestsWithGlobalSetupAndPossiblePause(params: RunTestsParams): Promise<{ output: string, status: FullResultStatus | 'paused' }> {
+ const configDir = this._configLocation.configDir;
+ const testRunnerAndScreen = await this.createTestRunner();
+ const { testRunner, screen, claimStdio, releaseStdio } = testRunnerAndScreen;
claimStdio();
try {
const setupReporter = new ListReporter({ configDir, screen, includeTestId: true });
const { status } = await testRunner.runGlobalSetup([setupReporter]);
if (status !== 'passed')
- throw new Error('Failed to run global setup');
+ return { output: testRunnerAndScreen.output.join('\n'), status };
} finally {
releaseStdio();
}
- try {
- const reporter = new ListReporter({ configDir, screen, includeTestId: true });
- return await callback(testRunner, reporter);
- } finally {
+ let status: FullResultStatus | 'paused' = 'passed';
+
+ const cleanup = async () => {
claimStdio();
- await testRunner.runGlobalTeardown().finally(() => {
+ try {
+ const result = await testRunner.runGlobalTeardown();
+ if (status === 'passed')
+ status = result.status;
+ } finally {
releaseStdio();
- });
+ }
+ };
+
+ try {
+ const reporter = new ListReporter({ configDir, screen, includeTestId: true });
+ status = await Promise.race([
+ testRunner.runTests(reporter, params).then(result => result.status),
+ testRunnerAndScreen.waitForTestPaused().then(() => 'paused' as const),
+ ]);
+
+ if (status === 'paused') {
+ const response = await testRunnerAndScreen.sendMessageToPausedTest!({ request: { initialize: { clientInfo: this._clientInfo } } });
+ if (response.error)
+ throw new Error(response.error.message);
+ testRunnerAndScreen.output.push(response.response.initialize!.pausedMessage);
+ return { output: testRunnerAndScreen.output.join('\n'), status };
+ }
+ } catch (e) {
+ status = 'failed';
+ testRunnerAndScreen.output.push(String(e));
+ await cleanup();
+ return { output: testRunnerAndScreen.output.join('\n'), status };
}
+
+ await cleanup();
+ return { output: testRunnerAndScreen.output.join('\n'), status };
}
async close() {
+ await this._cleanupTestRunner().catch(logUnhandledError);
+ }
+
+ async sendMessageToPausedTest(request: BrowserMCPRequest): Promise<BrowserMCPResponse> {
+ const sendMessage = this._testRunnerAndScreen?.sendMessageToPausedTest;
+ if (!sendMessage)
+ throw new Error('Must setup test before interacting with the page');
+ const result = await sendMessage({ request });
+ if (result.error)
+ throw new Error(result.error.message);
+ if (typeof request?.callTool?.arguments?.['intent'] === 'string') {
+ const response = parseResponse(result.response.callTool!);
+ if (response && !response.isError && response.code)
+ this.generatorJournal?.logStep(request.callTool.arguments['intent'], response.code);
+ }
+ return result.response;
}
}
-export function createScreen(progress: ProgressCallback) {
- const stdout = new StringWriteStream(progress, 'stdout');
- const stderr = new StringWriteStream(progress, 'stderr');
+export function createScreen() {
+ const output: string[] = [];
+ const stdout = new StringWriteStream(output, 'stdout');
+ const stderr = new StringWriteStream(output, 'stderr');
const screen = {
...terminalScreen,
@@ -238,7 +305,7 @@ export function createScreen(progress: ProgressCallback) {
};
/* eslint-enable no-restricted-properties */
- return { screen, claimStdio, releaseStdio };
+ return { screen, claimStdio, releaseStdio, output };
}
const bestPracticesMarkdown = `
diff --git a/packages/playwright/src/mcp/test/testTool.ts b/packages/playwright/src/mcp/test/testTool.ts
index b1dbbe32a7..05a3e5212b 100644
--- a/packages/playwright/src/mcp/test/testTool.ts
+++ b/packages/playwright/src/mcp/test/testTool.ts
@@ -18,11 +18,10 @@ import type { z } from 'zod';
import type { TestContext } from './testContext.js';
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import type { ToolSchema } from '../sdk/tool.js';
-import type { ProgressCallback } from '../sdk/server.js';
export type TestTool<Input extends z.Schema = z.Schema> = {
schema: ToolSchema<Input>;
- handle: (context: TestContext, params: z.output<Input>, progress: ProgressCallback) => Promise<CallToolResult>;
+ handle: (context: TestContext, params: z.output<Input>) => Promise<CallToolResult>;
};
export function defineTestTool<Input extends z.Schema>(tool: TestTool<Input>): TestTool<Input> {
diff --git a/packages/playwright/src/mcp/test/testTools.ts b/packages/playwright/src/mcp/test/testTools.ts
index 5087a3ba19..2c57ecc81c 100644
--- a/packages/playwright/src/mcp/test/testTools.ts
+++ b/packages/playwright/src/mcp/test/testTools.ts
@@ -17,7 +17,6 @@
import { z } from '../sdk/bundle';
import ListModeReporter from '../../reporters/listModeReporter';
import { defineTestTool } from './testTool';
-import { createScreen } from './testContext';
export const listTests = defineTestTool({
schema: {
@@ -28,13 +27,11 @@ export const listTests = defineTestTool({
type: 'readOnly',
},
- handle: async (context, _, progress) => {
- const { screen } = createScreen(progress);
+ handle: async context => {
+ const { testRunner, screen, output } = await context.createTestRunner();
const reporter = new ListModeReporter({ screen, includeTestId: true });
- const testRunner = await context.createTestRunner();
await testRunner.listTests(reporter, {});
-
- return { content: [] };
+ return { content: output.map(text => ({ type: 'text', text })) };
},
});
@@ -50,16 +47,13 @@ export const runTests = defineTestTool({
type: 'readOnly',
},
- handle: async (context, params, progress) => {
- await context.runWithGlobalSetup(async (testRunner, reporter) => {
- await testRunner.runTests(reporter, {
- locations: params.locations,
- projects: params.projects,
- disableConfigReporters: true,
- });
- }, progress);
-
- return { content: [] };
+ handle: async (context, params) => {
+ const { output } = await context.runTestsWithGlobalSetupAndPossiblePause({
+ locations: params.locations,
+ projects: params.projects,
+ disableConfigReporters: true,
+ });
+ return { content: [{ type: 'text', text: output }] };
},
});
@@ -77,20 +71,17 @@ export const debugTest = defineTestTool({
type: 'readOnly',
},
- handle: async (context, params, progress) => {
- await context.runWithGlobalSetup(async (testRunner, reporter) => {
- await testRunner.runTests(reporter, {
- headed: context.computedHeaded,
- testIds: [params.test.id],
- // For automatic recovery
- timeout: 0,
- workers: 1,
- pauseOnError: true,
- disableConfigReporters: true,
- actionTimeout: 5000,
- });
- }, progress);
-
- return { content: [] };
+ handle: async (context, params) => {
+ const { output, status } = await context.runTestsWithGlobalSetupAndPossiblePause({
+ headed: context.computedHeaded,
+ testIds: [params.test.id],
+ // For automatic recovery
+ timeout: 0,
+ workers: 1,
+ pauseOnError: true,
+ disableConfigReporters: true,
+ actionTimeout: 5000,
+ });
+ return { content: [{ type: 'text', text: output }], isError: status !== 'paused' && status !== 'passed' };
},
});
diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.ts
index 8f5dce0440..426c90d19c 100644
--- a/packages/playwright/src/runner/testRunner.ts
+++ b/packages/playwright/src/runner/testRunner.ts
@@ -82,7 +82,7 @@ export type RunTestsParams = {
failOnLoadErrors?: boolean;
};
-type FullResultStatus = reporterTypes.FullResult['status'];
+export type FullResultStatus = reporterTypes.FullResult['status'];
export class TestRunner extends EventEmitter<TestRunnerEventMap> {
readonly configLocation: ConfigLocation;
diff --git a/tests/mcp/planner.spec.ts b/tests/mcp/planner.spec.ts
index 1917660b52..30fbe74283 100644
--- a/tests/mcp/planner.spec.ts
+++ b/tests/mcp/planner.spec.ts
@@ -58,6 +58,7 @@ test('planner_setup_page', async ({ startClient }) => {
arguments: {
element: 'Submit button',
ref: 'e2',
+ intent: 'Click on the "Submit" button',
},
})).toHaveResponse({
code: `await page.getByRole('button', { name: 'Submit' }).click();`,
diff --git a/tests/mcp/test-debug.spec.ts b/tests/mcp/test-debug.spec.ts
index b0f1a87581..b060380e12 100644
--- a/tests/mcp/test-debug.spec.ts
+++ b/tests/mcp/test-debug.spec.ts
@@ -223,6 +223,7 @@ test('test_debug / evaluate', async ({ startClient }) => {
name: 'browser_evaluate',
arguments: {
function: '() => 21+21',
+ intent: 'Calculate 21+21',
},
})).toHaveResponse({
result: `42`,
@@ -241,6 +242,7 @@ test('test_debug / evaluate x 2', async ({ startClient }) => {
name: 'browser_evaluate',
arguments: {
function: '() => 21+21',
+ intent: 'Calculate 21+21',
},
})).toHaveResponse({
result: `42`,
@@ -254,13 +256,15 @@ test('test_debug / evaluate x 2', async ({ startClient }) => {
})).toEqual({
content: [
{ type: 'text', text: expect.stringContaining(`Paused on error`) },
- ]
+ ],
+ isError: false,
});
expect(await client.callTool({
name: 'browser_evaluate',
arguments: {
function: '() => 21+23',
+ intent: 'Calculate 21+23',
},
})).toHaveResponse({
result: `44`,
@@ -281,6 +285,7 @@ test('test_debug / evaluate (with element)', async ({ startClient }) => {
function: 'element => element.textContent',
element: 'button',
ref: 'e2',
+ intent: 'Get button text',
},
})).toHaveResponse({
result: `"Submit"`,
@@ -427,7 +432,7 @@ Error: expect(locator).toBeVisible() failed`));
expect(await client.callTool({
name: 'browser_navigate',
- arguments: { url: server.HELLO_WORLD },
+ arguments: { url: server.HELLO_WORLD, intent: 'Go to hello world' },
})).toHaveResponse({
pageState: expect.stringContaining(`- Page URL: ${server.HELLO_WORLD}\n- Page Title: Title2`),
});