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: remove mdb, replace by `sendMessage` ipc into test worker (#38170)
Dmitry Gozman34a09f37c7f8
diff --git a/packages/playwright/src/common/ipc.ts b/packages/playwright/src/common/ipc.tsindex 0ddcf9ff19..4d9ca7f846 100644--- a/packages/playwright/src/common/ipc.ts+++ b/packages/playwright/src/common/ipc.ts@@ -87,8 +87,18 @@ export type AttachmentPayload = {export type TestInfoErrorImpl = TestInfoError;export type TestPausedPayload = {+ testId: string;errors: TestInfoErrorImpl[];- extraData: any;+};++export type CustomMessageRequestPayload = {+ testId: string;+ request: any;+};++export type CustomMessageResponsePayload = {+ response: any;+ error?: TestInfoErrorImpl;};export type TestEndPayload = {diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.tsindex 82144035ac..f398caaf37 100644--- a/packages/playwright/src/index.ts+++ b/packages/playwright/src/index.ts@@ -22,7 +22,7 @@ import { setBoxedStackPrefixes, createGuid, currentZone, debugMode, jsonStringifimport { currentTestInfo } from './common/globals';import { rootTestType } from './common/testType';-import { runBrowserBackendOnTestPause } from './mcp/test/browserBackend';+import { createCustomMessageHandler } from './mcp/test/browserBackend';import type { Fixtures, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, ScreenshotMode, TestInfo, TestType, VideoMode } from '../types/test';import type { ContextReuseMode } from './common/config';@@ -417,14 +417,14 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({attachConnectedHeaderIfNeeded(testInfo, browserImpl);if (!_reuseContext) {const { context, close } = await _contextFactory();- (testInfo as TestInfoImpl)._onDidPauseTestCallback = () => runBrowserBackendOnTestPause(testInfo, context);+ (testInfo as TestInfoImpl)._onCustomMessageCallback = createCustomMessageHandler(testInfo, context);await use(context);await close();return;}const context = await browserImpl._wrapApiCall(() => browserImpl._newContextForReuse(), { internal: true });- (testInfo as TestInfoImpl)._onDidPauseTestCallback = () => runBrowserBackendOnTestPause(testInfo, context);+ (testInfo as TestInfoImpl)._onCustomMessageCallback = createCustomMessageHandler(testInfo, context);await use(context);const closeReason = testInfo.status === 'timedOut' ? 'Test timeout of ' + testInfo.timeout + 'ms exceeded.' : 'Test ended.';await browserImpl._wrapApiCall(() => browserImpl._disconnectFromReusedContext(closeReason), { internal: true });diff --git a/packages/playwright/src/mcp/browser/browserServerBackend.ts b/packages/playwright/src/mcp/browser/browserServerBackend.tsindex e9ec5c0945..9387f86f8a 100644--- a/packages/playwright/src/mcp/browser/browserServerBackend.ts+++ b/packages/playwright/src/mcp/browser/browserServerBackend.ts@@ -40,7 +40,7 @@ export class BrowserServerBackend implements ServerBackend {this._tools = filteredTools(config);}- async initialize(server: mcpServer.Server, clientInfo: mcpServer.ClientInfo): Promise<void> {+ async initialize(clientInfo: mcpServer.ClientInfo): Promise<void> {this._sessionLog = this._config.saveSession ? await SessionLog.create(this._config, clientInfo) : undefined;this._context = new Context({config: this._config,diff --git a/packages/playwright/src/mcp/sdk/exports.ts b/packages/playwright/src/mcp/sdk/exports.tsindex 92c0c176ba..b30e94b9be 100644--- a/packages/playwright/src/mcp/sdk/exports.ts+++ b/packages/playwright/src/mcp/sdk/exports.ts@@ -19,4 +19,3 @@ export * from './proxyBackend';export * from './server';export * from './tool';export * from './http';-export * from './mdb';diff --git a/packages/playwright/src/mcp/sdk/mdb.ts b/packages/playwright/src/mcp/sdk/mdb.tsdeleted file mode 100644index 06f4263ff0..0000000000--- a/packages/playwright/src/mcp/sdk/mdb.ts+++ /dev/null@@ -1,143 +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 { debug } from 'playwright-core/lib/utilsBundle';-import { ManualPromise } from 'playwright-core/lib/utils';--import * as mcpBundle from './bundle';-import * as mcpServer from './server';-import * as mcpHttp from './http';--import type { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';-import type { Client } from '@modelcontextprotocol/sdk/client/index.js';--const mdbDebug = debug('pw:mcp:mdb');-const errorsDebug = debug('pw:mcp:errors');--export type MDBPushClientCallback = (mcpUrl: string, introMessage?: string) => Promise<void>;-export type MDBServerBackendFactory = Omit<mcpServer.ServerBackendFactory, 'create'> & {- create: (pushClient: MDBPushClientCallback) => mcpServer.ServerBackend;-};--export class MDBBackend implements mcpServer.ServerBackend {- private _onPauseClient: { client: Client, tools: mcpServer.Tool[], transport: StreamableHTTPClientTransport } | undefined;- private _interruptPromise: ManualPromise<mcpServer.CallToolResult> | undefined;- private _mainBackend: mcpServer.ServerBackend;- private _clientInfo: mcpServer.ClientInfo | undefined;- private _progress: mcpServer.CallToolResult['content'] = [];- private _progressCallback: mcpServer.ProgressCallback;-- constructor(mainBackendFactory: MDBServerBackendFactory) {- this._mainBackend = mainBackendFactory.create(this._createOnPauseClient.bind(this));- this._progressCallback = (params: mcpServer.ProgressParams) => {- if (params.message)- this._progress.push({ type: 'text', text: params.message });- };- }-- async initialize(server: mcpServer.Server, clientInfo: mcpServer.ClientInfo): Promise<void> {- if (!this._clientInfo) {- this._clientInfo = clientInfo;- await this._mainBackend.initialize?.(server, clientInfo);- }- }-- async listTools(): Promise<mcpServer.Tool[]> {- return await this._mainBackend.listTools();- }-- async callTool(name: string, args: mcpServer.CallToolRequest['params']['arguments']): Promise<mcpServer.CallToolResult> {- if (this._onPauseClient?.tools.find(tool => tool.name === name)) {- const result = await this._onPauseClient.client.callTool({- name,- arguments: args,- }) as mcpServer.CallToolResult;- await this._mainBackend.afterCallTool?.(name, args, result);- return result;- }-- await this._onPauseClient?.transport.terminateSession().catch(errorsDebug);- await this._onPauseClient?.client.close().catch(errorsDebug);- this._onPauseClient = undefined;-- const resultPromise = new ManualPromise<mcpServer.CallToolResult>();- const interruptPromise = new ManualPromise<mcpServer.CallToolResult>();- this._interruptPromise = interruptPromise;-- this._mainBackend.callTool(name, args, this._progressCallback).then(result => {- resultPromise.resolve(result as mcpServer.CallToolResult);- }).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;- }-- private async _createOnPauseClient(mcpUrl: string, introMessage?: string) {- if (this._onPauseClient)- await this._onPauseClient.client.close().catch(errorsDebug);-- this._onPauseClient = await this._createClient(mcpUrl);-- this._interruptPromise?.resolve({- content: [{- type: 'text',- text: introMessage || '',- }],- });- this._interruptPromise = undefined;- }-- private async _createClient(url: string): Promise<{ client: Client, tools: mcpServer.Tool[], transport: StreamableHTTPClientTransport }> {- const client = new mcpBundle.Client({ name: 'Interrupting client', version: '0.0.0' }, { capabilities: { roots: {} } });- client.setRequestHandler(mcpBundle.ListRootsRequestSchema, () => ({ roots: this._clientInfo?.roots || [] }));- client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));- client.setNotificationHandler(mcpBundle.ProgressNotificationSchema, notification => {- if (notification.method === 'notifications/progress') {- const { message } = notification.params;- if (message)- this._progress.push({ type: 'text', text: message });- }- });- const transport = new mcpBundle.StreamableHTTPClientTransport(new URL(url));- await client.connect(transport);- const { tools } = await client.listTools();- return { client, tools, transport };- }-}--// TODO: add all options from mcpHttp.startHttpServer.-export async function runMainBackend(backendFactory: MDBServerBackendFactory, options?: { port?: number }): Promise<string | undefined> {- const mdbBackend = new MDBBackend(backendFactory);- const factory: mcpServer.ServerBackendFactory = {- ...backendFactory,- create: () => mdbBackend- };-- if (options?.port !== undefined) {- const httpServer = await mcpHttp.startHttpServer(options);- return await mcpHttp.installHttpTransport(httpServer, factory, true);- }-- await mcpServer.connect(factory, new mcpBundle.StdioServerTransport(), false);-}diff --git a/packages/playwright/src/mcp/sdk/proxyBackend.ts b/packages/playwright/src/mcp/sdk/proxyBackend.tsindex c55e5f97d6..ba8fc790f4 100644--- a/packages/playwright/src/mcp/sdk/proxyBackend.ts+++ b/packages/playwright/src/mcp/sdk/proxyBackend.ts@@ -18,7 +18,7 @@ import { debug } from 'playwright-core/lib/utilsBundle';import * as mcpBundle from './bundle';-import type { ServerBackend, ClientInfo, Server } from './server';+import type { ServerBackend, ClientInfo } from './server';import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';import type { Tool, CallToolResult, CallToolRequest } from '@modelcontextprotocol/sdk/types.js';import type { Client } from '@modelcontextprotocol/sdk/client/index.js';@@ -43,7 +43,7 @@ export class ProxyBackend implements ServerBackend {this._contextSwitchTool = this._defineContextSwitchTool();}- async initialize(server: Server, clientInfo: ClientInfo): Promise<void> {+ async initialize(clientInfo: ClientInfo): Promise<void> {this._clientInfo = clientInfo;}diff --git a/packages/playwright/src/mcp/sdk/server.ts b/packages/playwright/src/mcp/sdk/server.tsindex 1488f2990f..d6a60e3e14 100644--- a/packages/playwright/src/mcp/sdk/server.ts+++ b/packages/playwright/src/mcp/sdk/server.ts@@ -42,9 +42,8 @@ export type ProgressParams = { message?: string, progress?: number, total?: numbexport type ProgressCallback = (params: ProgressParams) => void;export interface ServerBackend {- initialize?(server: Server, clientInfo: ClientInfo): Promise<void>;+ initialize?(clientInfo: ClientInfo): Promise<void>;listTools(): Promise<Tool[]>;- afterCallTool?(name: string, args: CallToolRequest['params']['arguments'], result: CallToolResult): Promise<void>;callTool(name: string, args: CallToolRequest['params']['arguments'], progress: ProgressCallback): Promise<CallToolResult>;serverClosed?(server: Server): void;}@@ -135,7 +134,7 @@ const initializeServer = async (server: Server, backend: ServerBackend, runHearttimestamp: Date.now(),};- await backend.initialize?.(server, clientInfo);+ await backend.initialize?.(clientInfo);if (runHeartbeat)startHeartbeat(server);};diff --git a/packages/playwright/src/mcp/test/browserBackend.ts b/packages/playwright/src/mcp/test/browserBackend.tsindex f60a9844ac..a362acda63 100644--- a/packages/playwright/src/mcp/test/browserBackend.ts+++ b/packages/playwright/src/mcp/test/browserBackend.ts@@ -15,23 +15,75 @@*/import * as mcp from '../sdk/exports';-import { defaultConfig, FullConfig } from '../browser/config';+import { defaultConfig } from '../browser/config';import { BrowserServerBackend } from '../browser/browserServerBackend';import { Tab } from '../browser/tab';+import { stripAnsiEscapes } from '../../util';import type * as playwright from '../../../index';import type { Page } from '../../../../playwright-core/src/client/page';import type { BrowserContextFactory } from '../browser/browserContextFactory';import type { TestInfo } from '../../../test';-export type TestPausedExtraData = {- mcpUrl: string;- contextState: string;+export type BrowserMCPRequest = {+ initialize?: { clientInfo: mcp.ClientInfo },+ listTools?: {},+ callTool?: { name: string, arguments: mcp.CallToolRequest['params']['arguments'] },+ close?: {},};-export async function runBrowserBackendOnTestPause(testInfo: TestInfo, context: playwright.BrowserContext) {+export type BrowserMCPResponse = {+ initialize?: { pausedMessage: string },+ listTools?: mcp.Tool[],+ callTool?: mcp.CallToolResult,+ close?: {},+};++export function createCustomMessageHandler(testInfo: TestInfo, context: playwright.BrowserContext) {+ let backend: BrowserServerBackend | undefined;+ return async (data: BrowserMCPRequest): Promise<BrowserMCPResponse> => {+ if (data.initialize) {+ if (backend)+ throw new Error('MCP backend is already initialized');+ backend = new BrowserServerBackend({ ...defaultConfig, capabilities: ['testing'] }, identityFactory(context));+ await backend.initialize(data.initialize.clientInfo);+ const pausedMessage = await generatePausedMessage(testInfo, context);+ return { initialize: { pausedMessage } };+ }++ if (data.listTools) {+ if (!backend)+ throw new Error('MCP backend is not initialized');+ return { listTools: await backend.listTools() };+ }++ if (data.callTool) {+ if (!backend)+ throw new Error('MCP backend is not initialized');+ return { callTool: await backend.callTool(data.callTool.name, data.callTool.arguments) };+ }++ if (data.close) {+ backend?.serverClosed();+ backend = undefined;+ return { close: {} };+ }++ throw new Error('Unknown MCP request');+ };+}++async function generatePausedMessage(testInfo: TestInfo, context: playwright.BrowserContext) {const lines: string[] = [];+ if (testInfo.errors.length) {+ lines.push(`### Paused on error:`);+ for (const error of testInfo.errors)+ lines.push(stripAnsiEscapes(error.message || ''));+ } else {+ lines.push(`### Paused at end of test. ready for interaction`);+ }+for (let i = 0; i < context.pages().length; i++) {const page = context.pages()[i];const stateSuffix = context.pages().length > 1 ? (i + 1) + ' of ' + (context.pages().length) : 'state';@@ -57,24 +109,11 @@ export async function runBrowserBackendOnTestPause(testInfo: TestInfo, context:);}- const config: FullConfig = {- ...defaultConfig,- capabilities: ['testing'],- };+ lines.push('');+ if (testInfo.errors.length)+ lines.push(`### Task`, `Try recovering from the error prior to continuing`);- const factory: mcp.ServerBackendFactory = {- name: 'Playwright',- nameInConfig: 'playwright',- version: '0.0.0',- create: () => new BrowserServerBackend(config, identityFactory(context))- };- const httpServer = await mcp.startHttpServer({ port: 0 });- const mcpUrl = await mcp.installHttpTransport(httpServer, factory, true);- const dispose = async () => {- await new Promise(cb => httpServer.close(cb));- };- const extraData = { mcpUrl, contextState: lines.join('\n') } as TestPausedExtraData;- return { extraData, dispose };+ return lines.join('\n');}function identityFactory(browserContext: playwright.BrowserContext): BrowserContextFactory {diff --git a/packages/playwright/src/mcp/test/testBackend.ts b/packages/playwright/src/mcp/test/testBackend.tsindex 60dbd83574..0f87aa5ba0 100644--- a/packages/playwright/src/mcp/test/testBackend.ts+++ b/packages/playwright/src/mcp/test/testBackend.ts@@ -14,6 +14,9 @@* 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';@@ -24,6 +27,9 @@ import { resolveConfigLocation } from '../../common/configLoader';import { parseResponse } from '../browser/response';import type { TestTool } from './testTool';+import type { BrowserMCPRequest, BrowserMCPResponse } from './browserBackend';++const errorsDebug = debug('pw:mcp:errors');export class TestServerBackend implements mcp.ServerBackend {readonly name = 'Playwright';@@ -41,13 +47,40 @@ export class TestServerBackend implements mcp.ServerBackend {];private _context: TestContext;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, pushClient: mcp.MDBPushClientCallback, options?: { muteConsole?: boolean, headless?: boolean }) {- this._context = new TestContext(pushClient, options);+ constructor(configOption: string | undefined, options?: { muteConsole?: boolean, headless?: boolean }) {+ this._context = new TestContext(this._pushClient.bind(this), 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(server: mcp.Server, clientInfo: mcp.ClientInfo): Promise<void> {+ async initialize(clientInfo: mcp.ClientInfo): Promise<void> {+ this._clientInfo = clientInfo;const rootPath = mcp.firstRootPath(clientInfo);if (this._configOption) {@@ -70,20 +103,41 @@ export class TestServerBackend implements mcp.ServerBackend {];}- async afterCallTool(name: string, args: mcp.CallToolRequest['params']['arguments'], result: mcp.CallToolResult) {- if (!browserTools.find(tool => tool.schema.name === name))- return;- const response = parseResponse(result);- if (response && !response.isError && response.code && typeof args?.['intent'] === 'string')- this._context.generatorJournal?.logStep(args['intent'], response.code);+ 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;}- async callTool(name: string, args: mcp.CallToolRequest['params']['arguments'], progress: mcp.ProgressCallback): Promise<mcp.CallToolResult> {+ 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, progress);+ return await tool.handle(this._context!, parsedArguments, this._progressCallback);}serverClosed() {diff --git a/packages/playwright/src/mcp/test/testContext.ts b/packages/playwright/src/mcp/test/testContext.tsindex d556d806a5..63d5aa0319 100644--- a/packages/playwright/src/mcp/test/testContext.ts+++ b/packages/playwright/src/mcp/test/testContext.ts@@ -23,13 +23,12 @@ import { noColors, escapeRegExp } from 'playwright-core/lib/utils';import { terminalScreen } from '../../reporters/base';import ListReporter from '../../reporters/list';import { StringWriteStream } from './streams';-import { stripAnsiEscapes, fileExistsAsync } from '../../util';+import { fileExistsAsync } from '../../util';import { TestRunner, TestRunnerEvent } from '../../runner/testRunner';import { ensureSeedFile, seedProject } from './seed';import type { ConfigLocation } from '../../common/config';-import type { ProgressCallback, MDBPushClientCallback } from '../sdk/exports';-import type { TestPausedExtraData } from './browserBackend';+import type { ProgressCallback } from '../sdk/exports';export type SeedFile = {file: string;@@ -72,8 +71,10 @@ ${step.code}}}+type PushClientCallback = (sendMessage: (request: any) => Promise<any>) => Promise<void>;+export class TestContext {- private _pushClient: MDBPushClientCallback;+ private _pushClient: PushClientCallback;private _testRunner: TestRunner | undefined;readonly options?: { muteConsole?: boolean, headless?: boolean };readonly computedHeaded: boolean;@@ -81,7 +82,7 @@ export class TestContext {rootPath!: string;generatorJournal: GeneratorJournal | undefined;- constructor(pushClient: MDBPushClientCallback, options?: { muteConsole?: boolean, headless?: boolean }) {+ constructor(pushClient: PushClientCallback, options?: { muteConsole?: boolean, headless?: boolean }) {this._pushClient = pushClient;this.options = options;if (options?.headless !== undefined)@@ -104,25 +105,13 @@ export class TestContext {await this._testRunner.stopTests();const testRunner = new TestRunner(this.configLocation!, {});await testRunner.initialize({});- this._testRunner = testRunner;- testRunner.on(TestRunnerEvent.TestFilesChanged, testFiles => {- this._testRunner?.emit(TestRunnerEvent.TestFilesChanged, testFiles);- });testRunner.on(TestRunnerEvent.TestPaused, params => {- const extraData = params.extraData as TestPausedExtraData;- const introMessage: string[] = [];- if (params.errors.length) {- introMessage.push(`### Paused on error:`);- for (const error of params.errors)- introMessage.push(stripAnsiEscapes(error.message || ''));- } else {- introMessage.push(`### Paused at end of test. ready for interaction`);- }- introMessage.push(extraData.contextState);- introMessage.push('');- if (params.errors.length)- introMessage.push(`### Task`, `Try recovering from the error prior to continuing`);- void this._pushClient(extraData.mcpUrl, introMessage.join('\n'));+ void this._pushClient(async (request: any) => {+ const response = await params.sendMessage({ request });+ if (response.error)+ throw new Error(response.error.message);+ return response.response;+ });});this._testRunner = testRunner;return testRunner;diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.tsindex 5e58a10260..922ea766e0 100644--- a/packages/playwright/src/program.ts+++ b/packages/playwright/src/program.ts@@ -33,7 +33,7 @@ import * as testServer from './runner/testServer';import { runWatchModeLoop } from './runner/watchMode';import { runAllTestsWithConfig, TestRunner } from './runner/testRunner';import { createErrorCollectingReporter } from './runner/reporters';-import { MDBServerBackendFactory, runMainBackend } from './mcp/sdk/exports';+import * as mcp from './mcp/sdk/exports';import { TestServerBackend } from './mcp/test/testBackend';import { decorateCommand } from './mcp/program';import { setupExitWatchdog } from './mcp/browser/watchdog';@@ -162,20 +162,14 @@ function addTestMCPServerCommand(program: Command) {command.option('--port <port>', 'port to listen on for SSE transport.');command.action(async options => {setupExitWatchdog();- const backendFactory: MDBServerBackendFactory = {+ const factory: mcp.ServerBackendFactory = {name: 'Playwright Test Runner',nameInConfig: 'playwright-test-runner',version: packageJSON.version,- create: pushClient => new TestServerBackend(options.config, pushClient, { muteConsole: options.port === undefined, headless: options.headless }),+ create: () => new TestServerBackend(options.config, { muteConsole: options.port === undefined, headless: options.headless }),};- const mdbUrl = await runMainBackend(- backendFactory,- {- port: options.port === undefined ? undefined : +options.port- },- );- if (mdbUrl)- console.error('MCP Listening on: ', mdbUrl);+ // TODO: add all options from mcp.startHttpServer.+ await mcp.start(factory, { port: options.port === undefined ? undefined : +options.port, host: options.host });});}diff --git a/packages/playwright/src/runner/dispatcher.ts b/packages/playwright/src/runner/dispatcher.tsindex 89d15427be..d2e122fa53 100644--- a/packages/playwright/src/runner/dispatcher.ts+++ b/packages/playwright/src/runner/dispatcher.ts@@ -21,6 +21,7 @@ import { addSuggestedRebaseline } from './rebase';import { WorkerHost } from './workerHost';import { serializeConfig } from '../common/ipc';import { addLocationAndSnippetToError } from '../reporters/internalReporter';+import { serializeError } from '../util';import type { FailureTracker } from './failureTracker';import type { ProcessExitData } from './processHost';@@ -577,16 +578,33 @@ class JobDispatcher {eventsHelper.addEventListener(worker, 'stepBegin', this._onStepBegin.bind(this)),eventsHelper.addEventListener(worker, 'stepEnd', this._onStepEnd.bind(this)),eventsHelper.addEventListener(worker, 'attach', this._onAttach.bind(this)),- eventsHelper.addEventListener(worker, 'testPaused', (params: TestPausedPayload) => {- for (const error of params.errors)- addLocationAndSnippetToError(this._config.config, error);- this._failureTracker.onTestPaused?.(params);- }),+ eventsHelper.addEventListener(worker, 'testPaused', this._onTestPaused.bind(this, worker)),eventsHelper.addEventListener(worker, 'done', this._onDone.bind(this)),eventsHelper.addEventListener(worker, 'exit', this.onExit.bind(this)),];}+ private _onTestPaused(worker: WorkerHost, params: TestPausedPayload) {+ const sendMessage = async (message: { request: any }) => {+ try {+ if (this.jobResult.isDone())+ throw new Error('Test has already stopped');+ const response = await worker.sendCustomMessage({ testId: params.testId, request: message.request });+ if (response.error)+ addLocationAndSnippetToError(this._config.config, response.error);+ return response;+ } catch (e) {+ const error = serializeError(e);+ addLocationAndSnippetToError(this._config.config, error);+ return { response: undefined, error };+ }+ };++ for (const error of params.errors)+ addLocationAndSnippetToError(this._config.config, error);+ this._failureTracker.onTestPaused?.({ ...params, sendMessage });+ }+skipWholeJob(): boolean {// If all the tests in a group are skipped, we report them immediately// without sending anything to a worker. This avoids creating unnecessary worker processes.diff --git a/packages/playwright/src/runner/failureTracker.ts b/packages/playwright/src/runner/failureTracker.tsindex e1b518d2b9..6e6ef85b95 100644--- a/packages/playwright/src/runner/failureTracker.ts+++ b/packages/playwright/src/runner/failureTracker.ts@@ -18,6 +18,11 @@ import type { TestResult, TestError } from '../../types/testReporter';import type { FullConfigInternal, FullProjectInternal } from '../common/config';import type { Suite, TestCase } from '../common/test';+export type TestPausedParams = {+ errors: TestError[];+ sendMessage: (params: { request: any }) => Promise<{ response: any, error?: TestError }>;+};+export class FailureTracker {private _config: FullConfigInternal;private _failureCount = 0;@@ -26,7 +31,7 @@ export class FailureTracker {private _topLevelProjects: FullProjectInternal[] = [];private _pauseOnError: boolean;private _pauseAtEnd: boolean;- onTestPaused?: (params: { errors: TestError[], extraData: any }) => void;+ onTestPaused?: (params: TestPausedParams) => void;constructor(config: FullConfigInternal, options?: { pauseOnError?: boolean, pauseAtEnd?: boolean }) {this._config = config;diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.tsindex 3b3c1450e6..8f5dce0440 100644--- a/packages/playwright/src/runner/testRunner.ts+++ b/packages/playwright/src/runner/testRunner.ts@@ -39,6 +39,7 @@ import type { ConfigLocation, FullConfigInternal } from '../common/config';import type { ConfigCLIOverrides } from '../common/ipc';import type { TestRunnerPluginRegistration } from '../plugins';import type { AnyReporter } from '../reporters/reporterV2';+import type { TestPausedParams } from './failureTracker';export const TestRunnerEvent = {TestFilesChanged: 'testFilesChanged',@@ -47,7 +48,7 @@ export const TestRunnerEvent = {export type TestRunnerEventMap = {[TestRunnerEvent.TestFilesChanged]: [testFiles: string[]];- [TestRunnerEvent.TestPaused]: [params: { errors: reporterTypes.TestError[], extraData: any }];+ [TestRunnerEvent.TestPaused]: [params: TestPausedParams];};export type ListTestsParams = {diff --git a/packages/playwright/src/runner/testServer.ts b/packages/playwright/src/runner/testServer.tsindex b613b06568..c1fd0693ab 100644--- a/packages/playwright/src/runner/testServer.ts+++ b/packages/playwright/src/runner/testServer.ts@@ -108,7 +108,7 @@ export class TestServerDispatcher implements TestServerInterface {this._dispatchEvent = (method, params) => this.transport.sendEvent?.(method, params);this._testRunner.on(TestRunnerEvent.TestFilesChanged, testFiles => this._dispatchEvent('testFilesChanged', { testFiles }));- this._testRunner.on(TestRunnerEvent.TestPaused, params => this._dispatchEvent('testPaused', params));+ this._testRunner.on(TestRunnerEvent.TestPaused, params => this._dispatchEvent('testPaused', { errors: params.errors }));}private async _wireReporter(messageSink: (message: any) => void) {diff --git a/packages/playwright/src/runner/workerHost.ts b/packages/playwright/src/runner/workerHost.tsindex 979f56f3a9..19ebe0c2be 100644--- a/packages/playwright/src/runner/workerHost.ts+++ b/packages/playwright/src/runner/workerHost.ts@@ -24,7 +24,7 @@ import { stdioChunkToParams } from '../common/ipc';import { artifactsFolderName } from '../isomorphic/folders';import type { TestGroup } from './testGroups';-import type { RunPayload, SerializedConfig, WorkerInitParams } from '../common/ipc';+import type { CustomMessageRequestPayload, CustomMessageResponsePayload, RunPayload, SerializedConfig, WorkerInitParams } from '../common/ipc';let lastWorkerIndex = 0;@@ -90,6 +90,10 @@ export class WorkerHost extends ProcessHost {this.sendMessageNoReply({ method: 'runTestGroup', params: runPayload });}+ async sendCustomMessage(payload: CustomMessageRequestPayload) {+ return await this.sendMessage({ method: 'customMessage', params: payload }) as CustomMessageResponsePayload;+ }+hash() {return this._hash;}diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.tsindex d9f979510f..a3eb8f800f 100644--- a/packages/playwright/src/worker/testInfo.ts+++ b/packages/playwright/src/worker/testInfo.ts@@ -85,7 +85,7 @@ export class TestInfoImpl implements TestInfo {private readonly _steps: TestStepInternal[] = [];private readonly _stepMap = new Map<string, TestStepInternal>();_onDidFinishTestFunctionCallback?: () => Promise<void>;- _onDidPauseTestCallback?: () => Promise<{ extraData: any, dispose: () => Promise<void> }>;+ _onCustomMessageCallback?: (data: any) => Promise<any>;_hasNonRetriableError = false;_hasUnhandledError = false;_allowSkips = false;@@ -463,10 +463,8 @@ export class TestInfoImpl implements TestInfo {async _didFinishTestFunction() {const shouldPause = (this._workerParams.pauseAtEnd && !this._isFailure()) || (this._workerParams.pauseOnError && this._isFailure());if (shouldPause) {- const customHandler = await this._onDidPauseTestCallback?.();- this._onTestPaused({ errors: this._isFailure() ? this.errors : [], extraData: customHandler?.extraData });+ this._onTestPaused({ testId: this.testId, errors: this._isFailure() ? this.errors : [] });await this._interruptedPromise;- await customHandler?.dispose();}await this._onDidFinishTestFunctionCallback?.();}diff --git a/packages/playwright/src/worker/workerMain.ts b/packages/playwright/src/worker/workerMain.tsindex cb5a33cdc3..f1a96cbdd8 100644--- a/packages/playwright/src/worker/workerMain.ts+++ b/packages/playwright/src/worker/workerMain.ts@@ -33,7 +33,7 @@ import { loadTestFile } from '../common/testLoader';import type { TimeSlot } from './timeoutManager';import type { Location } from '../../types/testReporter';import type { FullConfigInternal, FullProjectInternal } from '../common/config';-import type { DonePayload, RunPayload, TeardownErrorsPayload, TestBeginPayload, TestEndPayload, TestInfoErrorImpl, WorkerInitParams } from '../common/ipc';+import type { CustomMessageRequestPayload, CustomMessageResponsePayload, DonePayload, RunPayload, TeardownErrorsPayload, TestBeginPayload, TestEndPayload, TestInfoErrorImpl, WorkerInitParams } from '../common/ipc';import type { Suite, TestCase } from '../common/test';import type { TestAnnotation } from '../../types/test';@@ -267,6 +267,17 @@ export class WorkerMain extends ProcessRunner {}}+ async customMessage(payload: CustomMessageRequestPayload): Promise<CustomMessageResponsePayload> {+ try {+ if (this._currentTest?.testId !== payload.testId)+ throw new Error('Test has already stopped');+ const response = await this._currentTest._onCustomMessageCallback?.(payload.request);+ return { response };+ } catch (error) {+ return { response: {}, error: testInfoError(error) };+ }+ }+private async _runTest(test: TestCase, retry: number, nextTest: TestCase | undefined) {const testInfo = new TestInfoImpl(this._config, this._project, this._params, test, retry,stepBeginPayload => this.dispatchEvent('stepBegin', stepBeginPayload),diff --git a/tests/mcp/mdb.spec.ts b/tests/mcp/mdb.spec.tsdeleted file mode 100644index f8a408a8b9..0000000000--- a/tests/mcp/mdb.spec.ts+++ /dev/null@@ -1,361 +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 { z } from 'zod';-import zodToJsonSchema from 'zod-to-json-schema';-import { Client } from '@modelcontextprotocol/sdk/client/index.js';-import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';--import * as mcp from '../../packages/playwright/lib/mcp/sdk/exports';-import * as mcpBundle from '../../packages/playwright/lib/mcp/sdk/bundle';-import { test, expect } from './fixtures';--import type http from 'http';--test('call top level tool', async () => {- const { mdbUrl } = await startMDBAndCLI();- const mdbClient = await createMDBClient(mdbUrl);-- const { tools } = await mdbClient.client.listTools();- expect(tools).toEqual([{- name: 'cli_echo',- description: 'Echo a message',- inputSchema: expect.any(Object),- }, {- name: 'cli_pause_in_gdb',- description: 'Pause in gdb',- inputSchema: expect.any(Object),- }, {- name: 'cli_restart',- description: 'Restart the process',- inputSchema: expect.any(Object),- }, {- name: 'gdb_bt',- description: 'Print backtrace',- inputSchema: expect.any(Object),- }, {- name: 'gdb_echo',- description: 'Echo a message',- inputSchema: expect.any(Object),- }]);-- const echoResult = await mdbClient.client.callTool({- name: 'cli_echo',- arguments: {- message: 'Hello, world!',- },- });- expect(echoResult.content).toEqual([{ type: 'text', text: 'Echo: Hello, world!, roots: ' }]);-- await mdbClient.close();-});--test('pause on error', async () => {- const { mdbUrl } = await startMDBAndCLI();- const mdbClient = await createMDBClient(mdbUrl);-- // Make a call that results in a recoverable error.- const interruptResult = await mdbClient.client.callTool({- name: 'cli_pause_in_gdb',- arguments: {},- });- expect(interruptResult.content).toEqual([{ type: 'text', text: 'Paused on exception' }]);-- // Call the new inner tool.- const btResult = await mdbClient.client.callTool({- name: 'gdb_bt',- arguments: {},- });- expect(btResult.content).toEqual([{ type: 'text', text: 'Backtrace' }]);-- await mdbClient.close();-});--test('outer and inner roots available', async () => {- const { mdbUrl } = await startMDBAndCLI();- const mdbClient = await createMDBClient(mdbUrl, [{ name: 'test', uri: 'file://tmp/' }]);-- expect(await mdbClient.client.callTool({- name: 'cli_echo',- arguments: {- message: 'Hello, cli!',- },- })).toEqual({- content: [{- type: 'text',- text: 'Echo: Hello, cli!, roots: test=file://tmp/',- }]- });-- await mdbClient.client.callTool({- name: 'cli_pause_in_gdb',- arguments: {},- });-- expect(await mdbClient.client.callTool({- name: 'gdb_echo',- arguments: {- message: 'Hello, bt!',- },- })).toEqual({- content: [{- type: 'text',- text: 'Echo: Hello, bt!, roots: test=file://tmp/',- }]- });-- await mdbClient.close();-});--test('should reset', async () => {- const { mdbUrl, log } = await startMDBAndCLI();- const mdbClient = await createMDBClient(mdbUrl);-- // Make a call that results in a recoverable error.- const interruptResult = await mdbClient.client.callTool({- name: 'cli_pause_in_gdb',- arguments: {},- });- expect(interruptResult.content).toEqual([{ type: 'text', text: 'Paused on exception' }]);-- // Call the new inner tool.- const btResult = await mdbClient.client.callTool({- name: 'gdb_bt',- arguments: {},- });- expect(btResult.content).toEqual([{ type: 'text', text: 'Backtrace' }]);-- await mdbClient.client.callTool({- name: 'cli_echo',- arguments: {},- });-- await expect.poll(() => log).toEqual([- 'CLI: initialize',- 'CLI: callTool cli_pause_in_gdb',- 'GDB: listTools',- 'GDB: initialize',- 'GDB: callTool gdb_bt',- 'CLI: afterCallTool gdb_bt',- 'GDB: serverClosed',- 'CLI: callTool cli_echo',- ]);-- const restartResult = await mdbClient.client.callTool({- name: 'cli_restart',- arguments: {},- });- expect(restartResult.content).toEqual([{ type: 'text', text: 'Restarted' }]);-- const pauseResult = await mdbClient.client.callTool({- name: 'cli_pause_in_gdb',- arguments: {},- });- expect(pauseResult.content).toEqual([{ type: 'text', text: 'Paused on exception' }]);-- const btResult2 = await mdbClient.client.callTool({- name: 'gdb_bt',- arguments: {},- });- expect(btResult2.content).toEqual([{ type: 'text', text: 'Backtrace' }]);-- await expect.poll(() => log).toEqual([- 'CLI: initialize',- 'CLI: callTool cli_pause_in_gdb',- 'GDB: listTools',- 'GDB: initialize',- 'GDB: callTool gdb_bt',- 'CLI: afterCallTool gdb_bt',- 'GDB: serverClosed',- 'CLI: callTool cli_echo',- 'CLI: callTool cli_restart',- 'CLI: callTool cli_pause_in_gdb',- 'GDB: listTools',- 'GDB: initialize',- 'GDB: callTool gdb_bt',- 'CLI: afterCallTool gdb_bt',- ]);-- await mdbClient.close();-});--test('mdb has unguessable url', async () => {- let firstPathname: string | undefined;- let secondPathname: string | undefined;- {- const { mdbUrl } = await startMDBAndCLI();- firstPathname = new URL(mdbUrl).pathname;- const mdbClient = await createMDBClient(mdbUrl);- await mdbClient.close();- }- {- const { mdbUrl } = await startMDBAndCLI();- secondPathname = new URL(mdbUrl).pathname;- const mdbClient = await createMDBClient(mdbUrl);- await mdbClient.close();- }- expect(firstPathname.length).toBe(37);- expect(secondPathname.length).toBe(37);- expect(firstPathname).not.toBe(secondPathname);-});--async function startMDBAndCLI(): Promise<{ mdbUrl: string, log: string[] }> {- const mdbUrlBox = { mdbUrl: undefined as string | undefined };- const log: string[] = [];- const cliBackendFactory = {- name: 'CLI',- nameInConfig: 'cli',- version: '0.0.0',- create: pushClient => new CLIBackend(log, pushClient)- };-- const mdbUrl = (await mcp.runMainBackend(cliBackendFactory, { port: 0 }))!;- mdbUrlBox.mdbUrl = mdbUrl;- return { mdbUrl, log };-}--async function createMDBClient(mdbUrl: string, roots: any[] | undefined = undefined): Promise<{ client: Client, close: () => Promise<void> }> {- const client = new Client({ name: 'Test client', version: '0.0.0' }, roots ? { capabilities: { roots: {} } } : undefined);- if (roots)- client.setRequestHandler(mcpBundle.ListRootsRequestSchema, () => ({ roots }));- const transport = new StreamableHTTPClientTransport(new URL(mdbUrl));- await client.connect(transport);- return {- client,- close: async () => {- await transport.terminateSession();- await client.close();- }- };-}--class CLIBackend {- private _roots: any[] | undefined;- private _log: string[] = [];- private _pushClient: (url: string, message: string) => Promise<void>;- private _gdbServer: http.Server | undefined;-- constructor(log: string[], pushClient: (url: string, message: string) => Promise<void>) {- this._log = log;- this._pushClient = pushClient;- }-- async initialize(server, clientInfo) {- this._log.push('CLI: initialize');- this._roots = clientInfo.roots;- }-- async listTools() {- this._log.push('CLI: listTools');- return [{- name: 'cli_echo',- description: 'Echo a message',- inputSchema: zodToJsonSchema(z.object({ message: z.string() })) as any,- }, {- name: 'cli_pause_in_gdb',- description: 'Pause in gdb',- inputSchema: zodToJsonSchema(z.object({})) as any,- }, {- name: 'cli_restart',- description: 'Restart the process',- inputSchema: zodToJsonSchema(z.object({})) as any,- }, {- name: 'gdb_bt',- description: 'Print backtrace',- inputSchema: zodToJsonSchema(z.object({})) as any,- }, {- name: 'gdb_echo',- description: 'Echo a message',- inputSchema: zodToJsonSchema(z.object({ message: z.string() })) as any,- }];- }-- async afterCallTool(name: string, args: any) {- this._log.push(`CLI: afterCallTool ${name}`);- }-- async callTool(name: string, args: any) {- this._log.push(`CLI: callTool ${name}`);- if (name === 'cli_echo')- return { content: [{ type: 'text', text: `Echo: ${args?.message as string}, roots: ${stringifyRoots(this._roots)}` }] };- if (name === 'cli_pause_in_gdb') {- const factory = {- name: 'gdb',- nameInConfig: 'gdb',- version: '0.0.0',- create: () => new GDBBackend(this._log),- };- this._gdbServer = await mcp.startHttpServer({ port: 0 });- const mcpUrl = await mcp.installHttpTransport(this._gdbServer, factory, true);- await this._pushClient(mcpUrl, 'Paused on exception');- return { content: [{ type: 'text', text: 'Done' }] };- }- if (name === 'cli_restart') {- this._gdbServer.close();- this._gdbServer = undefined;- return { content: [{ type: 'text', text: 'Restarted' }] };- }- throw new Error(`Unknown tool: ${name}`);- }-- serverClosed() {- this._log.push('CLI: serverClosed');- }-}--class GDBBackend {- private _roots: any[] | undefined;- private _log: string[] = [];-- constructor(log: string[]) {- this._log = log;- }-- async initialize(server, clientVersion) {- this._log.push('GDB: initialize');- this._roots = clientVersion.roots;- }-- async listTools() {- this._log.push('GDB: listTools');- return [{- name: 'gdb_bt',- description: 'Print backtrace',- inputSchema: zodToJsonSchema(z.object({})) as any,- }, {- name: 'gdb_echo',- description: 'Echo a message',- inputSchema: zodToJsonSchema(z.object({ message: z.string() })) as any,- }];- }-- async callTool(name: string, args: any) {- this._log.push(`GDB: callTool ${name}`);- if (name === 'gdb_echo')- return { content: [{ type: 'text', text: `Echo: ${args?.message as string}, roots: ${stringifyRoots(this._roots)}` }] };- if (name === 'gdb_bt')- return { content: [{ type: 'text', text: 'Backtrace' }] };- throw new Error(`Unknown tool: ${name}`);- }-- serverClosed() {- this._log.push('GDB: serverClosed');- }-}--function stringifyRoots(roots: any[]) {- return roots.map(root => `${root.name}=${root.uri}`).join(',');-}