Skip to content

Commit

Permalink
Fix step context for native playwright steps (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw authored Feb 13, 2025
1 parent 3c94a62 commit e020a92
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 36 deletions.
18 changes: 16 additions & 2 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
2 changes: 1 addition & 1 deletion packages/allure-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"eslint-plugin-n": "^17.10.1",
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-prefer-arrow": "^1.2.3",
"glob": "^11.0.0",
"glob": "^11.0.1",
"mocha": "^11.0.0",
"npm-run-all2": "^7.0.0",
"rimraf": "^6.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/allure-playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"eslint-plugin-n": "^17.10.1",
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-prefer-arrow": "^1.2.3",
"glob": "^11.0.1",
"npm-run-all2": "^7.0.0",
"rimraf": "^6.0.0",
"typescript": "^5.2.2",
Expand Down
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 } 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
82 changes: 72 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,24 +117,78 @@ 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",
});
});

it("should use native playwright steps under the hood", async () => {
const { tests, restFiles } = await runPlaywrightInlineTest({
"playwright.config.js": `
module.exports = {
reporter: [
[
"allure-playwright",
{
resultsDir: "./allure-results",
},
],
["dot"],
["json", { outputFile: "./test-results.json" }],
],
projects: [
{
name: "project",
},
],
};
`,
"sample.test.ts": `
import { allure, test } from "allure-playwright";
test("steps", async () => {
await allure.step("step 1", async () => {});
});
`,
});

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[2]).toMatchObject({
name: "After Hooks",
});
expect(restFiles["test-results.json"]).toBeDefined();

const pwTestResults = JSON.parse(restFiles["test-results.json"]);

expect(pwTestResults.suites[0].specs[0].tests[0].results[0].steps[0]).toMatchObject({
title: "step 1",
});
});
Loading

0 comments on commit e020a92

Please sign in to comment.