Skip to content

Commit

Permalink
add ability to use step context in the native pw steps
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Feb 13, 2025
1 parent 3c94a62 commit f7debd9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 23 deletions.
17 changes: 16 additions & 1 deletion packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
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 @@ -585,18 +586,32 @@ 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") {
const { name, parameters = [] } = message.data;

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

step.parameters.push(...parameters);
});
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

0 comments on commit f7debd9

Please sign in to comment.