Skip to content

Commit f7debd9

Browse files
committed
add ability to use step context in the native pw steps
1 parent 3c94a62 commit f7debd9

File tree

4 files changed

+92
-23
lines changed

4 files changed

+92
-23
lines changed

packages/allure-playwright/src/index.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ export class AllureReporter implements ReporterV2 {
306306

307307
if (step.category === "attach") {
308308
const currentStep = this.allureRuntime?.currentStep(testUuid);
309+
309310
this.attachmentSteps.set(testUuid, [...(this.attachmentSteps.get(testUuid) ?? []), currentStep]);
310311
return;
311312
}
@@ -585,18 +586,32 @@ export class AllureReporter implements ReporterV2 {
585586
if (allureRuntimeMessage) {
586587
const message = JSON.parse(attachment.body!.toString()) as RuntimeMessage;
587588

588-
// TODO fix step metadata messages
589+
if (message.type === "step_metadata") {
590+
const { name, parameters = [] } = message.data;
591+
592+
this.allureRuntime!.updateStep(attachmentStepUuid!, (step) => {
593+
if (name) {
594+
step.name = name;
595+
}
596+
597+
step.parameters.push(...parameters);
598+
});
599+
return;
600+
}
601+
589602
this.allureRuntime!.applyRuntimeMessages(testUuid, [message]);
590603
return;
591604
}
592605

593606
const parentUuid = this.allureRuntime!.startStep(testUuid, attachmentStepUuid, { name: attachment.name });
607+
594608
// only stop if step is created. Step may not be created only if test with specified uuid doesn't exists.
595609
// usually, missing test by uuid means we should completely skip result processing;
596610
// the later operations are safe and will only produce console warnings
597611
if (parentUuid) {
598612
this.allureRuntime!.stopStep(parentUuid, undefined);
599613
}
614+
600615
if (attachment.body) {
601616
this.allureRuntime!.writeAttachment(testUuid, parentUuid, attachment.name, attachment.body, {
602617
contentType: attachment.contentType,

packages/allure-playwright/src/runtime.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from "@playwright/test";
2-
import type { AttachmentOptions } from "allure-js-commons";
2+
import type { AttachmentOptions, ParameterMode, StepContext } from "allure-js-commons";
33
import type { RuntimeMessage } from "allure-js-commons/sdk";
44
import { ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE } from "allure-js-commons/sdk/reporter";
55
import { MessageTestRuntime } from "allure-js-commons/sdk/runtime";
@@ -9,6 +9,40 @@ export class AllurePlaywrightTestRuntime extends MessageTestRuntime {
99
super();
1010
}
1111

12+
async step(stepName: string, body: () => any) {
13+
return await test.step(stepName, async () => await body());
14+
}
15+
16+
async stepDisplayName(name: string) {
17+
await test.info().attach("Allure Step Metadata", {
18+
contentType: ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE,
19+
body: Buffer.from(
20+
JSON.stringify({
21+
type: "step_metadata",
22+
data: {
23+
name,
24+
},
25+
}),
26+
"utf8",
27+
),
28+
});
29+
}
30+
31+
async stepParameter(name: string, value: string, mode?: ParameterMode) {
32+
await test.info().attach("Allure Step Metadata", {
33+
contentType: ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE,
34+
body: Buffer.from(
35+
JSON.stringify({
36+
type: "step_metadata",
37+
data: {
38+
parameters: [{ name, value, mode }],
39+
},
40+
}),
41+
"utf8",
42+
),
43+
});
44+
}
45+
1246
async attachment(name: string, content: Buffer | string, options: AttachmentOptions) {
1347
await test.info().attach(name, { body: content, contentType: options.contentType });
1448
}

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

+20-10
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,24 @@ it("handles nested lambda steps", async () => {
8888
it("should allow to set step metadata through its context", async () => {
8989
const { tests } = await runPlaywrightInlineTest({
9090
"sample.test.ts": `
91-
import { test, allure } from "allure-playwright";
91+
import { allure, test } from "allure-playwright";
9292
9393
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");
94+
await allure.step("step 1", async (ctx1) => {
95+
await ctx1.displayName("custom name 1");
96+
97+
await allure.step("step 2", async (ctx2) => {
98+
await ctx2.displayName("custom name 2");
99+
100+
await allure.step("step 3", async (ctx3) => {
101+
await ctx3.displayName("custom name 3");
102+
await ctx3.parameter("param", "value 3");
99103
});
104+
105+
await ctx2.parameter("param", "value 2");
100106
});
107+
108+
await ctx1.parameter("param", "value 1");
101109
});
102110
});
103111
`,
@@ -109,22 +117,24 @@ it("should allow to set step metadata through its context", async () => {
109117
name: "Before Hooks",
110118
});
111119
expect(tests[0].steps[1]).toMatchObject({
112-
name: "step 1",
120+
name: "custom name 1",
113121
status: Status.PASSED,
114122
stage: Stage.FINISHED,
123+
parameters: [expect.objectContaining({ name: "param", value: "value 1" })],
115124
});
116125
expect(tests[0].steps[1].steps).toHaveLength(1);
117126
expect(tests[0].steps[1].steps[0]).toMatchObject({
118-
name: "step 2",
127+
name: "custom name 2",
119128
status: Status.PASSED,
120129
stage: Stage.FINISHED,
130+
parameters: [expect.objectContaining({ name: "param", value: "value 2" })],
121131
});
122132
expect(tests[0].steps[1].steps[0].steps).toHaveLength(1);
123133
expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({
124-
name: "custom name",
125-
parameters: [expect.objectContaining({ name: "param", value: "value" })],
134+
name: "custom name 3",
126135
status: Status.PASSED,
127136
stage: Stage.FINISHED,
137+
parameters: [expect.objectContaining({ name: "param", value: "value 3" })],
128138
});
129139
expect(tests[0].steps[2]).toMatchObject({
130140
name: "After Hooks",

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

+21-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ it("handles single lambda step with attachment", async () => {
2828
const { tests, attachments } = await runPlaywrightInlineTest({
2929
"sample.test.ts": `
3030
import { test } from '@playwright/test';
31-
import { step, attachment } from "allure-js-commons";
31+
import { attachment, step } from "allure-js-commons";
3232
3333
test("steps", async () => {
3434
await step("step", async () => {
@@ -115,16 +115,24 @@ it("should allow to set step metadata through its context", async () => {
115115
const { tests } = await runPlaywrightInlineTest({
116116
"sample.test.ts": `
117117
import { test } from "allure-playwright";
118-
import { step } from "allure-js-commons";
118+
import { step, attachment } from "allure-js-commons";
119119
120120
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");
121+
await step("step 1", async (ctx1) => {
122+
await ctx1.displayName("custom name 1");
123+
124+
await step("step 2", async (ctx2) => {
125+
await ctx2.displayName("custom name 2");
126+
127+
await step("step 3", async (ctx3) => {
128+
await ctx3.displayName("custom name 3");
129+
await ctx3.parameter("param", "value 3");
126130
});
131+
132+
await ctx2.parameter("param", "value 2");
127133
});
134+
135+
await ctx1.parameter("param", "value 1");
128136
});
129137
});
130138
`,
@@ -136,22 +144,24 @@ it("should allow to set step metadata through its context", async () => {
136144
name: "Before Hooks",
137145
});
138146
expect(tests[0].steps[1]).toMatchObject({
139-
name: "step 1",
147+
name: "custom name 1",
140148
status: Status.PASSED,
141149
stage: Stage.FINISHED,
150+
parameters: [expect.objectContaining({ name: "param", value: "value 1" })],
142151
});
143152
expect(tests[0].steps[1].steps).toHaveLength(1);
144153
expect(tests[0].steps[1].steps[0]).toMatchObject({
145-
name: "step 2",
154+
name: "custom name 2",
146155
status: Status.PASSED,
147156
stage: Stage.FINISHED,
157+
parameters: [expect.objectContaining({ name: "param", value: "value 2" })],
148158
});
149159
expect(tests[0].steps[1].steps[0].steps).toHaveLength(1);
150160
expect(tests[0].steps[1].steps[0].steps[0]).toMatchObject({
151-
name: "custom name",
152-
parameters: [expect.objectContaining({ name: "param", value: "value" })],
161+
name: "custom name 3",
153162
status: Status.PASSED,
154163
stage: Stage.FINISHED,
164+
parameters: [expect.objectContaining({ name: "param", value: "value 3" })],
155165
});
156166
expect(tests[0].steps[2]).toMatchObject({
157167
name: "After Hooks",

0 commit comments

Comments
 (0)