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 for native playwright steps #1255

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
type StepResult,
type TestResult,
} from "allure-js-commons";
import type { RuntimeMessage, TestPlanV1Test } from "allure-js-commons/sdk";
import type { RuntimeMessage, RuntimeStepMetadataMessage, TestPlanV1Test } from "allure-js-commons/sdk";
import {
extractMetadataFromString,
getMessageAndTraceFromError,
Expand Down Expand Up @@ -306,6 +306,7 @@ export class AllureReporter implements ReporterV2 {

if (step.category === "attach") {
const currentStep = this.allureRuntime?.currentStep(testUuid);

this.attachmentSteps.set(testUuid, [...(this.attachmentSteps.get(testUuid) ?? []), currentStep]);
return;
}
Expand Down Expand Up @@ -562,6 +563,18 @@ export class AllureReporter implements ReporterV2 {
return false;
}

private processStepMetadataMessage(attachmentStepUuid: string, message: RuntimeStepMetadataMessage) {
const { name, parameters = [] } = message.data;

this.allureRuntime!.updateStep(attachmentStepUuid, (step) => {
if (name) {
step.name = name;
}

step.parameters.push(...parameters);
});
}

private async processAttachment(
testUuid: string,
attachmentStepUuid: string | undefined,
Expand All @@ -585,18 +598,24 @@ export class AllureReporter implements ReporterV2 {
if (allureRuntimeMessage) {
const message = JSON.parse(attachment.body!.toString()) as RuntimeMessage;

// TODO fix step metadata messages
if (message.type === "step_metadata") {
this.processStepMetadataMessage(attachmentStepUuid!, message);
return;
}

this.allureRuntime!.applyRuntimeMessages(testUuid, [message]);
return;
}

const parentUuid = this.allureRuntime!.startStep(testUuid, attachmentStepUuid, { name: attachment.name });

// only stop if step is created. Step may not be created only if test with specified uuid doesn't exists.
// usually, missing test by uuid means we should completely skip result processing;
// the later operations are safe and will only produce console warnings
if (parentUuid) {
this.allureRuntime!.stopStep(parentUuid, undefined);
}

if (attachment.body) {
this.allureRuntime!.writeAttachment(testUuid, parentUuid, attachment.name, attachment.body, {
contentType: attachment.contentType,
Expand Down
36 changes: 35 additions & 1 deletion packages/allure-playwright/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "@playwright/test";
import type { AttachmentOptions } from "allure-js-commons";
import type { AttachmentOptions, ParameterMode, StepContext } from "allure-js-commons";
import type { RuntimeMessage } from "allure-js-commons/sdk";
import { ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE } from "allure-js-commons/sdk/reporter";
import { MessageTestRuntime } from "allure-js-commons/sdk/runtime";
Expand All @@ -9,6 +9,40 @@ export class AllurePlaywrightTestRuntime extends MessageTestRuntime {
super();
}

async step(stepName: string, body: () => any) {
return await test.step(stepName, async () => await body());
}

async stepDisplayName(name: string) {
await test.info().attach("Allure Step Metadata", {
contentType: ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE,
body: Buffer.from(
JSON.stringify({
type: "step_metadata",
data: {
name,
},
}),
"utf8",
),
});
}

async stepParameter(name: string, value: string, mode?: ParameterMode) {
await test.info().attach("Allure Step Metadata", {
contentType: ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE,
body: Buffer.from(
JSON.stringify({
type: "step_metadata",
data: {
parameters: [{ name, value, mode }],
},
}),
"utf8",
),
});
}

async attachment(name: string, content: Buffer | string, options: AttachmentOptions) {
await test.info().attach(name, { body: content, contentType: options.contentType });
}
Expand Down
30 changes: 20 additions & 10 deletions packages/allure-playwright/test/spec/runtime/legacy/steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,24 @@ it("handles nested lambda steps", async () => {
it("should allow to set step metadata through its context", async () => {
const { tests } = await runPlaywrightInlineTest({
"sample.test.ts": `
import { test, allure } from "allure-playwright";
import { allure, test } 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");
await allure.step("step 1", async (ctx1) => {
await ctx1.displayName("custom name 1");

await allure.step("step 2", async (ctx2) => {
await ctx2.displayName("custom name 2");

await allure.step("step 3", async (ctx3) => {
await ctx3.displayName("custom name 3");
await ctx3.parameter("param", "value 3");
});

await ctx2.parameter("param", "value 2");
});

await ctx1.parameter("param", "value 1");
});
});
`,
Expand All @@ -109,22 +117,24 @@ it("should allow to set step metadata through its context", async () => {
name: "Before Hooks",
});
expect(tests[0].steps[1]).toMatchObject({
name: "step 1",
name: "custom name 1",
status: Status.PASSED,
stage: Stage.FINISHED,
parameters: [expect.objectContaining({ name: "param", value: "value 1" })],
});
expect(tests[0].steps[1].steps).toHaveLength(1);
expect(tests[0].steps[1].steps[0]).toMatchObject({
name: "step 2",
name: "custom name 2",
status: Status.PASSED,
stage: Stage.FINISHED,
parameters: [expect.objectContaining({ name: "param", value: "value 2" })],
});
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" })],
name: "custom name 3",
status: Status.PASSED,
stage: Stage.FINISHED,
parameters: [expect.objectContaining({ name: "param", value: "value 3" })],
});
expect(tests[0].steps[2]).toMatchObject({
name: "After Hooks",
Expand Down
32 changes: 21 additions & 11 deletions packages/allure-playwright/test/spec/runtime/modern/steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ it("handles single lambda step with attachment", async () => {
const { tests, attachments } = await runPlaywrightInlineTest({
"sample.test.ts": `
import { test } from '@playwright/test';
import { step, attachment } from "allure-js-commons";
import { attachment, step } from "allure-js-commons";

test("steps", async () => {
await step("step", async () => {
Expand Down Expand Up @@ -115,16 +115,24 @@ 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";
import { step, attachment } 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");
await step("step 1", async (ctx1) => {
await ctx1.displayName("custom name 1");

await step("step 2", async (ctx2) => {
await ctx2.displayName("custom name 2");

await step("step 3", async (ctx3) => {
await ctx3.displayName("custom name 3");
await ctx3.parameter("param", "value 3");
});

await ctx2.parameter("param", "value 2");
});

await ctx1.parameter("param", "value 1");
});
});
`,
Expand All @@ -136,22 +144,24 @@ it("should allow to set step metadata through its context", async () => {
name: "Before Hooks",
});
expect(tests[0].steps[1]).toMatchObject({
name: "step 1",
name: "custom name 1",
status: Status.PASSED,
stage: Stage.FINISHED,
parameters: [expect.objectContaining({ name: "param", value: "value 1" })],
});
expect(tests[0].steps[1].steps).toHaveLength(1);
expect(tests[0].steps[1].steps[0]).toMatchObject({
name: "step 2",
name: "custom name 2",
status: Status.PASSED,
stage: Stage.FINISHED,
parameters: [expect.objectContaining({ name: "param", value: "value 2" })],
});
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" })],
name: "custom name 3",
status: Status.PASSED,
stage: Stage.FINISHED,
parameters: [expect.objectContaining({ name: "param", value: "value 3" })],
});
expect(tests[0].steps[2]).toMatchObject({
name: "After Hooks",
Expand Down
Loading