From 5cb10053ae73d5f3387e6feea386fcda40ece3db Mon Sep 17 00:00:00 2001 From: epszaw Date: Tue, 11 Feb 2025 09:36:08 +0100 Subject: [PATCH] fix step contexts in playwright --- packages/allure-playwright/src/index.ts | 36 +++++--------- packages/allure-playwright/src/runtime.ts | 4 -- .../test/spec/runtime/legacy/steps.spec.ts | 46 ++++++++++++++++++ .../test/spec/runtime/modern/steps.spec.ts | 47 +++++++++++++++++++ 4 files changed, 105 insertions(+), 28 deletions(-) diff --git a/packages/allure-playwright/src/index.ts b/packages/allure-playwright/src/index.ts index f4c6d99b3..30fc211d2 100644 --- a/packages/allure-playwright/src/index.ts +++ b/packages/allure-playwright/src/index.ts @@ -364,7 +364,6 @@ export class AllureReporter implements ReporterV2 { const testUuid = this.allureResultsUuids.get(test.id)!; const isRootBeforeHook = step.title === BEFORE_HOOKS_ROOT_STEP_TITLE; const isRootAfterHook = step.title === AFTER_HOOKS_ROOT_STEP_TITLE; - const isRootHook = isRootBeforeHook || isRootAfterHook; const isBeforeHookDescendant = isBeforeHookStep(step); const isAfterHookDescendant = isAfterHookStep(step); const isAfterHook = isRootAfterHook || isAfterHookDescendant; @@ -386,29 +385,6 @@ export class AllureReporter implements ReporterV2 { stack.stopStep({ duration: step.duration, }); - } - - if (isRootHook) { - const stack = isRootAfterHook - ? this.afterHooksStepsStack.get(test.id)! - : this.beforeHooksStepsStack.get(test.id)!; - - this.allureRuntime?.updateTest(testUuid, (testResult) => { - if (isRootAfterHook) { - testResult.steps.push(...stack.steps); - } else { - testResult.steps.unshift(...stack.steps); - } - }); - - if (isRootAfterHook) { - this.afterHooksStepsStack.delete(test.id); - } else { - this.beforeHooksStepsStack.delete(test.id); - } - } - - if (isHook) { return; } @@ -439,6 +415,8 @@ export class AllureReporter implements ReporterV2 { const error = result.error; // only apply default suites if not set by user const [, projectSuiteTitle, fileSuiteTitle, ...suiteTitles] = test.parent.titlePath(); + const beforeHooksStack = this.beforeHooksStepsStack.get(test.id); + const afterHooksStack = this.afterHooksStepsStack.get(test.id); this.allureRuntime!.updateTest(testUuid, (testResult) => { testResult.labels.push(getHostLabel()); @@ -531,6 +509,16 @@ export class AllureReporter implements ReporterV2 { return labelsGroup; }); + if (beforeHooksStack) { + testResult.steps.unshift(...beforeHooksStack.steps); + this.beforeHooksStepsStack.delete(test.id); + } + + if (afterHooksStack) { + testResult.steps.push(...afterHooksStack.steps); + this.afterHooksStepsStack.delete(test.id); + } + testResult.labels = newLabels; }); diff --git a/packages/allure-playwright/src/runtime.ts b/packages/allure-playwright/src/runtime.ts index 4d4f3859c..7612581ef 100644 --- a/packages/allure-playwright/src/runtime.ts +++ b/packages/allure-playwright/src/runtime.ts @@ -9,10 +9,6 @@ export class AllurePlaywrightTestRuntime extends MessageTestRuntime { super(); } - async step(name: string, body: () => T | PromiseLike) { - return await test.step(name, () => Promise.resolve(body())); - } - async attachment(name: string, content: Buffer | string, options: AttachmentOptions) { await test.info().attach(name, { body: content, contentType: options.contentType }); } diff --git a/packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts b/packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts index d066e94fa..0f9b8128a 100644 --- a/packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts +++ b/packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts @@ -84,3 +84,49 @@ it("handles nested lambda steps", async () => { stage: Stage.FINISHED, }); }); + +it("should allow to set step metadata through its context", async () => { + const { tests } = await runPlaywrightInlineTest({ + "sample.test.ts": ` + import { test, allure } from "allure-playwright"; + + test("steps", async () => { + await allure.step("step 1", async () => { + await allure.step("step 2", async () => { + await allure.step("step 3", async (stepContext) => { + await stepContext.displayName("custom name"); + await stepContext.parameter("param", "value"); + }); + }); + }); + }); + `, + }); + + expect(tests).toHaveLength(1); + expect(tests[0].steps).toHaveLength(3); + expect(tests[0].steps[0]).toMatchObject({ + name: "Before Hooks", + }); + expect(tests[0].steps[1]).toMatchObject({ + name: "step 1", + status: Status.PASSED, + stage: Stage.FINISHED, + }); + expect(tests[0].steps[1].steps).toHaveLength(1); + expect(tests[0].steps[1].steps[0]).toMatchObject({ + name: "step 2", + status: Status.PASSED, + stage: Stage.FINISHED, + }); + expect(tests[0].steps[1].steps[0].steps).toHaveLength(1); + expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({ + name: "custom name", + parameters: [expect.objectContaining({ name: "param", value: "value" })], + status: Status.PASSED, + stage: Stage.FINISHED, + }); + expect(tests[0].steps[2]).toMatchObject({ + name: "After Hooks", + }); +}); diff --git a/packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts b/packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts index 9c064abf3..559e41db8 100644 --- a/packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts +++ b/packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts @@ -110,3 +110,50 @@ it("should support log steps", async () => { ]), ); }); + +it("should allow to set step metadata through its context", async () => { + const { tests } = await runPlaywrightInlineTest({ + "sample.test.ts": ` + import { test } from "allure-playwright"; + import { step } from "allure-js-commons"; + + test("steps", async () => { + await step("step 1", async () => { + await step("step 2", async () => { + await step("step 3", async (stepContext) => { + await stepContext.displayName("custom name"); + await stepContext.parameter("param", "value"); + }); + }); + }); + }); + `, + }); + + expect(tests).toHaveLength(1); + expect(tests[0].steps).toHaveLength(3); + expect(tests[0].steps[0]).toMatchObject({ + name: "Before Hooks", + }); + expect(tests[0].steps[1]).toMatchObject({ + name: "step 1", + status: Status.PASSED, + stage: Stage.FINISHED, + }); + expect(tests[0].steps[1].steps).toHaveLength(1); + expect(tests[0].steps[1].steps[0]).toMatchObject({ + name: "step 2", + status: Status.PASSED, + stage: Stage.FINISHED, + }); + expect(tests[0].steps[1].steps[0].steps).toHaveLength(1); + expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({ + name: "custom name", + parameters: [expect.objectContaining({ name: "param", value: "value" })], + status: Status.PASSED, + stage: Stage.FINISHED, + }); + expect(tests[0].steps[2]).toMatchObject({ + name: "After Hooks", + }); +});