Skip to content

Commit a884422

Browse files
authored
fix step context metadata for Playwright (fixes #1241, via #1251)
1 parent 9f50c6a commit a884422

File tree

4 files changed

+105
-28
lines changed

4 files changed

+105
-28
lines changed

packages/allure-playwright/src/index.ts

+12-24
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ export class AllureReporter implements ReporterV2 {
364364
const testUuid = this.allureResultsUuids.get(test.id)!;
365365
const isRootBeforeHook = step.title === BEFORE_HOOKS_ROOT_STEP_TITLE;
366366
const isRootAfterHook = step.title === AFTER_HOOKS_ROOT_STEP_TITLE;
367-
const isRootHook = isRootBeforeHook || isRootAfterHook;
368367
const isBeforeHookDescendant = isBeforeHookStep(step);
369368
const isAfterHookDescendant = isAfterHookStep(step);
370369
const isAfterHook = isRootAfterHook || isAfterHookDescendant;
@@ -386,29 +385,6 @@ export class AllureReporter implements ReporterV2 {
386385
stack.stopStep({
387386
duration: step.duration,
388387
});
389-
}
390-
391-
if (isRootHook) {
392-
const stack = isRootAfterHook
393-
? this.afterHooksStepsStack.get(test.id)!
394-
: this.beforeHooksStepsStack.get(test.id)!;
395-
396-
this.allureRuntime?.updateTest(testUuid, (testResult) => {
397-
if (isRootAfterHook) {
398-
testResult.steps.push(...stack.steps);
399-
} else {
400-
testResult.steps.unshift(...stack.steps);
401-
}
402-
});
403-
404-
if (isRootAfterHook) {
405-
this.afterHooksStepsStack.delete(test.id);
406-
} else {
407-
this.beforeHooksStepsStack.delete(test.id);
408-
}
409-
}
410-
411-
if (isHook) {
412388
return;
413389
}
414390

@@ -439,6 +415,8 @@ export class AllureReporter implements ReporterV2 {
439415
const error = result.error;
440416
// only apply default suites if not set by user
441417
const [, projectSuiteTitle, fileSuiteTitle, ...suiteTitles] = test.parent.titlePath();
418+
const beforeHooksStack = this.beforeHooksStepsStack.get(test.id);
419+
const afterHooksStack = this.afterHooksStepsStack.get(test.id);
442420

443421
this.allureRuntime!.updateTest(testUuid, (testResult) => {
444422
testResult.labels.push(getHostLabel());
@@ -531,6 +509,16 @@ export class AllureReporter implements ReporterV2 {
531509
return labelsGroup;
532510
});
533511

512+
if (beforeHooksStack) {
513+
testResult.steps.unshift(...beforeHooksStack.steps);
514+
this.beforeHooksStepsStack.delete(test.id);
515+
}
516+
517+
if (afterHooksStack) {
518+
testResult.steps.push(...afterHooksStack.steps);
519+
this.afterHooksStepsStack.delete(test.id);
520+
}
521+
534522
testResult.labels = newLabels;
535523
});
536524

packages/allure-playwright/src/runtime.ts

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ export class AllurePlaywrightTestRuntime extends MessageTestRuntime {
99
super();
1010
}
1111

12-
async step<T = void>(name: string, body: () => T | PromiseLike<T>) {
13-
return await test.step(name, () => Promise.resolve(body()));
14-
}
15-
1612
async attachment(name: string, content: Buffer | string, options: AttachmentOptions) {
1713
await test.info().attach(name, { body: content, contentType: options.contentType });
1814
}

packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts

+46
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,49 @@ it("handles nested lambda steps", async () => {
8484
stage: Stage.FINISHED,
8585
});
8686
});
87+
88+
it("should allow to set step metadata through its context", async () => {
89+
const { tests } = await runPlaywrightInlineTest({
90+
"sample.test.ts": `
91+
import { test, allure } from "allure-playwright";
92+
93+
test("steps", async () => {
94+
await allure.step("step 1", async () => {
95+
await allure.step("step 2", async () => {
96+
await allure.step("step 3", async (stepContext) => {
97+
await stepContext.displayName("custom name");
98+
await stepContext.parameter("param", "value");
99+
});
100+
});
101+
});
102+
});
103+
`,
104+
});
105+
106+
expect(tests).toHaveLength(1);
107+
expect(tests[0].steps).toHaveLength(3);
108+
expect(tests[0].steps[0]).toMatchObject({
109+
name: "Before Hooks",
110+
});
111+
expect(tests[0].steps[1]).toMatchObject({
112+
name: "step 1",
113+
status: Status.PASSED,
114+
stage: Stage.FINISHED,
115+
});
116+
expect(tests[0].steps[1].steps).toHaveLength(1);
117+
expect(tests[0].steps[1].steps[0]).toMatchObject({
118+
name: "step 2",
119+
status: Status.PASSED,
120+
stage: Stage.FINISHED,
121+
});
122+
expect(tests[0].steps[1].steps[0].steps).toHaveLength(1);
123+
expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({
124+
name: "custom name",
125+
parameters: [expect.objectContaining({ name: "param", value: "value" })],
126+
status: Status.PASSED,
127+
stage: Stage.FINISHED,
128+
});
129+
expect(tests[0].steps[2]).toMatchObject({
130+
name: "After Hooks",
131+
});
132+
});

packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,50 @@ it("should support log steps", async () => {
110110
]),
111111
);
112112
});
113+
114+
it("should allow to set step metadata through its context", async () => {
115+
const { tests } = await runPlaywrightInlineTest({
116+
"sample.test.ts": `
117+
import { test } from "allure-playwright";
118+
import { step } from "allure-js-commons";
119+
120+
test("steps", async () => {
121+
await step("step 1", async () => {
122+
await step("step 2", async () => {
123+
await step("step 3", async (stepContext) => {
124+
await stepContext.displayName("custom name");
125+
await stepContext.parameter("param", "value");
126+
});
127+
});
128+
});
129+
});
130+
`,
131+
});
132+
133+
expect(tests).toHaveLength(1);
134+
expect(tests[0].steps).toHaveLength(3);
135+
expect(tests[0].steps[0]).toMatchObject({
136+
name: "Before Hooks",
137+
});
138+
expect(tests[0].steps[1]).toMatchObject({
139+
name: "step 1",
140+
status: Status.PASSED,
141+
stage: Stage.FINISHED,
142+
});
143+
expect(tests[0].steps[1].steps).toHaveLength(1);
144+
expect(tests[0].steps[1].steps[0]).toMatchObject({
145+
name: "step 2",
146+
status: Status.PASSED,
147+
stage: Stage.FINISHED,
148+
});
149+
expect(tests[0].steps[1].steps[0].steps).toHaveLength(1);
150+
expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({
151+
name: "custom name",
152+
parameters: [expect.objectContaining({ name: "param", value: "value" })],
153+
status: Status.PASSED,
154+
stage: Stage.FINISHED,
155+
});
156+
expect(tests[0].steps[2]).toMatchObject({
157+
name: "After Hooks",
158+
});
159+
});

0 commit comments

Comments
 (0)