Skip to content

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

fix: boxed fixtures should hide all steps inside them (#36952)

Dmitry Gozman5965db1bd782
diff --git a/packages/playwright/src/worker/fixtureRunner.ts b/packages/playwright/src/worker/fixtureRunner.ts
index 8a792eebfd..ad76b3a360 100644
--- a/packages/playwright/src/worker/fixtureRunner.ts
+++ b/packages/playwright/src/worker/fixtureRunner.ts
@@ -35,7 +35,7 @@ class Fixture {
private _selfTeardownComplete: Promise<void> | undefined;
private _setupDescription: FixtureDescription;
private _teardownDescription: FixtureDescription;
- private _stepInfo: { title: string, category: 'fixture', location?: Location } | undefined;
+ private _stepInfo: { title: string, category: 'fixture', location?: Location, hidden?: boolean };
_deps = new Set<Fixture>();
_usages = new Set<Fixture>();
@@ -43,11 +43,11 @@ class Fixture {
this.runner = runner;
this.registration = registration;
this.value = null;
- const shouldGenerateStep = !this.registration.box && !this.registration.option;
+ const hidden = this.registration.box || this.registration.option;
const isUserFixture = this.registration.location && filterStackFile(this.registration.location.file);
const title = this.registration.customTitle || this.registration.name;
const location = isUserFixture ? this.registration.location : undefined;
- this._stepInfo = shouldGenerateStep ? { title, category: 'fixture', location } : undefined;
+ this._stepInfo = { title, category: 'fixture', location, hidden };
this._setupDescription = {
title,
phase: 'setup',
@@ -68,11 +68,9 @@ class Fixture {
return;
}
- const run = () => testInfo._runWithTimeout({ ...runnable, fixture: this._setupDescription }, () => this._setupInternal(testInfo));
- if (this._stepInfo)
- await testInfo._runAsStep(this._stepInfo, run);
- else
- await run();
+ await testInfo._runAsStep(this._stepInfo, async () => {
+ await testInfo._runWithTimeout({ ...runnable, fixture: this._setupDescription }, () => this._setupInternal(testInfo));
+ });
}
private async _setupInternal(testInfo: TestInfoImpl) {
@@ -131,11 +129,9 @@ class Fixture {
// Do not even start the teardown for a fixture that does not have any
// time remaining in the time slot. This avoids cascading timeouts.
if (!testInfo._timeoutManager.isTimeExhaustedFor(fixtureRunnable)) {
- const run = () => testInfo._runWithTimeout(fixtureRunnable, () => this._teardownInternal());
- if (this._stepInfo)
- await testInfo._runAsStep(this._stepInfo, run);
- else
- await run();
+ await testInfo._runAsStep(this._stepInfo, async () => {
+ await testInfo._runWithTimeout(fixtureRunnable, () => this._teardownInternal());
+ });
}
} finally {
// To preserve fixtures integrity, forcefully cleanup fixtures
diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.ts
index 0eb185c211..c0d62e131a 100644
--- a/packages/playwright/src/worker/testInfo.ts
+++ b/packages/playwright/src/worker/testInfo.ts
@@ -34,22 +34,26 @@ import type { TestCase } from '../common/test';
import type { StackFrame } from '@protocol/channels';
import type { TestStepCategory } from '../util';
-export interface TestStepInternal {
- complete(result: { error?: Error | unknown, suggestedRebaseline?: string }): void;
- info: TestStepInfoImpl
- attachmentIndices: number[];
- stepId: string;
+interface TestStepData {
title: string;
category: TestStepCategory;
location?: Location;
- boxedStack?: StackFrame[];
- steps: TestStepInternal[];
- endWallTime?: number;
apiName?: string;
params?: Record<string, any>;
- error?: TestInfoErrorImpl;
infectParentStepsWithError?: boolean;
box?: boolean;
+ hidden?: boolean;
+}
+
+export interface TestStepInternal extends TestStepData {
+ complete(result: { error?: Error | unknown, suggestedRebaseline?: string }): void;
+ info: TestStepInfoImpl;
+ attachmentIndices: number[];
+ stepId: string;
+ boxedStack?: StackFrame[];
+ steps: TestStepInternal[];
+ endWallTime?: number;
+ error?: TestInfoErrorImpl;
}
type SnapshotNames = {
@@ -261,7 +265,7 @@ export class TestInfoImpl implements TestInfo {
return currentZone().data<TestStepInternal>('stepZone') ?? this._findLastPredefinedStep(this._steps);
}
- _addStep(data: Omit<TestStepInternal, 'complete' | 'stepId' | 'steps' | 'attachmentIndices' | 'info'>, parentStep?: TestStepInternal): TestStepInternal {
+ _addStep(data: Readonly<TestStepData>, parentStep?: TestStepInternal): TestStepInternal {
const stepId = `${data.category}@${++this._lastStepId}`;
if (data.category === 'hook' || data.category === 'fixture') {
@@ -273,19 +277,23 @@ export class TestInfoImpl implements TestInfo {
}
const filteredStack = filteredStackTrace(captureRawStack());
- data.boxedStack = parentStep?.boxedStack;
- if (!data.boxedStack && data.box) {
- data.boxedStack = filteredStack.slice(1);
- data.location = data.location || data.boxedStack[0];
+ let boxedStack = parentStep?.boxedStack;
+ let location = data.location;
+ if (!boxedStack && data.box) {
+ boxedStack = filteredStack.slice(1);
+ location = location || boxedStack[0];
}
- data.location = data.location || filteredStack[0];
+ location = location || filteredStack[0];
+ const hidden = data.hidden || parentStep?.hidden;
- const attachmentIndices: number[] = [];
const step: TestStepInternal = {
- stepId,
...data,
+ stepId,
+ hidden,
+ boxedStack,
+ location,
steps: [],
- attachmentIndices,
+ attachmentIndices: [],
info: new TestStepInfoImpl(this, stepId, data.title, parentStep?.info),
complete: result => {
if (step.endWallTime)
@@ -296,8 +304,8 @@ export class TestInfoImpl implements TestInfo {
if (typeof result.error === 'object' && !(result.error as any)?.[stepSymbol])
(result.error as any)[stepSymbol] = step;
const error = testInfoError(result.error);
- if (data.boxedStack)
- error.stack = `${error.message}\n${stringifyStackFrames(data.boxedStack).join('\n')}`;
+ if (step.boxedStack)
+ error.stack = `${error.message}\n${stringifyStackFrames(step.boxedStack).join('\n')}`;
step.error = error;
}
@@ -314,39 +322,45 @@ export class TestInfoImpl implements TestInfo {
}
}
- const payload: StepEndPayload = {
- testId: this.testId,
- stepId,
- wallTime: step.endWallTime,
- error: step.error,
- suggestedRebaseline: result.suggestedRebaseline,
- annotations: step.info.annotations,
- };
- this._onStepEnd(payload);
- const errorForTrace = step.error ? { name: '', message: step.error.message || '', stack: step.error.stack } : undefined;
- const attachments = attachmentIndices.map(i => this.attachments[i]);
- this._tracing.appendAfterActionForStep(stepId, errorForTrace, attachments, step.info.annotations);
+ if (!step.hidden) {
+ const payload: StepEndPayload = {
+ testId: this.testId,
+ stepId,
+ wallTime: step.endWallTime,
+ error: step.error,
+ suggestedRebaseline: result.suggestedRebaseline,
+ annotations: step.info.annotations,
+ };
+ this._onStepEnd(payload);
+ const errorForTrace = step.error ? { name: '', message: step.error.message || '', stack: step.error.stack } : undefined;
+ const attachments = step.attachmentIndices.map(i => this.attachments[i]);
+ this._tracing.appendAfterActionForStep(stepId, errorForTrace, attachments, step.info.annotations);
+ }
}
};
const parentStepList = parentStep ? parentStep.steps : this._steps;
parentStepList.push(step);
this._stepMap.set(stepId, step);
- const payload: StepBeginPayload = {
- testId: this.testId,
- stepId,
- parentStepId: parentStep ? parentStep.stepId : undefined,
- title: data.title,
- category: data.category,
- wallTime: Date.now(),
- location: data.location,
- };
- this._onStepBegin(payload);
- this._tracing.appendBeforeActionForStep(stepId, parentStep?.stepId, {
- title: data.title,
- category: data.category,
- params: data.params,
- stack: data.location ? [data.location] : []
- });
+
+ if (!step.hidden) {
+ const payload: StepBeginPayload = {
+ testId: this.testId,
+ stepId,
+ parentStepId: parentStep ? parentStep.stepId : undefined,
+ title: step.title,
+ category: step.category,
+ wallTime: Date.now(),
+ location: step.location,
+ };
+ this._onStepBegin(payload);
+ this._tracing.appendBeforeActionForStep(stepId, parentStep?.stepId, {
+ title: step.title,
+ category: step.category,
+ params: step.params,
+ stack: step.location ? [step.location] : []
+ });
+ }
+
return step;
}
@@ -370,7 +384,7 @@ export class TestInfoImpl implements TestInfo {
this._tracing.appendForError(serialized);
}
- async _runAsStep(stepInfo: { title: string, category: 'hook' | 'fixture', location?: Location }, cb: () => Promise<any>) {
+ async _runAsStep(stepInfo: { title: string, category: 'hook' | 'fixture', location?: Location, hidden?: boolean }, cb: () => Promise<any>) {
const step = this._addStep(stepInfo);
try {
await cb();
diff --git a/tests/playwright-test/test-step.spec.ts b/tests/playwright-test/test-step.spec.ts
index f477a77057..3949bf1527 100644
--- a/tests/playwright-test/test-step.spec.ts
+++ b/tests/playwright-test/test-step.spec.ts
@@ -1769,3 +1769,59 @@ fixture | context
pw:api | Close context
`);
});
+
+test('should box fixtures with everything inside them', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'reporter.ts': stepIndentReporter,
+ 'playwright.config.ts': `module.exports = { reporter: './reporter' };`,
+ 'a.test.ts': `
+ import { test as baseTest, expect, type Page } from '@playwright/test';
+
+ const test = baseTest.extend<{ foo: number, bar: number }>({
+ foo: [async ({ page }, use) => {
+ await page.setContent('<div>here we go</div>');
+ await test.step('inner step', async () => {
+ await page.goto('data:text/html,<div>here we go</div>');
+ });
+ await use(1);
+ }, { box: true }],
+ bar: [async ({ page }, use) => {
+ await page.setContent('<div>here we go</div>');
+ await test.step('inner step', async () => {
+ await page.goto('data:text/html,<div>here we go</div>');
+ });
+ await use(2);
+ }, { box: false }],
+ });
+
+ test('test', async ({ foo, bar, page }) => {
+ await expect(page.locator('body')).toBeVisible();
+ expect(foo).toBe(1);
+ expect(bar).toBe(2);
+ });
+ `
+ }, { reporter: '' });
+
+ expect(result.exitCode).toBe(0);
+ expect(stripAnsi(result.output)).toBe(`
+hook |Before Hooks
+fixture | browser
+pw:api | Launch browser
+fixture | context
+pw:api | Create context
+fixture | page
+pw:api | Create page
+fixture | bar @ a.test.ts:4
+pw:api | Set content @ a.test.ts:13
+test.step | inner step @ a.test.ts:14
+pw:api | Navigate to "data:" @ a.test.ts:15
+expect |toBeVisible @ a.test.ts:22
+expect |toBe @ a.test.ts:23
+expect |toBe @ a.test.ts:24
+hook |After Hooks
+fixture | bar @ a.test.ts:4
+fixture | page
+fixture | context
+pw:api | Close context
+`);
+});