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: reduce plumbing for pauseAtEnd/pauseOnError (#37515)
Dmitry Gozmanbbc71098fe8d
diff --git a/packages/playwright-core/src/client/channelOwner.ts b/packages/playwright-core/src/client/channelOwner.tsindex 26bc4bab7a..7bd097b3ff 100644--- a/packages/playwright-core/src/client/channelOwner.ts+++ b/packages/playwright-core/src/client/channelOwner.ts@@ -20,7 +20,7 @@ import { methodMetainfo } from '../utils/isomorphic/protocolMetainfo';import { captureLibraryStackTrace } from './clientStackTrace';import { stringifyStackFrames } from '../utils/isomorphic/stackTrace';-import type { ClientInstrumentation, RecoverFromApiErrorHandler } from './clientInstrumentation';+import type { ClientInstrumentation } from './clientInstrumentation';import type { Connection } from './connection';import type { Logger } from './types';import type { ValidatorContext } from '../protocol/validator';@@ -199,11 +199,7 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channelelsee.stack = '';if (!options?.internal) {- const recoveryHandlers: RecoverFromApiErrorHandler[] = [];apiZone.error = e;- this._instrumentation.onApiCallRecovery(apiZone, e, this, recoveryHandlers);- for (const handler of recoveryHandlers)- await handler();logApiCall(this._platform, logger, `<= ${apiZone.apiName} failed`);this._instrumentation.onApiCallEnd(apiZone);}diff --git a/packages/playwright-core/src/client/clientInstrumentation.ts b/packages/playwright-core/src/client/clientInstrumentation.tsindex a049668ea0..da63f59b4d 100644--- a/packages/playwright-core/src/client/clientInstrumentation.ts+++ b/packages/playwright-core/src/client/clientInstrumentation.ts@@ -15,7 +15,6 @@*/import type { BrowserContext } from './browserContext';-import type { ChannelOwner } from './channelOwner';import type { APIRequestContext } from './fetch';import type { StackFrame } from '@protocol/channels';@@ -29,14 +28,11 @@ export interface ApiCallData {error?: Error;}-export type RecoverFromApiErrorHandler = () => Promise<void>;-export interface ClientInstrumentation {addListener(listener: ClientInstrumentationListener): void;removeListener(listener: ClientInstrumentationListener): void;removeAllListeners(): void;onApiCallBegin(apiCall: ApiCallData, channel: { type: string, method: string, params?: Record<string, any> }): void;- onApiCallRecovery(apiCall: ApiCallData, error: Error, channelOwner: ChannelOwner, recoveryHandlers: RecoverFromApiErrorHandler[]): void;onApiCallEnd(apiCall: ApiCallData): void;onWillPause(options: { keepTestTimeout: boolean }): void;@@ -48,7 +44,6 @@ export interface ClientInstrumentation {export interface ClientInstrumentationListener {onApiCallBegin?(apiCall: ApiCallData, channel: { type: string, method: string, params?: Record<string, any> }): void;- onApiCallRecovery?(apiCall: ApiCallData, error: Error, channelOwner: ChannelOwner, recoveryHandlers: RecoverFromApiErrorHandler[]): void;onApiCallEnd?(apiCall: ApiCallData): void;onWillPause?(options: { keepTestTimeout: boolean }): void;diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.tsindex 40fa04ae7c..6c412c0d98 100644--- a/packages/playwright/src/index.ts+++ b/packages/playwright/src/index.ts@@ -22,11 +22,9 @@ import { setBoxedStackPrefixes, createGuid, currentZone, debugMode, jsonStringifimport { currentTestInfo } from './common/globals';import { rootTestType } from './common/testType';-import { runBrowserBackendOnError, runBrowserBackendAtEnd } from './mcp/test/browserBackend';-import { codeFrameColumns } from './transform/babelBundle';-import { stripAnsiEscapes } from './util';+import { runBrowserBackendAtEnd } from './mcp/test/browserBackend';-import type { Fixtures, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, ScreenshotMode, TestInfo, TestType, VideoMode, Location } from '../types/test';+import type { Fixtures, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, ScreenshotMode, TestInfo, TestType, VideoMode } from '../types/test';import type { ContextReuseMode } from './common/config';import type { TestInfoImpl, TestStepInternal } from './worker/testInfo';import type { ClientInstrumentationListener } from '../../playwright-core/src/client/clientInstrumentation';@@ -36,7 +34,6 @@ import type { BrowserContext as BrowserContextImpl } from '../../playwright-coreimport type { APIRequestContext as APIRequestContextImpl } from '../../playwright-core/src/client/fetch';import type { ChannelOwner } from '../../playwright-core/src/client/channelOwner';import type { Page as PageImpl } from '../../playwright-core/src/client/page';-import type { Frame as FrameImpl } from '../../playwright-core/src/client/frame';import type { BrowserContext, BrowserContextOptions, LaunchOptions, Page, Tracing } from 'playwright-core';export { expect } from './matchers/expect';@@ -291,23 +288,6 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({if (data.apiName === 'tracing.group')tracingGroupSteps.push(step);},- onApiCallRecovery: (data, error, channelOwner, recoveryHandlers) => {- const testInfo = currentTestInfo();- if (!testInfo || !testInfo._pauseOnError())- return;-- const step = data.userData;- if (!step)- return;- const page = channelToPage(channelOwner);- if (!page)- return;- recoveryHandlers.push(async () => {- await runBrowserBackendOnError(page, () => {- return stripAnsiEscapes(createErrorCodeframe(error.message, step.location));- });- });- },onApiCallEnd: data => {// "tracing.group" step will end later, when "tracing.groupEnd" finishes.@@ -437,13 +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));await use(context);- await runBrowserBackendAtEnd(context);await close();return;}const context = await browserImpl._wrapApiCall(() => browserImpl._newContextForReuse(), { internal: true });+ (testInfo as TestInfoImpl)._onDidFinishTestFunctions.unshift(() => runBrowserBackendAtEnd(context, testInfo.errors[0]?.message));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 });@@ -666,7 +647,7 @@ class ArtifactsRecorder {async willStartTest(testInfo: TestInfoImpl) {this._testInfo = testInfo;- testInfo._onDidFinishTestFunction = () => this.didFinishTestFunction();+ testInfo._onDidFinishTestFunctions.push(() => this.didFinishTestFunction());this._screenshotRecorder.fixOrdinal();@@ -804,36 +785,3 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>(playwrightFixexport { defineConfig } from './common/configLoader';export { mergeTests } from './common/testType';export { mergeExpects } from './matchers/expect';--function channelToPage(channelOwner: ChannelOwner): Page | undefined {- if (channelOwner._type === 'Page')- return channelOwner as PageImpl;- if (channelOwner._type === 'Frame')- return (channelOwner as FrameImpl).page();- return undefined;-}--function createErrorCodeframe(message: string, location: Location) {- let source: string;- try {- source = fs.readFileSync(location.file, 'utf-8') + '\n//';- } catch (e) {- return '';- }-- return codeFrameColumns(- source,- {- start: {- line: location.line,- column: location.column,- },- },- {- highlightCode: true,- linesAbove: 5,- linesBelow: 5,- message: message.split('\n')[0] || undefined,- }- );-}diff --git a/packages/playwright/src/matchers/toBeTruthy.ts b/packages/playwright/src/matchers/toBeTruthy.tsindex 89940e7a3e..0802277421 100644--- a/packages/playwright/src/matchers/toBeTruthy.ts+++ b/packages/playwright/src/matchers/toBeTruthy.ts@@ -16,7 +16,6 @@import { expectTypes } from '../util';import { formatMatcherMessage } from './matcherHint';-import { runBrowserBackendOnError } from '../mcp/test/browserBackend';import type { MatcherResult } from './matcherHint';import type { ExpectMatcherState } from '../../types/test';@@ -70,8 +69,6 @@ export async function toBeTruthy(});};- await runBrowserBackendOnError(locator.page(), message);-return {message,pass,diff --git a/packages/playwright/src/matchers/toEqual.ts b/packages/playwright/src/matchers/toEqual.tsindex 082eaa3b4d..e22f8a9c65 100644--- a/packages/playwright/src/matchers/toEqual.ts+++ b/packages/playwright/src/matchers/toEqual.ts@@ -18,7 +18,6 @@ import { isRegExp } from 'playwright-core/lib/utils';import { expectTypes } from '../util';import { formatMatcherMessage } from './matcherHint';-import { runBrowserBackendOnError } from '../mcp/test/browserBackend';import type { MatcherResult } from './matcherHint';import type { ExpectMatcherState } from '../../types/test';@@ -99,8 +98,6 @@ export async function toEqual<T>(});};- await runBrowserBackendOnError(locator.page(), message);-// Passing the actual and expected objects so that a custom reporter// could access them, for example in order to display a custom visual diff,// or create a different error messagediff --git a/packages/playwright/src/matchers/toMatchText.ts b/packages/playwright/src/matchers/toMatchText.tsindex 8c5cfd06dc..508702bcd0 100644--- a/packages/playwright/src/matchers/toMatchText.ts+++ b/packages/playwright/src/matchers/toMatchText.ts@@ -22,7 +22,6 @@ import {} from './expect';import { formatMatcherMessage } from './matcherHint';import { EXPECTED_COLOR } from '../common/expectBundle';-import { runBrowserBackendOnError } from '../mcp/test/browserBackend';import type { MatcherResult } from './matcherHint';import type { ExpectMatcherState } from '../../types/test';@@ -103,9 +102,6 @@ export async function toMatchText(});};- if (locator)- await runBrowserBackendOnError(locator.page(), message);-return {name: matcherName,expected,diff --git a/packages/playwright/src/mcp/test/browserBackend.ts b/packages/playwright/src/mcp/test/browserBackend.tsindex 38d2a7e22b..3653e7c4e7 100644--- a/packages/playwright/src/mcp/test/browserBackend.ts+++ b/packages/playwright/src/mcp/test/browserBackend.ts@@ -25,50 +25,46 @@ import type { Page } from '../../../../playwright-core/src/client/page';import type { BrowserContextFactory } from '../browser/browserContextFactory';import type { ClientInfo } from '../sdk/server';-export async function runBrowserBackendOnError(page: playwright.Page, message: () => string) {+export async function runBrowserBackendAtEnd(context: playwright.BrowserContext, errorMessage?: string) {const testInfo = currentTestInfo();- if (!testInfo || !testInfo._pauseOnError())+ if (!testInfo)return;- const config: FullConfig = {- ...defaultConfig,- capabilities: ['testing'],- };-- const snapshot = await (page as Page)._snapshotForAI();- const introMessage = `### Paused on error:-${stripAnsiEscapes(message())}--### Current page snapshot:-${snapshot}--### Task-Try recovering from the error prior to continuing`;-- await mcp.runOnPauseBackendLoop(new BrowserServerBackend(config, identityFactory(page.context())), introMessage);-}--export async function runBrowserBackendAtEnd(context: playwright.BrowserContext) {- const testInfo = currentTestInfo();- if (!testInfo || !testInfo._pauseAtEnd())+ const shouldPause = errorMessage ? testInfo?._pauseOnError() : testInfo?._pauseAtEnd();+ if (!shouldPause)return;- const page = context.pages()[0];- if (!page)- return;+ const lines: string[] = [];+ if (errorMessage)+ lines.push(`### Paused on error:`, stripAnsiEscapes(errorMessage));+ else+ lines.push(`### Paused at end of test. ready for interaction`);- const snapshot = await (page as Page)._snapshotForAI();- const introMessage = `### 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';+ lines.push(+ '',+ `### Page ${stateSuffix}`,+ `- Page URL: ${page.url()}`,+ `- Page Title: ${await page.title()}`.trim(),+ `- Page Snapshot:`,+ '```yaml',+ await (page as Page)._snapshotForAI(),+ '```',+ );+ }-### Current page snapshot:-${snapshot}`;+ 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)), introMessage);+ await mcp.runOnPauseBackendLoop(new BrowserServerBackend(config, identityFactory(context)), lines.join('\n'));}function identityFactory(browserContext: playwright.BrowserContext): BrowserContextFactory {diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.tsindex aba9a28b9f..e4add9bbae 100644--- a/packages/playwright/src/worker/testInfo.ts+++ b/packages/playwright/src/worker/testInfo.ts@@ -83,7 +83,7 @@ export class TestInfoImpl implements TestInfo {readonly _configInternal: FullConfigInternal;private readonly _steps: TestStepInternal[] = [];private readonly _stepMap = new Map<string, TestStepInternal>();- _onDidFinishTestFunction: (() => Promise<void>) | undefined;+ _onDidFinishTestFunctions: (() => Promise<void>)[] = [];_hasNonRetriableError = false;_hasUnhandledError = false;_allowSkips = false;diff --git a/packages/playwright/src/worker/workerMain.ts b/packages/playwright/src/worker/workerMain.tsindex 29f421dcc9..154c341a4b 100644--- a/packages/playwright/src/worker/workerMain.ts+++ b/packages/playwright/src/worker/workerMain.ts@@ -396,7 +396,10 @@ export class WorkerMain extends ProcessRunner {try {// Run "immediately upon test function finish" callback.- await testInfo._runWithTimeout({ type: 'test', slot: afterHooksSlot }, async () => testInfo._onDidFinishTestFunction?.());+ await testInfo._runWithTimeout({ type: 'test', slot: afterHooksSlot }, async () => {+ for (const fn of testInfo._onDidFinishTestFunctions)+ await fn();+ });} catch (error) {firstAfterHooksError = firstAfterHooksError ?? error;}diff --git a/tests/mcp/test-tools.spec.ts b/tests/mcp/test-tools.spec.tsindex 76151f87aa..9da107e4f4 100644--- a/tests/mcp/test-tools.spec.ts+++ b/tests/mcp/test-tools.spec.ts@@ -242,7 +242,7 @@ test('test_debug (pause/resume)', async ({ startClient }) => {test: { id, title: 'fail' },},})).toHaveTextResponse(`### Paused on error:-expect(locator).toBeVisible() failed+Error: expect(locator).toBeVisible() failedLocator: getByRole('button', { name: 'Missing' })Expected: visible@@ -254,8 +254,13 @@ Call log:- waiting for getByRole('button', { name: 'Missing' })-### Current page snapshot:+### Page state+- Page URL: about:blank+- Page Title:+- Page Snapshot:+\`\`\`yaml- button "Submit" [ref=e2]+\`\`\`### TaskTry recovering from the error prior to continuing`);@@ -303,6 +308,45 @@ test('test_debug (browser_snapshot/network/console)', async ({ startClient, serv});});+test('test_debug (multiple pages and custom errors)', async ({ startClient, server }) => {+ const { client, id } = await prepareDebugTest(startClient, `+ import { test, expect } from '@playwright/test';+ test('fail', async ({ page }) => {+ const page2 = await page.context().newPage();+ await page.goto(${JSON.stringify(server.PREFIX + '/frames/frame.html')});+ await page2.goto(${JSON.stringify(server.PREFIX + '/wrappedlink.html')});+ throw new Error('non-api error');+ });+ `);+ expect(await client.callTool({+ name: 'test_debug',+ arguments: {+ test: { id, title: 'fail' },+ },+ })).toHaveTextResponse(`### Paused on error:+Error: non-api error++### Page 1 of 2+- Page URL: ${server.PREFIX + '/frames/frame.html'}+- Page Title:+- Page Snapshot:+\`\`\`yaml+- generic [ref=e2]: Hi, I'm frame+\`\`\`++### Page 2 of 2+- Page URL: ${server.PREFIX + '/wrappedlink.html'}+- Page Title:+- Page Snapshot:+\`\`\`yaml+- link "123321" [ref=e3] [cursor=pointer]:+ - /url: "#clicked"+\`\`\`++### Task+Try recovering from the error prior to continuing`);+});+test('test_debug (pause/snapshot/resume)', async ({ startClient }) => {const { client, id } = await prepareDebugTest(startClient);@@ -312,7 +356,7 @@ test('test_debug (pause/snapshot/resume)', async ({ startClient }) => {test: { id, title: 'fail' },},})).toHaveTextResponse(`### Paused on error:-expect(locator).toBeVisible() failed+Error: expect(locator).toBeVisible() failedLocator: getByRole('button', { name: 'Missing' })Expected: visible@@ -324,8 +368,13 @@ Call log:- waiting for getByRole('button', { name: 'Missing' })-### Current page snapshot:+### Page state+- Page URL: about:blank+- Page Title:+- Page Snapshot:+\`\`\`yaml- button "Submit" [ref=e2]+\`\`\`### TaskTry recovering from the error prior to continuing`);@@ -421,8 +470,14 @@ test('test_setup_page', async ({ startClient }) => {},})).toHaveTextResponse(`### Paused at end of test. ready for interaction-### Current page snapshot:-- button "Submit" [ref=e2]`);+### Page state+- Page URL: about:blank+- Page Title:+- Page Snapshot:+\`\`\`yaml+- button "Submit" [ref=e2]+\`\`\`+`);expect(await client.callTool({name: 'browser_click',@@ -470,7 +525,13 @@ test('test_setup_page with dependencies', async ({ startClient }) => {},})).toHaveTextResponse(`### Paused at end of test. ready for interaction-### Current page snapshot:+### Page state+- Page URL: about:blank+- Page Title:+- Page Snapshot:+\`\`\`yaml++\`\`\``);// Should pause at the target test, not in a dependency or any other stray project.@@ -486,7 +547,13 @@ test('test_setup_page (no test location)', async ({ startClient }) => {arguments: {},})).toHaveTextResponse(`### Paused at end of test. ready for interaction-### Current page snapshot:+### Page state+- Page URL: about:blank+- Page Title:+- Page Snapshot:+\`\`\`yaml++\`\`\``);});@@ -508,7 +575,13 @@ test('test_setup_page chooses top-level project', async ({ startClient }) => {arguments: {},})).toHaveTextResponse(`### Paused at end of test. ready for interaction-### Current page snapshot:+### Page state+- Page URL: about:blank+- Page Title:+- Page Snapshot:+\`\`\`yaml++\`\`\``);expect(fs.existsSync(path.join(baseDir, 'one', 'default.seed.spec.ts'))).toBe(false);@@ -537,7 +610,13 @@ test('test_setup_page without location respects testsDir', async ({ startClientarguments: {},})).toHaveTextResponse(`### Paused at end of test. ready for interaction-### Current page snapshot:+### Page state+- Page URL: about:blank+- Page Title:+- Page Snapshot:+\`\`\`yaml++\`\`\``);expect(fs.existsSync(test.info().outputPath('tests', 'default.seed.spec.ts'))).toBe(true);});