Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix step context metadata for Playwright #1251

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
});

Expand Down
4 changes: 0 additions & 4 deletions packages/allure-playwright/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export class AllurePlaywrightTestRuntime extends MessageTestRuntime {
super();
}

async step<T = void>(name: string, body: () => T | PromiseLike<T>) {
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 });
}
Expand Down
46 changes: 46 additions & 0 deletions packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
47 changes: 47 additions & 0 deletions packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
Loading