Skip to content

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

chore(list): onTestPaused (#38777)

Simon Knott595983451517
diff --git a/packages/playwright/src/mcp/test/testContext.ts b/packages/playwright/src/mcp/test/testContext.ts
index 16fce05c55..863a34de9a 100644
--- a/packages/playwright/src/mcp/test/testContext.ts
+++ b/packages/playwright/src/mcp/test/testContext.ts
@@ -322,7 +322,7 @@ const bestPracticesMarkdown = `
`;
class MCPListReporter extends ListReporter {
- async onTestPaused() {
+ override async onTestPaused() {
// ListReporter waits for user input to resume, we don't want that in MCP.
await new Promise<void>(() => {});
}
diff --git a/packages/playwright/src/reporters/base.ts b/packages/playwright/src/reporters/base.ts
index f94ffebf29..200a975740 100644
--- a/packages/playwright/src/reporters/base.ts
+++ b/packages/playwright/src/reporters/base.ts
@@ -359,8 +359,8 @@ export class TerminalReporter implements ReporterV2 {
return formatError(this.screen, error);
}
- formatSingleResult(test: TestCase, result: TestResult, index?: number): string {
- return formatSingleResult(this.screen, this.config, test, result, index);
+ formatResultErrors(test: TestCase, result: TestResult): string {
+ return formatResultErrors(this.screen, test, result);
}
writeLine(line?: string) {
@@ -368,10 +368,8 @@ export class TerminalReporter implements ReporterV2 {
}
}
-function formatSingleResult(screen: Screen, config: FullConfig, test: TestCase, result: TestResult, index?: number): string {
+function formatResultErrors(screen: Screen, test: TestCase, result: TestResult): string {
const lines: string[] = [];
- const header = formatTestHeader(screen, config, test, { indent: ' ', index });
- lines.push(test.outcome() === 'unexpected' ? screen.colors.red(header) : screen.colors.yellow(header));
if (test.outcome() === 'unexpected') {
const errorDetails = formatResultFailure(screen, test, result, ' ');
if (errorDetails.length > 0)
diff --git a/packages/playwright/src/reporters/line.ts b/packages/playwright/src/reporters/line.ts
index 4bcda258f6..3f7b2b6313 100644
--- a/packages/playwright/src/reporters/line.ts
+++ b/packages/playwright/src/reporters/line.ts
@@ -86,9 +86,15 @@ class LineReporter extends TerminalReporter {
if (!process.env.PW_TEST_DEBUG_REPORTERS)
this.screen.stdout.write(`\u001B[1A\u001B[2K`);
- this.writeLine(this.formatSingleResult(test, result, test.outcome() === 'unexpected' ? ++this._failures : undefined));
- markErrorsAsReported(result);
- this.writeLine(this.screen.colors.yellow(` Paused ${test.outcome() === 'unexpected' ? 'on error' : 'at test end'}. Press Ctrl+C to end.`) + '\n\n');
+ if (test.outcome() === 'unexpected') {
+ this.writeLine(this.screen.colors.red(this.formatTestHeader(test, { indent: ' ', index: ++this._failures })));
+ this.writeLine(this.formatResultErrors(test, result));
+ markErrorsAsReported(result);
+ this.writeLine(this.screen.colors.yellow(` Paused on error. Press Ctrl+C to end.`) + '\n\n');
+ } else {
+ this.writeLine(this.screen.colors.yellow(this.formatTestHeader(test, { indent: ' ' })));
+ this.writeLine(this.screen.colors.yellow(` Paused at test end. Press Ctrl+C to end.`) + '\n\n');
+ }
this._updateLine(test, result, undefined);
diff --git a/packages/playwright/src/reporters/list.ts b/packages/playwright/src/reporters/list.ts
index b2b399e6eb..9315e3bfa3 100644
--- a/packages/playwright/src/reporters/list.ts
+++ b/packages/playwright/src/reporters/list.ts
@@ -17,7 +17,7 @@
import { getAsBooleanFromENV } from 'playwright-core/lib/utils';
import { ms as milliseconds } from 'playwright-core/lib/utilsBundle';
-import { TerminalReporter, stepSuffix } from './base';
+import { markErrorsAsReported, TerminalReporter, stepSuffix } from './base';
import { stripAnsiEscapes } from '../util';
import type { ListReporterOptions } from '../../types/test';
@@ -38,6 +38,7 @@ class ListReporter extends TerminalReporter {
private _stepIndex = new Map<TestStep, string>();
private _needNewLine = false;
private _printSteps: boolean;
+ private _paused = new Set<TestResult>();
constructor(options?: ListReporterOptions & CommonReporterOptions & TerminalReporterOptions) {
super(options);
@@ -165,9 +166,34 @@ class ListReporter extends TerminalReporter {
stream.write(chunk);
}
+ async onTestPaused(test: TestCase, result: TestResult) {
+ // Without TTY, user cannot interrupt the pause. Let's skip it.
+ if (!process.stdin.isTTY && !process.env.PW_TEST_DEBUG_REPORTERS)
+ return;
+
+ this._paused.add(result);
+
+ this._updateTestLine(test, result);
+ this._maybeWriteNewLine();
+ if (test.outcome() === 'unexpected') {
+ const errors = this.formatResultErrors(test, result);
+ this.writeLine(errors);
+ this._updateLineCountAndNewLineFlagForOutput(errors);
+ markErrorsAsReported(result);
+ }
+ this._appendLine(this.screen.colors.yellow(`Paused ${test.outcome() === 'unexpected' ? 'on error' : 'at test end'}. Press Ctrl+C to end.`), this._testPrefix('', ''));
+
+ await new Promise<void>(() => {});
+ }
+
override onTestEnd(test: TestCase, result: TestResult) {
super.onTestEnd(test, result);
+ const wasPaused = this._paused.delete(result);
+ if (!wasPaused)
+ this._updateTestLine(test, result);
+ }
+ private _updateTestLine(test: TestCase, result: TestResult) {
const title = this.formatTestTitle(test);
let prefix = '';
let text = '';
diff --git a/tests/playwright-test/reporter-list.spec.ts b/tests/playwright-test/reporter-list.spec.ts
index 976ce6f2f9..7d2a87f160 100644
--- a/tests/playwright-test/reporter-list.spec.ts
+++ b/tests/playwright-test/reporter-list.spec.ts
@@ -416,6 +416,141 @@ for (const useIntermediateMergeReport of [false, true] as const) {
});
}
+test.describe('onTestPaused', () => {
+ test.skip(process.platform === 'win32', 'No SIGINT on windows');
+
+ test('pause at end', async ({ interactWithTestRunner }) => {
+ const runner = await interactWithTestRunner({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('foo', async ({}) => {
+ });
+
+ test.afterEach(() => {
+ console.log('Running teardown');
+ });
+ `,
+ }, { pause: true, reporter: 'list' }, { PW_TEST_DEBUG_REPORTERS: '1' });
+
+ await runner.waitForOutput('Paused at test end. Press Ctrl+C to end.');
+ const { exitCode } = await runner.kill('SIGINT');
+ expect(exitCode).toBe(130);
+
+ const output = stripAnsi(runner.output).replace(/\d+(\.\d+)?m?s/g, 'Xms');
+ expect(output).toEqual(`
+Running 1 test using 1 worker
+
+#0 : ${POSITIVE_STATUS_MARK} 1 a.test.ts:3:13 › foo (Xms)
+#1 : Paused at test end. Press Ctrl+C to end.
+Running teardown
+
+ 1 interrupted
+ a.test.ts:3:13 › foo ───────────────────────────────────────────────────────────────────────────
+`);
+ });
+
+ test('pause at end - error in teardown', async ({ interactWithTestRunner }) => {
+ const runner = await interactWithTestRunner({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('foo', async ({}) => {
+ });
+
+ test.afterEach(() => {
+ throw new Error('teardown error');
+ });
+ `,
+ }, { pause: true, reporter: 'list' }, { PW_TEST_DEBUG_REPORTERS: '1' });
+
+ await runner.waitForOutput('Paused at test end. Press Ctrl+C to end.');
+ const { exitCode } = await runner.kill('SIGINT');
+ expect(exitCode).toBe(130);
+
+ const output = stripAnsi(runner.output).replace(/\d+(\.\d+)?m?s/g, 'Xms');
+ expect(output).toEqual(`
+Running 1 test using 1 worker
+
+#0 : ${POSITIVE_STATUS_MARK} 1 a.test.ts:3:13 › foo (Xms)
+#1 : Paused at test end. Press Ctrl+C to end.
+
+
+ 1) a.test.ts:3:13 › foo ──────────────────────────────────────────────────────────────────────────
+
+ Test was interrupted.
+
+ Error: teardown error
+
+ 5 |
+ 6 | test.afterEach(() => {
+ > 7 | throw new Error('teardown error');
+ | ^
+ 8 | });
+ 9 |
+ at ${test.info().outputPath('a.test.ts')}:7:17
+
+ 1 interrupted
+ a.test.ts:3:13 › foo ───────────────────────────────────────────────────────────────────────────
+`);
+ });
+
+ test('pause on error', async ({ interactWithTestRunner }) => {
+ const runner = await interactWithTestRunner({
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('fails', async ({}) => {
+ expect.soft(2).toBe(3);
+ expect(3).toBe(4);
+ });
+ `,
+ }, { pause: true, reporter: 'list' }, { PW_TEST_DEBUG_REPORTERS: '1' });
+
+ await runner.waitForOutput('Paused on error. Press Ctrl+C to end.');
+ const { exitCode } = await runner.kill('SIGINT');
+ expect(exitCode).toBe(130);
+
+ const output = stripAnsi(runner.output).replace(/\d+(\.\d+)?m?s/g, 'Xms');
+ expect(output).toEqual(`
+Running 1 test using 1 worker
+
+#0 : ${NEGATIVE_STATUS_MARK} 1 a.test.ts:3:13 › fails (Xms)
+
+ Error: expect(received).toBe(expected) // Object.is equality
+
+ Expected: 3
+ Received: 2
+
+ 2 | import { test, expect } from '@playwright/test';
+ 3 | test('fails', async ({}) => {
+ > 4 | expect.soft(2).toBe(3);
+ | ^
+ 5 | expect(3).toBe(4);
+ 6 | });
+ 7 |
+ at ${test.info().outputPath('a.test.ts')}:4:26
+
+ Error: expect(received).toBe(expected) // Object.is equality
+
+ Expected: 4
+ Received: 3
+
+ 3 | test('fails', async ({}) => {
+ 4 | expect.soft(2).toBe(3);
+ > 5 | expect(3).toBe(4);
+ | ^
+ 6 | });
+ 7 |
+ at ${test.info().outputPath('a.test.ts')}:5:21
+
+#1 : Paused on error. Press Ctrl+C to end.
+
+
+
+ 1 failed
+ a.test.ts:3:13 › fails ─────────────────────────────────────────────────────────────────────────
+`);
+ });
+});
+
function simpleAnsiRenderer(text, ttyWidth) {
let lineNumber = 0;
let columnNumber = 0;