Skip to content

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

chore(testRunner): expose pauseAtEnd/pauseOnError (#37923)

Dmitry Gozmana6fb46768330
diff --git a/packages/playwright/src/common/ipc.ts b/packages/playwright/src/common/ipc.ts
index 240acf185f..0ddcf9ff19 100644
--- a/packages/playwright/src/common/ipc.ts
+++ b/packages/playwright/src/common/ipc.ts
@@ -86,6 +86,11 @@ export type AttachmentPayload = {
export type TestInfoErrorImpl = TestInfoError;
+export type TestPausedPayload = {
+ errors: TestInfoErrorImpl[];
+ extraData: any;
+};
+
export type TestEndPayload = {
testId: string;
duration: number;
diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts
index 70db507412..82144035ac 100644
--- a/packages/playwright/src/index.ts
+++ b/packages/playwright/src/index.ts
@@ -22,7 +22,7 @@ import { setBoxedStackPrefixes, createGuid, currentZone, debugMode, jsonStringif
import { currentTestInfo } from './common/globals';
import { rootTestType } from './common/testType';
-import { runBrowserBackendAtEnd } from './mcp/test/browserBackend';
+import { runBrowserBackendOnTestPause } from './mcp/test/browserBackend';
import type { Fixtures, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, ScreenshotMode, TestInfo, TestType, VideoMode } from '../types/test';
import type { ContextReuseMode } from './common/config';
@@ -237,7 +237,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
(testInfo as TestInfoImpl)._setDebugMode();
playwright._defaultContextOptions = _combinedContextOptions;
- playwright._defaultContextTimeout = (testInfo as TestInfoImpl)._pauseOnError() ? 5000 : actionTimeout || 0;
+ playwright._defaultContextTimeout = actionTimeout || 0;
playwright._defaultContextNavigationTimeout = navigationTimeout || 0;
await use();
playwright._defaultContextOptions = undefined;
@@ -417,14 +417,14 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
attachConnectedHeaderIfNeeded(testInfo, browserImpl);
if (!_reuseContext) {
const { context, close } = await _contextFactory();
- (testInfo as TestInfoImpl)._onDidFinishTestFunctions.unshift(() => runBrowserBackendAtEnd(context, testInfo.errors[0]?.message));
+ (testInfo as TestInfoImpl)._onDidPauseTestCallback = () => runBrowserBackendOnTestPause(testInfo, context);
await use(context);
await close();
return;
}
const context = await browserImpl._wrapApiCall(() => browserImpl._newContextForReuse(), { internal: true });
- (testInfo as TestInfoImpl)._onDidFinishTestFunctions.unshift(() => runBrowserBackendAtEnd(context, testInfo.errors[0]?.message));
+ (testInfo as TestInfoImpl)._onDidPauseTestCallback = () => runBrowserBackendOnTestPause(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 });
@@ -647,7 +647,7 @@ class ArtifactsRecorder {
async willStartTest(testInfo: TestInfoImpl) {
this._testInfo = testInfo;
- testInfo._onDidFinishTestFunctions.push(() => this.didFinishTestFunction());
+ testInfo._onDidFinishTestFunctionCallback = () => this.didFinishTestFunction();
this._screenshotRecorder.fixOrdinal();
diff --git a/packages/playwright/src/isomorphic/testServerConnection.ts b/packages/playwright/src/isomorphic/testServerConnection.ts
index 57a416a604..31928563ee 100644
--- a/packages/playwright/src/isomorphic/testServerConnection.ts
+++ b/packages/playwright/src/isomorphic/testServerConnection.ts
@@ -17,6 +17,7 @@
import * as events from './events';
import type { TestServerInterface, TestServerInterfaceEvents } from '@testIsomorphic/testServerInterface';
+import type * as reporterTypes from '../../types/testReporter';
// -- Reuse boundary -- Everything below this line is reused in the vscode extension.
@@ -68,12 +69,14 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
readonly onStdio: events.Event<{ type: 'stderr' | 'stdout'; text?: string | undefined; buffer?: string | undefined; }>;
readonly onTestFilesChanged: events.Event<{ testFiles: string[] }>;
readonly onLoadTraceRequested: events.Event<{ traceUrl: string }>;
+ readonly onTestPaused: events.Event<{ errors: reporterTypes.TestError[] }>;
private _onCloseEmitter = new events.EventEmitter<void>();
private _onReportEmitter = new events.EventEmitter<any>();
private _onStdioEmitter = new events.EventEmitter<{ type: 'stderr' | 'stdout'; text?: string | undefined; buffer?: string | undefined; }>();
private _onTestFilesChangedEmitter = new events.EventEmitter<{ testFiles: string[] }>();
private _onLoadTraceRequestedEmitter = new events.EventEmitter<{ traceUrl: string }>();
+ private _onTestPausedEmitter = new events.EventEmitter<{ errors: reporterTypes.TestError[] }>();
private _lastId = 0;
private _transport: TestServerTransport;
@@ -87,6 +90,7 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this.onStdio = this._onStdioEmitter.event;
this.onTestFilesChanged = this._onTestFilesChangedEmitter.event;
this.onLoadTraceRequested = this._onLoadTraceRequestedEmitter.event;
+ this.onTestPaused = this._onTestPausedEmitter.event;
this._transport = transport;
this._transport.onmessage(data => {
@@ -147,6 +151,8 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
this._onTestFilesChangedEmitter.fire(params);
else if (method === 'loadTraceRequested')
this._onLoadTraceRequestedEmitter.fire(params);
+ else if (method === 'testPaused')
+ this._onTestPausedEmitter.fire(params);
}
async initialize(params: Parameters<TestServerInterface['initialize']>[0]): ReturnType<TestServerInterface['initialize']> {
diff --git a/packages/playwright/src/isomorphic/testServerInterface.ts b/packages/playwright/src/isomorphic/testServerInterface.ts
index 0761f6cdfb..666ea6c5ba 100644
--- a/packages/playwright/src/isomorphic/testServerInterface.ts
+++ b/packages/playwright/src/isomorphic/testServerInterface.ts
@@ -103,6 +103,8 @@ export interface TestServerInterface {
reuseContext?: boolean;
connectWsEndpoint?: string;
timeout?: number;
+ pauseOnError?: boolean;
+ pauseAtEnd?: boolean;
}): Promise<{
status: reporterTypes.FullResult['status'];
}>;
@@ -121,6 +123,7 @@ export interface TestServerInterfaceEvents {
onStdio: Event<{ type: 'stdout' | 'stderr', text?: string, buffer?: string }>;
onTestFilesChanged: Event<{ testFiles: string[] }>;
onLoadTraceRequested: Event<{ traceUrl: string }>;
+ onTestPaused: Event<{ errors: reporterTypes.TestError[] }>;
}
export interface TestServerInterfaceEventEmitters {
@@ -128,4 +131,5 @@ export interface TestServerInterfaceEventEmitters {
dispatchEvent(event: 'stdio', params: { type: 'stdout' | 'stderr', text?: string, buffer?: string }): void;
dispatchEvent(event: 'testFilesChanged', params: { testFiles: string[] }): void;
dispatchEvent(event: 'loadTraceRequested', params: { traceUrl: string }): void;
+ dispatchEvent(event: 'testPaused', params: { errors: reporterTypes.TestError[] }): void;
}
diff --git a/packages/playwright/src/mcp/sdk/mdb.ts b/packages/playwright/src/mcp/sdk/mdb.ts
index d8840cfd68..06f4263ff0 100644
--- a/packages/playwright/src/mcp/sdk/mdb.ts
+++ b/packages/playwright/src/mcp/sdk/mdb.ts
@@ -17,7 +17,6 @@
import { debug } from 'playwright-core/lib/utilsBundle';
import { ManualPromise } from 'playwright-core/lib/utils';
-import { defineToolSchema } from './tool';
import * as mcpBundle from './bundle';
import * as mcpServer from './server';
import * as mcpHttp from './http';
@@ -27,7 +26,11 @@ import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
const mdbDebug = debug('pw:mcp:mdb');
const errorsDebug = debug('pw:mcp:errors');
-const z = mcpBundle.z;
+
+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;
@@ -37,8 +40,8 @@ export class MDBBackend implements mcpServer.ServerBackend {
private _progress: mcpServer.CallToolResult['content'] = [];
private _progressCallback: mcpServer.ProgressCallback;
- constructor(mainBackend: mcpServer.ServerBackend) {
- this._mainBackend = mainBackend;
+ 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 });
@@ -57,11 +60,6 @@ export class MDBBackend implements mcpServer.ServerBackend {
}
async callTool(name: string, args: mcpServer.CallToolRequest['params']['arguments']): Promise<mcpServer.CallToolResult> {
- if (name === pushToolsSchema.name) {
- await this._createOnPauseClient(pushToolsSchema.inputSchema.parse(args || {}));
- return { content: [{ type: 'text', text: 'Tools pushed' }] };
- }
-
if (this._onPauseClient?.tools.find(tool => tool.name === name)) {
const result = await this._onPauseClient.client.callTool({
name,
@@ -95,16 +93,16 @@ export class MDBBackend implements mcpServer.ServerBackend {
return result;
}
- private async _createOnPauseClient(params: { mcpUrl: string, introMessage?: string }) {
+ private async _createOnPauseClient(mcpUrl: string, introMessage?: string) {
if (this._onPauseClient)
await this._onPauseClient.client.close().catch(errorsDebug);
- this._onPauseClient = await this._createClient(params.mcpUrl);
+ this._onPauseClient = await this._createClient(mcpUrl);
this._interruptPromise?.resolve({
content: [{
type: 'text',
- text: params.introMessage || '',
+ text: introMessage || '',
}],
});
this._interruptPromise = undefined;
@@ -128,100 +126,18 @@ export class MDBBackend implements mcpServer.ServerBackend {
}
}
-const pushToolsSchema = defineToolSchema({
- name: 'mdb_push_tools',
- title: 'Push MCP tools to the tools stack',
- description: 'Push MCP tools to the tools stack',
- inputSchema: z.object({
- mcpUrl: z.string(),
- introMessage: z.string().optional(),
- }),
- type: 'readOnly',
-});
-
-export async function runMainBackend(backendFactory: mcpServer.ServerBackendFactory, options?: { port?: number }): Promise<string | undefined> {
- const mdbBackend = new MDBBackend(backendFactory.create());
- // Start HTTP unconditionally.
+// 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
};
- const url = await startAsHttp(factory, { port: options?.port || 0 });
- process.env.PLAYWRIGHT_DEBUGGER_MCP = url;
-
- if (options?.port !== undefined)
- return url;
-
- // Start stdio conditionally.
- await mcpServer.connect(factory, new mcpBundle.StdioServerTransport(), false);
-}
-
-export async function runOnPauseBackendLoop(backend: mcpServer.ServerBackend, introMessage: string) {
- const wrappedBackend = new ServerBackendWithCloseListener(backend);
-
- const factory = {
- name: 'on-pause-backend',
- nameInConfig: 'on-pause-backend',
- version: '0.0.0',
- create: () => wrappedBackend,
- };
-
- const httpServer = await mcpHttp.startHttpServer({ port: 0 });
- const url = await mcpHttp.installHttpTransport(httpServer, factory, true);
-
- const client = new mcpBundle.Client({ name: 'Pushing client', version: '0.0.0' });
- client.setRequestHandler(mcpBundle.PingRequestSchema, () => ({}));
- const transport = new mcpBundle.StreamableHTTPClientTransport(new URL(process.env.PLAYWRIGHT_DEBUGGER_MCP!));
- await client.connect(transport);
-
- const pushToolsResult = await client.callTool({
- name: pushToolsSchema.name,
- arguments: {
- mcpUrl: url,
- introMessage,
- },
- });
- if (pushToolsResult.isError)
- errorsDebug('Failed to push tools', pushToolsResult.content);
- await transport.terminateSession();
- await client.close();
-
- await wrappedBackend.waitForClosed();
- httpServer.close();
-}
-
-async function startAsHttp(backendFactory: mcpServer.ServerBackendFactory, options: { port: number }) {
- const httpServer = await mcpHttp.startHttpServer(options);
- return await mcpHttp.installHttpTransport(httpServer, backendFactory, true);
-}
-
-class ServerBackendWithCloseListener implements mcpServer.ServerBackend {
- private _backend: mcpServer.ServerBackend;
- private _serverClosedPromise = new ManualPromise<void>();
-
- constructor(backend: mcpServer.ServerBackend) {
- this._backend = backend;
- }
-
- async initialize(server: mcpServer.Server, clientInfo: mcpServer.ClientInfo): Promise<void> {
- await this._backend.initialize?.(server, clientInfo);
- }
-
- async listTools(): Promise<mcpServer.Tool[]> {
- return this._backend.listTools();
- }
-
- async callTool(name: string, args: mcpServer.CallToolRequest['params']['arguments'], progress: mcpServer.ProgressCallback): Promise<mcpServer.CallToolResult> {
- return this._backend.callTool(name, args, progress);
+ if (options?.port !== undefined) {
+ const httpServer = await mcpHttp.startHttpServer(options);
+ return await mcpHttp.installHttpTransport(httpServer, factory, true);
}
- serverClosed(server: mcpServer.Server) {
- this._backend.serverClosed?.(server);
- this._serverClosedPromise.resolve();
- }
-
- async waitForClosed() {
- await this._serverClosedPromise;
- }
+ await mcpServer.connect(factory, new mcpBundle.StdioServerTransport(), false);
}
diff --git a/packages/playwright/src/mcp/test/browserBackend.ts b/packages/playwright/src/mcp/test/browserBackend.ts
index 471a5b1cd8..f60a9844ac 100644
--- a/packages/playwright/src/mcp/test/browserBackend.ts
+++ b/packages/playwright/src/mcp/test/browserBackend.ts
@@ -15,8 +15,6 @@
*/
import * as mcp from '../sdk/exports';
-import { currentTestInfo } from '../../common/globals';
-import { stripAnsiEscapes } from '../../util';
import { defaultConfig, FullConfig } from '../browser/config';
import { BrowserServerBackend } from '../browser/browserServerBackend';
import { Tab } from '../browser/tab';
@@ -24,22 +22,15 @@ import { Tab } from '../browser/tab';
import type * as playwright from '../../../index';
import type { Page } from '../../../../playwright-core/src/client/page';
import type { BrowserContextFactory } from '../browser/browserContextFactory';
-import type { ClientInfo } from '../sdk/server';
+import type { TestInfo } from '../../../test';
-export async function runBrowserBackendAtEnd(context: playwright.BrowserContext, errorMessage?: string) {
- const testInfo = currentTestInfo();
- if (!testInfo)
- return;
-
- const shouldPause = errorMessage ? testInfo?._pauseOnError() : testInfo?._pauseAtEnd();
- if (!shouldPause)
- return;
+export type TestPausedExtraData = {
+ mcpUrl: string;
+ contextState: string;
+};
+export async function runBrowserBackendOnTestPause(testInfo: TestInfo, context: playwright.BrowserContext) {
const lines: string[] = [];
- if (errorMessage)
- lines.push(`### Paused on error:`, stripAnsiEscapes(errorMessage));
- 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];
@@ -51,7 +42,7 @@ export async function runBrowserBackendAtEnd(context: playwright.BrowserContext,
`- Page Title: ${await page.title()}`.trim()
);
// Only print console errors when pausing on error, not when everything works as expected.
- let console = errorMessage ? await Tab.collectConsoleMessages(page) : [];
+ let console = testInfo.errors.length ? await Tab.collectConsoleMessages(page) : [];
console = console.filter(msg => !msg.type || msg.type === 'error');
if (console.length) {
lines.push('- Console Messages:');
@@ -66,21 +57,29 @@ export async function runBrowserBackendAtEnd(context: playwright.BrowserContext,
);
}
- lines.push('');
- if (errorMessage)
- lines.push(`### Task`, `Try recovering from the error prior to continuing`);
-
const config: FullConfig = {
...defaultConfig,
capabilities: ['testing'],
};
- await mcp.runOnPauseBackendLoop(new BrowserServerBackend(config, identityFactory(context)), lines.join('\n'));
+ 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 };
}
function identityFactory(browserContext: playwright.BrowserContext): BrowserContextFactory {
return {
- createContext: async (clientInfo: ClientInfo, abortSignal: AbortSignal, toolName: string | undefined) => {
+ createContext: async (clientInfo: mcp.ClientInfo, abortSignal: AbortSignal, toolName: string | undefined) => {
return {
browserContext,
close: async () => {}
diff --git a/packages/playwright/src/mcp/test/testBackend.ts b/packages/playwright/src/mcp/test/testBackend.ts
index 8ac54f001c..8e81f923dc 100644
--- a/packages/playwright/src/mcp/test/testBackend.ts
+++ b/packages/playwright/src/mcp/test/testBackend.ts
@@ -40,8 +40,8 @@ export class TestServerBackend implements mcp.ServerBackend {
private _context: TestContext;
private _configOption: string | undefined;
- constructor(configOption: string | undefined, options?: { muteConsole?: boolean, headless?: boolean }) {
- this._context = new TestContext(options);
+ constructor(configOption: string | undefined, pushClient: mcp.MDBPushClientCallback, options?: { muteConsole?: boolean, headless?: boolean }) {
+ this._context = new TestContext(pushClient, options);
this._configOption = configOption;
}
diff --git a/packages/playwright/src/mcp/test/testContext.ts b/packages/playwright/src/mcp/test/testContext.ts
index d71a41ff48..e27b1fa505 100644
--- a/packages/playwright/src/mcp/test/testContext.ts
+++ b/packages/playwright/src/mcp/test/testContext.ts
@@ -22,12 +22,13 @@ import { noColors, escapeRegExp } from 'playwright-core/lib/utils';
import { terminalScreen } from '../../reporters/base';
import ListReporter from '../../reporters/list';
import { StringWriteStream } from './streams';
-import { fileExistsAsync } from '../../util';
+import { stripAnsiEscapes, fileExistsAsync } from '../../util';
import { TestRunner, TestRunnerEvent } from '../../runner/testRunner';
import { ensureSeedFile, seedProject } from './seed';
-import type { ProgressCallback } from '../sdk/server';
import type { ConfigLocation } from '../../common/config';
+import type { ProgressCallback, MDBPushClientCallback } from '../sdk/exports';
+import type { TestPausedExtraData } from './browserBackend';
export type SeedFile = {
file: string;
@@ -71,13 +72,15 @@ ${step.code}
}
export class TestContext {
+ private _pushClient: MDBPushClientCallback;
private _testRunner: TestRunner | undefined;
readonly options?: { muteConsole?: boolean, headless?: boolean };
configLocation!: ConfigLocation;
rootPath!: string;
generatorJournal: GeneratorJournal | undefined;
- constructor(options?: { muteConsole?: boolean, headless?: boolean }) {
+ constructor(pushClient: MDBPushClientCallback, options?: { muteConsole?: boolean, headless?: boolean }) {
+ this._pushClient = pushClient;
this.options = options;
}
@@ -99,6 +102,22 @@ export class TestContext {
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'));
+ });
this._testRunner = testRunner;
return testRunner;
}
diff --git a/packages/playwright/src/mcp/test/testTools.ts b/packages/playwright/src/mcp/test/testTools.ts
index 7a4f0bda8d..096d6bca90 100644
--- a/packages/playwright/src/mcp/test/testTools.ts
+++ b/packages/playwright/src/mcp/test/testTools.ts
@@ -87,6 +87,7 @@ export const debugTest = defineTestTool({
workers: 1,
pauseOnError: true,
disableConfigReporters: true,
+ actionTimeout: 5000,
});
}, progress);
diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.ts
index 22260277c7..5e58a10260 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 { ServerBackendFactory, runMainBackend } from './mcp/sdk/exports';
+import { MDBServerBackendFactory, runMainBackend } from './mcp/sdk/exports';
import { TestServerBackend } from './mcp/test/testBackend';
import { decorateCommand } from './mcp/program';
import { setupExitWatchdog } from './mcp/browser/watchdog';
@@ -162,11 +162,11 @@ function addTestMCPServerCommand(program: Command) {
command.option('--port <port>', 'port to listen on for SSE transport.');
command.action(async options => {
setupExitWatchdog();
- const backendFactory: ServerBackendFactory = {
+ const backendFactory: MDBServerBackendFactory = {
name: 'Playwright Test Runner',
nameInConfig: 'playwright-test-runner',
version: packageJSON.version,
- create: () => new TestServerBackend(options.config, { muteConsole: options.port === undefined, headless: options.headless }),
+ create: pushClient => new TestServerBackend(options.config, pushClient, { muteConsole: options.port === undefined, headless: options.headless }),
};
const mdbUrl = await runMainBackend(
backendFactory,
diff --git a/packages/playwright/src/reporters/internalReporter.ts b/packages/playwright/src/reporters/internalReporter.ts
index 2efb36d9ba..946c16932b 100644
--- a/packages/playwright/src/reporters/internalReporter.ts
+++ b/packages/playwright/src/reporters/internalReporter.ts
@@ -117,7 +117,7 @@ export class InternalReporter implements ReporterV2 {
}
}
-function addLocationAndSnippetToError(config: FullConfig, error: TestError, file?: string) {
+export function addLocationAndSnippetToError(config: FullConfig, error: TestError, file?: string) {
if (error.stack && !error.location)
error.location = prepareErrorStack(error.stack).location;
const location = error.location;
diff --git a/packages/playwright/src/runner/dispatcher.ts b/packages/playwright/src/runner/dispatcher.ts
index d9db512cd9..89d15427be 100644
--- a/packages/playwright/src/runner/dispatcher.ts
+++ b/packages/playwright/src/runner/dispatcher.ts
@@ -20,13 +20,14 @@ import { colors } from 'playwright-core/lib/utils';
import { addSuggestedRebaseline } from './rebase';
import { WorkerHost } from './workerHost';
import { serializeConfig } from '../common/ipc';
+import { addLocationAndSnippetToError } from '../reporters/internalReporter';
import type { FailureTracker } from './failureTracker';
import type { ProcessExitData } from './processHost';
import type { TestGroup } from './testGroups';
import type { TestError, TestResult, TestStep } from '../../types/testReporter';
import type { FullConfigInternal } from '../common/config';
-import type { AttachmentPayload, DonePayload, RunPayload, SerializedConfig, StepBeginPayload, StepEndPayload, TeardownErrorsPayload, TestBeginPayload, TestEndPayload, TestOutputPayload } from '../common/ipc';
+import type { AttachmentPayload, DonePayload, RunPayload, SerializedConfig, StepBeginPayload, StepEndPayload, TeardownErrorsPayload, TestBeginPayload, TestEndPayload, TestOutputPayload, TestPausedPayload } from '../common/ipc';
import type { Suite } from '../common/test';
import type { TestCase } from '../common/test';
import type { ReporterV2 } from '../reporters/reporterV2';
@@ -98,7 +99,7 @@ export class Dispatcher {
// 3. Claim both the job and the worker slot.
this._queue.splice(jobIndex, 1);
- const jobDispatcher = new JobDispatcher(job, this._reporter, this._failureTracker, () => this.stop().catch(() => {}));
+ const jobDispatcher = new JobDispatcher(job, this._config, this._reporter, this._failureTracker, () => this.stop().catch(() => {}));
this._workerSlots[workerIndex].busy = true;
this._workerSlots[workerIndex].jobDispatcher = jobDispatcher;
@@ -278,6 +279,7 @@ class JobDispatcher {
jobResult = new ManualPromise<{ newJob?: TestGroup, didFail: boolean }>();
readonly job: TestGroup;
+ private _config: FullConfigInternal;
private _reporter: ReporterV2;
private _failureTracker: FailureTracker;
private _stopCallback: () => void;
@@ -290,8 +292,9 @@ class JobDispatcher {
private _workerIndex = 0;
private _currentlyRunning: { test: TestCase, result: TestResult } | undefined;
- constructor(job: TestGroup, reporter: ReporterV2, failureTracker: FailureTracker, stopCallback: () => void) {
+ constructor(job: TestGroup, config: FullConfigInternal, reporter: ReporterV2, failureTracker: FailureTracker, stopCallback: () => void) {
this.job = job;
+ this._config = config;
this._reporter = reporter;
this._failureTracker = failureTracker;
this._stopCallback = stopCallback;
@@ -574,6 +577,11 @@ 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, 'done', this._onDone.bind(this)),
eventsHelper.addEventListener(worker, 'exit', this.onExit.bind(this)),
];
diff --git a/packages/playwright/src/runner/failureTracker.ts b/packages/playwright/src/runner/failureTracker.ts
index ef276fa509..e1b518d2b9 100644
--- a/packages/playwright/src/runner/failureTracker.ts
+++ b/packages/playwright/src/runner/failureTracker.ts
@@ -14,21 +14,24 @@
* limitations under the License.
*/
-import type { TestResult } from '../../types/testReporter';
+import type { TestResult, TestError } from '../../types/testReporter';
import type { FullConfigInternal, FullProjectInternal } from '../common/config';
import type { Suite, TestCase } from '../common/test';
export class FailureTracker {
+ private _config: FullConfigInternal;
private _failureCount = 0;
private _hasWorkerErrors = false;
private _rootSuite: Suite | undefined;
private _topLevelProjects: FullProjectInternal[] = [];
private _pauseOnError: boolean;
private _pauseAtEnd: boolean;
+ onTestPaused?: (params: { errors: TestError[], extraData: any }) => void;
- constructor(private _config: FullConfigInternal, options?: { pauseOnError?: boolean, pauseAtEnd?: boolean }) {
- this._pauseOnError = options?.pauseOnError ?? false;
- this._pauseAtEnd = options?.pauseAtEnd ?? false;
+ constructor(config: FullConfigInternal, options?: { pauseOnError?: boolean, pauseAtEnd?: boolean }) {
+ this._config = config;
+ this._pauseOnError = !!options?.pauseOnError;
+ this._pauseAtEnd = !!options?.pauseAtEnd;
}
onRootSuite(rootSuite: Suite, topLevelProjects: FullProjectInternal[]) {
@@ -46,12 +49,12 @@ export class FailureTracker {
this._hasWorkerErrors = true;
}
- pauseOnError(): boolean {
+ pauseOnError() {
return this._pauseOnError;
}
- pauseAtEnd(inProject: FullProjectInternal): boolean {
- return this._pauseAtEnd && this._topLevelProjects.includes(inProject);
+ pauseAtEnd(inProject: FullProjectInternal) {
+ return this._topLevelProjects.includes(inProject) && this._pauseAtEnd;
}
hasReachedMaxFailures() {
diff --git a/packages/playwright/src/runner/testRunner.ts b/packages/playwright/src/runner/testRunner.ts
index 07dfa74ece..3b3c1450e6 100644
--- a/packages/playwright/src/runner/testRunner.ts
+++ b/packages/playwright/src/runner/testRunner.ts
@@ -42,10 +42,12 @@ import type { AnyReporter } from '../reporters/reporterV2';
export const TestRunnerEvent = {
TestFilesChanged: 'testFilesChanged',
+ TestPaused: 'testPaused',
} as const;
export type TestRunnerEventMap = {
[TestRunnerEvent.TestFilesChanged]: [testFiles: string[]];
+ [TestRunnerEvent.TestPaused]: [params: { errors: reporterTypes.TestError[], extraData: any }];
};
export type ListTestsParams = {
@@ -71,6 +73,7 @@ export type RunTestsParams = {
projects?: string[];
reuseContext?: boolean;
connectWsEndpoint?: string;
+ actionTimeout?: number;
pauseOnError?: boolean;
pauseAtEnd?: boolean;
doNotRunDepsOutsideProjectFilter?: boolean;
@@ -314,6 +317,7 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
...(params.headed !== undefined ? { headless: !params.headed } : {}),
_optionContextReuseMode: params.reuseContext ? 'when-possible' : undefined,
_optionConnectOptions: params.connectWsEndpoint ? { wsEndpoint: params.connectWsEndpoint } : undefined,
+ actionTimeout: params.actionTimeout,
},
...(params.updateSnapshots ? { updateSnapshots: params.updateSnapshots } : {}),
...(params.updateSourceMethod ? { updateSourceMethod: params.updateSourceMethod } : {}),
@@ -349,6 +353,7 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
...createRunTestsTasks(config),
];
const testRun = new TestRun(config, reporter, { pauseOnError: params.pauseOnError, pauseAtEnd: params.pauseAtEnd });
+ testRun.failureTracker.onTestPaused = params => this.emit(TestRunnerEvent.TestPaused, params);
const run = runTasks(testRun, tasks, 0, stop).then(async status => {
this._testRun = undefined;
return status;
diff --git a/packages/playwright/src/runner/testServer.ts b/packages/playwright/src/runner/testServer.ts
index 0703f975d9..b613b06568 100644
--- a/packages/playwright/src/runner/testServer.ts
+++ b/packages/playwright/src/runner/testServer.ts
@@ -108,6 +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));
}
private async _wireReporter(messageSink: (message: any) => void) {
@@ -203,6 +204,8 @@ export class TestServerDispatcher implements TestServerInterface {
const { status } = await this._testRunner.runTests(wireReporter, {
...params,
doNotRunDepsOutsideProjectFilter: true,
+ pauseAtEnd: params.pauseAtEnd,
+ pauseOnError: params.pauseOnError,
});
return { status };
}
diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.ts
index 27ad9cc914..d9f979510f 100644
--- a/packages/playwright/src/worker/testInfo.ts
+++ b/packages/playwright/src/worker/testInfo.ts
@@ -17,7 +17,7 @@
import fs from 'fs';
import path from 'path';
-import { captureRawStack, monotonicTime, sanitizeForFilePath, stringifyStackFrames, currentZone, createGuid, escapeWithQuotes } from 'playwright-core/lib/utils';
+import { captureRawStack, monotonicTime, sanitizeForFilePath, stringifyStackFrames, currentZone, createGuid, escapeWithQuotes, ManualPromise } from 'playwright-core/lib/utils';
import { TimeoutManager, TimeoutManagerError, kMaxDeadline } from './timeoutManager';
import { addSuffixToFilePath, filteredStackTrace, getContainedPath, normalizeAndSaveAttachment, sanitizeFilePathBeforeExtension, trimLongString, windowsFilesystemFriendlyLength } from '../util';
@@ -29,7 +29,7 @@ import type { RunnableDescription } from './timeoutManager';
import type { FullProject, TestInfo, TestStatus, TestStepInfo, TestAnnotation } from '../../types/test';
import type { FullConfig, Location } from '../../types/testReporter';
import type { FullConfigInternal, FullProjectInternal } from '../common/config';
-import type { AttachmentPayload, StepBeginPayload, StepEndPayload, TestInfoErrorImpl, WorkerInitParams } from '../common/ipc';
+import type { AttachmentPayload, StepBeginPayload, StepEndPayload, TestInfoErrorImpl, TestPausedPayload, WorkerInitParams } from '../common/ipc';
import type { TestCase } from '../common/test';
import type { StackFrame } from '@protocol/channels';
@@ -68,6 +68,7 @@ export class TestInfoImpl implements TestInfo {
private _onStepBegin: (payload: StepBeginPayload) => void;
private _onStepEnd: (payload: StepEndPayload) => void;
private _onAttach: (payload: AttachmentPayload) => void;
+ private _onTestPaused: (payload: TestPausedPayload) => void;
private _snapshotNames: SnapshotNames = { lastAnonymousSnapshotIndex: 0, lastNamedSnapshotIndex: {} };
private _ariaSnapshotNames: SnapshotNames = { lastAnonymousSnapshotIndex: 0, lastNamedSnapshotIndex: {} };
readonly _timeoutManager: TimeoutManager;
@@ -76,14 +77,15 @@ export class TestInfoImpl implements TestInfo {
readonly _tracing: TestTracing;
readonly _uniqueSymbol;
- _wasInterrupted = false;
+ private _interruptedPromise = new ManualPromise<void>();
_lastStepId = 0;
private readonly _requireFile: string;
readonly _projectInternal: FullProjectInternal;
readonly _configInternal: FullConfigInternal;
private readonly _steps: TestStepInternal[] = [];
private readonly _stepMap = new Map<string, TestStepInternal>();
- _onDidFinishTestFunctions: (() => Promise<void>)[] = [];
+ _onDidFinishTestFunctionCallback?: () => Promise<void>;
+ _onDidPauseTestCallback?: () => Promise<{ extraData: any, dispose: () => Promise<void> }>;
_hasNonRetriableError = false;
_hasUnhandledError = false;
_allowSkips = false;
@@ -161,11 +163,13 @@ export class TestInfoImpl implements TestInfo {
onStepBegin: (payload: StepBeginPayload) => void,
onStepEnd: (payload: StepEndPayload) => void,
onAttach: (payload: AttachmentPayload) => void,
+ onTestPaused: (payload: TestPausedPayload) => void,
) {
this.testId = test?.id ?? '';
this._onStepBegin = onStepBegin;
this._onStepEnd = onStepEnd;
this._onAttach = onAttach;
+ this._onTestPaused = onTestPaused;
this._startTime = monotonicTime();
this._startWallTime = Date.now();
this._requireFile = test?._requireFile ?? '';
@@ -383,7 +387,7 @@ export class TestInfoImpl implements TestInfo {
_interrupt() {
// Mark as interrupted so we can ignore TimeoutError thrown by interrupt() call.
- this._wasInterrupted = true;
+ this._interruptedPromise.resolve();
this._timeoutManager.interrupt();
// Do not overwrite existing failure (for example, unhandled rejection) with "interrupted".
if (this.status === 'passed')
@@ -437,7 +441,7 @@ export class TestInfoImpl implements TestInfo {
} catch (error) {
// When interrupting, we arrive here with a TimeoutManagerError, but we should not
// consider it a timeout.
- if (!this._wasInterrupted && (error instanceof TimeoutManagerError))
+ if (!this._interruptedPromise.isDone() && (error instanceof TimeoutManagerError))
this._failWithError(error);
throw error;
}
@@ -456,6 +460,17 @@ export class TestInfoImpl implements TestInfo {
this._timeoutManager.setIgnoreTimeouts();
}
+ 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 });
+ await this._interruptedPromise;
+ await customHandler?.dispose();
+ }
+ await this._onDidFinishTestFunctionCallback?.();
+ }
+
// ------------ TestInfo methods ------------
async attach(name: string, options: { path?: string, body?: string | Buffer, contentType?: string } = {}) {
@@ -621,14 +636,6 @@ export class TestInfoImpl implements TestInfo {
setTimeout(timeout: number) {
this._timeoutManager.setTimeout(timeout);
}
-
- _pauseOnError(): boolean {
- return this._workerParams.pauseOnError;
- }
-
- _pauseAtEnd(): boolean {
- return this._workerParams.pauseAtEnd;
- }
}
export class TestStepInfoImpl implements TestStepInfo {
diff --git a/packages/playwright/src/worker/workerMain.ts b/packages/playwright/src/worker/workerMain.ts
index 1062852e5b..cb5a33cdc3 100644
--- a/packages/playwright/src/worker/workerMain.ts
+++ b/packages/playwright/src/worker/workerMain.ts
@@ -118,7 +118,7 @@ export class WorkerMain extends ProcessRunner {
return;
}
// Ignore top-level errors, they are already inside TestInfo.errors.
- const fakeTestInfo = new TestInfoImpl(this._config, this._project, this._params, undefined, 0, () => {}, () => {}, () => {});
+ const fakeTestInfo = new TestInfoImpl(this._config, this._project, this._params, undefined, 0, () => {}, () => {}, () => {}, () => {});
const runnable = { type: 'teardown' } as const;
// We have to load the project to get the right deadline below.
await fakeTestInfo._runWithTimeout(runnable, () => this._loadIfNeeded()).catch(() => {});
@@ -271,7 +271,8 @@ export class WorkerMain extends ProcessRunner {
const testInfo = new TestInfoImpl(this._config, this._project, this._params, test, retry,
stepBeginPayload => this.dispatchEvent('stepBegin', stepBeginPayload),
stepEndPayload => this.dispatchEvent('stepEnd', stepEndPayload),
- attachment => this.dispatchEvent('attach', attachment));
+ attachment => this.dispatchEvent('attach', attachment),
+ testPausedPayload => this.dispatchEvent('testPaused', testPausedPayload));
const processAnnotation = (annotation: TestAnnotation) => {
testInfo.annotations.push(annotation);
@@ -396,10 +397,7 @@ export class WorkerMain extends ProcessRunner {
try {
// Run "immediately upon test function finish" callback.
- await testInfo._runWithTimeout({ type: 'test', slot: afterHooksSlot }, async () => {
- for (const fn of testInfo._onDidFinishTestFunctions)
- await fn();
- });
+ await testInfo._runWithTimeout({ type: 'test', slot: afterHooksSlot }, () => testInfo._didFinishTestFunction());
} catch (error) {
firstAfterHooksError = firstAfterHooksError ?? error;
}
diff --git a/tests/mcp/mdb.spec.ts b/tests/mcp/mdb.spec.ts
index 668c1933f0..f8a408a8b9 100644
--- a/tests/mcp/mdb.spec.ts
+++ b/tests/mcp/mdb.spec.ts
@@ -19,11 +19,12 @@ import zodToJsonSchema from 'zod-to-json-schema';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
-import { runMainBackend, runOnPauseBackendLoop } from '../../packages/playwright/lib/mcp/sdk/mdb';
+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);
@@ -38,8 +39,8 @@ test('call top level tool', async () => {
description: 'Pause in gdb',
inputSchema: expect.any(Object),
}, {
- name: 'cli_pause_in_gdb_twice',
- description: 'Pause in gdb twice',
+ name: 'cli_restart',
+ description: 'Restart the process',
inputSchema: expect.any(Object),
}, {
name: 'gdb_bt',
@@ -119,7 +120,7 @@ test('outer and inner roots available', async () => {
await mdbClient.close();
});
-test('reset on pause tools', async () => {
+test('should reset', async () => {
const { mdbUrl, log } = await startMDBAndCLI();
const mdbClient = await createMDBClient(mdbUrl);
@@ -153,6 +154,41 @@ test('reset on pause tools', async () => {
'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();
});
@@ -183,10 +219,10 @@ async function startMDBAndCLI(): Promise<{ mdbUrl: string, log: string[] }> {
name: 'CLI',
nameInConfig: 'cli',
version: '0.0.0',
- create: () => new CLIBackend(log)
+ create: pushClient => new CLIBackend(log, pushClient)
};
- const mdbUrl = (await runMainBackend(cliBackendFactory, { port: 0 }))!;
+ const mdbUrl = (await mcp.runMainBackend(cliBackendFactory, { port: 0 }))!;
mdbUrlBox.mdbUrl = mdbUrl;
return { mdbUrl, log };
}
@@ -209,9 +245,12 @@ async function createMDBClient(mdbUrl: string, roots: any[] | undefined = undefi
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[]) {
+ constructor(log: string[], pushClient: (url: string, message: string) => Promise<void>) {
this._log = log;
+ this._pushClient = pushClient;
}
async initialize(server, clientInfo) {
@@ -230,8 +269,8 @@ class CLIBackend {
description: 'Pause in gdb',
inputSchema: zodToJsonSchema(z.object({})) as any,
}, {
- name: 'cli_pause_in_gdb_twice',
- description: 'Pause in gdb twice',
+ name: 'cli_restart',
+ description: 'Restart the process',
inputSchema: zodToJsonSchema(z.object({})) as any,
}, {
name: 'gdb_bt',
@@ -253,13 +292,21 @@ class CLIBackend {
if (name === 'cli_echo')
return { content: [{ type: 'text', text: `Echo: ${args?.message as string}, roots: ${stringifyRoots(this._roots)}` }] };
if (name === 'cli_pause_in_gdb') {
- await runOnPauseBackendLoop(new GDBBackend(this._log), 'Paused on exception');
+ 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_pause_in_gdb_twice') {
- await runOnPauseBackendLoop(new GDBBackend(this._log), 'Paused on exception 1');
- await runOnPauseBackendLoop(new GDBBackend(this._log), 'Paused on exception 2');
- 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}`);
}
diff --git a/tests/playwright-test/test-server.spec.ts b/tests/playwright-test/test-server.spec.ts
index e333827fe6..c70eb1fd8b 100644
--- a/tests/playwright-test/test-server.spec.ts
+++ b/tests/playwright-test/test-server.spec.ts
@@ -26,7 +26,11 @@ class WSTransport {
this._ws = new ws.WebSocket(url);
}
onmessage(listener: (message: string) => void) {
- this._ws.addEventListener('message', event => listener(event.data.toString()));
+ this._ws.addEventListener('message', event => {
+ if (process.env.PWTEST_DEBUG)
+ console.log('[test-server >>]', event.data.toString());
+ listener(event.data.toString());
+ });
}
onopen(listener: () => void) {
this._ws.addEventListener('open', listener);
@@ -38,6 +42,8 @@ class WSTransport {
this._ws.addEventListener('close', listener);
}
send(data: string) {
+ if (process.env.PWTEST_DEBUG)
+ console.log('[test-server <<]', data);
this._ws.send(data);
}
close() {
@@ -53,6 +59,7 @@ class TestServerConnectionUnderTest extends TestServerConnection {
this.onTestFilesChanged(params => this.events.push(['testFilesChanged', params]));
this.onStdio(params => this.events.push(['stdio', params]));
this.onLoadTraceRequested(params => this.events.push(['loadTraceRequested', params]));
+ this.onTestPaused(params => this.events.push(['testPaused', params]));
}
}
@@ -256,3 +263,67 @@ test('PLAYWRIGHT_TEST environment variable', async ({ startTestServer, writeFile
});
expect(await testServerConnection.runTests({})).toEqual({ status: 'passed' });
});
+
+test('pauseAtEnd', async ({ startTestServer, writeFiles }) => {
+ const testServerConnection = await startTestServer();
+ await testServerConnection.initialize({});
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('foo', () => {
+ });
+ `,
+ });
+
+ const promise = testServerConnection.runTests({ pauseAtEnd: true });
+ await expect.poll(() => testServerConnection.events.find(e => e[0] === 'testPaused')).toEqual(['testPaused', { errors: [] }]);
+ await testServerConnection.stopTests({});
+ expect(await promise).toEqual({ status: 'interrupted' });
+});
+
+test('pauseOnError', async ({ startTestServer, writeFiles }) => {
+ const testServerConnection = await startTestServer();
+ await testServerConnection.initialize({});
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('foo', () => {
+ expect(1).toBe(2);
+ });
+ `,
+ });
+
+ const promise = testServerConnection.runTests({ pauseOnError: true });
+ await expect.poll(() => testServerConnection.events.some(e => e[0] === 'testPaused')).toBeTruthy();
+ expect(testServerConnection.events.find(e => e[0] === 'testPaused')[1]).toEqual({
+ errors: [
+ expect.objectContaining({
+ message: expect.stringContaining('toBe'),
+ stack: expect.stringContaining('a.test.ts:4:19'),
+ location: {
+ file: expect.stringContaining('a.test.ts'),
+ line: 4,
+ column: 19,
+ },
+ }),
+ ]
+ });
+
+ await testServerConnection.stopTests({});
+ expect(await promise).toEqual({ status: 'interrupted' });
+});
+
+test('pauseOnError no errors', async ({ startTestServer, writeFiles }) => {
+ const testServerConnection = await startTestServer();
+ await testServerConnection.initialize({});
+ await writeFiles({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('foo', () => {
+ });
+ `,
+ });
+
+ expect(await testServerConnection.runTests({ pauseOnError: true })).toEqual({ status: 'passed' });
+ expect(testServerConnection.events.filter(e => e[0] === 'testPaused')).toEqual([]);
+});