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: pause test timeout when paused inside an api call (#39704)
Dmitry Gozman506b09bd6436
diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.tsindex 7d9cfe12a5..9ff01936ce 100644--- a/packages/playwright-core/src/server/browserContext.ts+++ b/packages/playwright-core/src/server/browserContext.ts@@ -19,7 +19,7 @@ import fs from 'fs';import path from 'path';import { createGuid } from './utils/crypto';-import { debugMode } from './utils/debug';+import { debugMode, isUnderTest } from './utils/debug';import { Clock } from './clock';import { Debugger } from './debugger';import { DialogManager } from './dialog';@@ -149,18 +149,21 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk// Debugger will pause execution upon page.pause in headed mode.this._debugger = new Debugger(this);- // When PWDEBUG=1, show inspector for each context.- if (debugMode() === 'inspector')- await RecorderApp.show(this, { pauseOnNextStatement: true });-// When paused, show inspector.- if (this._debugger.isPaused())- RecorderApp.showInspectorNoReply(this);+ const shouldEnableDebugger = !this.attribution.playwright.options.isServer && (isUnderTest() || !!this._browser.options.headful);+ if (shouldEnableDebugger) {+ this._debugger.setPauseAt();+ this._debugger.on(Debugger.Events.PausedStateChanged, () => {+ if (this._debugger.isPaused())+ RecorderApp.showInspectorNoReply(this);+ });+ }- this._debugger.on(Debugger.Events.PausedStateChanged, () => {- if (this._debugger.isPaused())- RecorderApp.showInspectorNoReply(this);- });+ // When PWDEBUG=1, show inspector for each context.+ if (debugMode() === 'inspector') {+ this._debugger.setPauseAt({ next: true });+ await RecorderApp.show(this, { pauseOnNextStatement: true });+ }if (debugMode() === 'console')await this.exposeConsoleApi();diff --git a/packages/playwright-core/src/server/debugger.ts b/packages/playwright-core/src/server/debugger.tsindex d8f76990d6..63e2735802 100644--- a/packages/playwright-core/src/server/debugger.ts+++ b/packages/playwright-core/src/server/debugger.ts@@ -15,7 +15,7 @@*/import { SdkObject } from './instrumentation';-import { debugMode, isUnderTest, monotonicTime } from '../utils';+import { monotonicTime } from '../utils';import { BrowserContext } from './browserContext';import { methodMetainfo } from '../utils/isomorphic/protocolMetainfo';@@ -28,7 +28,7 @@ type PauseAt = { next?: boolean, location?: { file: string, line?: number, columexport class Debugger extends SdkObject implements InstrumentationListener {private _pauseAt: PauseAt = {};private _pausedCallsMetadata = new Map<CallMetadata, { resolve: () => void, sdkObject: SdkObject }>();- private _enabled: boolean;+ private _enabled = false;private _context: BrowserContext;static Events = {@@ -40,9 +40,6 @@ export class Debugger extends SdkObject implements InstrumentationListener {super(context, 'debugger');this._context = context;(this._context as any)[symbol] = this;- this._enabled = !context.attribution.playwright.options.isServer && (isUnderTest() || !!context._browser.options.headful);- if (debugMode() === 'inspector')- this.setPauseAt({ next: true });context.instrumentation.addListener(this, context);this._context.once(BrowserContext.Events.Close, () => {this._context.instrumentation.removeListener(this);diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.tsindex 38346b9774..06bf458ad9 100644--- a/packages/playwright/src/index.ts+++ b/packages/playwright/src/index.ts@@ -237,7 +237,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({testInfo.snapshotSuffix = process.platform;testInfo._onCustomMessageCallback = () => Promise.reject(new Error('Only tests that use default Playwright context or page fixture support test_debug'));if (debugMode() === 'inspector')- (testInfo as TestInfoImpl)._setDebugMode();+ (testInfo as TestInfoImpl)._setIgnoreTimeouts(true);playwright._defaultContextTimeout = actionTimeout || 0;playwright._defaultContextNavigationTimeout = navigationTimeout || 0;@@ -256,6 +256,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({await artifactsRecorder.willStartTest(testInfo as TestInfoImpl);const tracingGroupSteps: TestStepInternal[] = [];+ const pausedContexts = new Set<BrowserContextImpl>();const csiListener: ClientInstrumentationListener = {onApiCallBegin: (data, channel) => {const testInfo = currentTestInfo();@@ -304,7 +305,7 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({},onWillPause: ({ keepTestTimeout }) => {if (!keepTestTimeout)- currentTestInfo()?._setDebugMode();+ currentTestInfo()?._setIgnoreTimeouts(true);},runBeforeCreateBrowserContext: async (options: BrowserContextOptions) => {for (const [key, value] of Object.entries(_combinedContextOptions)) {@@ -319,6 +320,17 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({}},runAfterCreateBrowserContext: async (context: BrowserContextImpl) => {+ context.debugger.on('pausedstatechanged', () => {+ const paused = context.debugger.pausedDetails().length > 0;+ if (pausedContexts.has(context) && !paused) {+ pausedContexts.delete(context);+ (testInfo as TestInfoImpl)._setIgnoreTimeouts(false);+ } else if (!pausedContexts.has(context) && paused) {+ pausedContexts.add(context);+ (testInfo as TestInfoImpl)._setIgnoreTimeouts(true);+ }+ });+await artifactsRecorder.didCreateBrowserContext(context);const testInfo = currentTestInfo();if (testInfo)diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.tsindex 1a5a6f3091..3dca10c7fa 100644--- a/packages/playwright/src/worker/testInfo.ts+++ b/packages/playwright/src/worker/testInfo.ts@@ -134,6 +134,7 @@ export class TestInfoImpl implements TestInfo {errors: ipc.TestInfoErrorImpl[] = [];readonly _attachmentsPush: (...items: TestInfo['attachments']) => number;private _workerParams: ipc.WorkerInitParams;+ private _ignoreTimeoutsCounter = 0;get error(): ipc.TestInfoErrorImpl | undefined {return this.errors[0];@@ -201,7 +202,7 @@ export class TestInfoImpl implements TestInfo {this._timeoutManager = new TimeoutManager(this.project.timeout);if (configInternal.configCLIOverrides.debug)- this._setDebugMode();+ this._setIgnoreTimeouts(true);this.outputDir = (() => {const relativeTestFilePath = path.relative(this.project.testDir, this._requireFile.replace(/\.(spec|test)\.(js|ts|jsx|tsx|mjs|mts|cjs|cts)$/, ''));@@ -462,8 +463,9 @@ export class TestInfoImpl implements TestInfo {return ['beforeAll', 'afterAll', 'beforeEach', 'afterEach'].includes(type) ? type : undefined;}- _setDebugMode() {- this._timeoutManager.setIgnoreTimeouts();+ _setIgnoreTimeouts(ignoreTimeouts: boolean) {+ this._ignoreTimeoutsCounter += ignoreTimeouts ? 1 : -1;+ this._timeoutManager.setIgnoreTimeouts(this._ignoreTimeoutsCounter > 0);}async _didFinishTestFunction() {diff --git a/packages/playwright/src/worker/timeoutManager.ts b/packages/playwright/src/worker/timeoutManager.tsindex 5d930f5338..ec4f0fb691 100644--- a/packages/playwright/src/worker/timeoutManager.ts+++ b/packages/playwright/src/worker/timeoutManager.ts@@ -62,10 +62,17 @@ export class TimeoutManager {this._defaultSlot = { timeout, elapsed: 0 };}- setIgnoreTimeouts() {- this._ignoreTimeouts = true;- if (this._running)+ setIgnoreTimeouts(ignoreTimeouts: boolean) {+ if (this._ignoreTimeouts === ignoreTimeouts)+ return;+ this._ignoreTimeouts = ignoreTimeouts;+ if (this._running) {+ if (ignoreTimeouts)+ this._running.slot.elapsed += monotonicTime() - this._running.start;+ else+ this._running.start = monotonicTime();this._updateTimeout(this._running);+ }}interrupt() {diff --git a/tests/playwright-test/playwright.spec.ts b/tests/playwright-test/playwright.spec.tsindex 52300f66f0..1d2fd3b665 100644--- a/tests/playwright-test/playwright.spec.ts+++ b/tests/playwright-test/playwright.spec.ts@@ -963,3 +963,22 @@ test('init script should not observe playwright internals', async ({ server, run}, {}, { PWDEBUG: '0' });expect(result.exitCode).toBe(0);});++test('should pause test timeout while on pause', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'a.test.ts': `+ import { test, expect } from '@playwright/test';++ test('test', async ({ page, context }) => {+ await context.debugger.setPauseAt({ next: true });+ const paused = new Promise(f => context.debugger.once('pausedstatechanged', f));+ const contentPromise = page.setContent('<div>hello</div>');+ await paused;+ await new Promise(f => setTimeout(f, 5000));+ await context.debugger.resume();+ await contentPromise;+ });+ `,+ }, { timeout: 3000 });+ expect(result.exitCode).toBe(0);+});