Skip to content

Commit 92758b7

Browse files
authored
added playwright trace hook attachments to test results (#1456)
1 parent 4abd3cb commit 92758b7

7 files changed

Lines changed: 112 additions & 3 deletions

File tree

packages/allure-cucumberjs/src/reporter.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,18 @@ export default class AllureCucumberReporter extends Formatter {
516516
}
517517

518518
if (message.mediaType === ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE) {
519+
if (fixtureUuid && testUuid) {
520+
const runtimeMessages = this.parseRuntimeMessages(message.body);
521+
const hasPlaywrightTrace = runtimeMessages.some(
522+
(msg) =>
523+
(msg.type === "attachment_content" || msg.type === "attachment_path") &&
524+
msg.data.contentType === ContentType.PLAYWRIGHT_TRACE,
525+
);
526+
if (hasPlaywrightTrace) {
527+
this.applyRuntimeAttachmentMessages(testUuid, message.body);
528+
return;
529+
}
530+
}
519531
this.applyRuntimeAttachmentMessages(rootUuid, message.body);
520532
return;
521533
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Hook Attachments
2+
3+
@trace
4+
Scenario: trace attachment in after hook
5+
Given a passed step
6+
7+
@regular
8+
Scenario: regular attachment in after hook
9+
Given a passed step
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { Given, After } = require("@cucumber/cucumber");
2+
const { attachment, ContentType } = require("allure-js-commons");
3+
4+
After("@trace", async () => {
5+
await attachment("Playwright Trace", "trace-content", {
6+
contentType: ContentType.PLAYWRIGHT_TRACE,
7+
});
8+
});
9+
10+
After("@regular", async () => {
11+
await attachment("Text attachment", "some text", {
12+
contentType: "text/plain",
13+
});
14+
});
15+
16+
Given("a passed step", () => {});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { ContentType } from "allure-js-commons";
2+
import { expect, it } from "vitest";
3+
4+
import { runCucumberInlineTest } from "../utils.js";
5+
6+
it("handles playwright trace attachments in after hooks", async () => {
7+
const { tests, groups, attachments } = await runCucumberInlineTest(["hookAttachments"], ["hookAttachments"]);
8+
9+
expect(tests).toHaveLength(2);
10+
11+
const traceTest = tests.find((t) => t.name === "trace attachment in after hook");
12+
const regularTest = tests.find((t) => t.name === "regular attachment in after hook");
13+
14+
expect(traceTest).toBeDefined();
15+
expect(regularTest).toBeDefined();
16+
17+
// playwright trace attachment should be in the test result
18+
expect(traceTest!.steps).toEqual(
19+
expect.arrayContaining([
20+
expect.objectContaining({
21+
name: "Given a passed step",
22+
status: "passed",
23+
}),
24+
expect.objectContaining({
25+
name: "Playwright Trace",
26+
attachments: expect.arrayContaining([
27+
expect.objectContaining({
28+
name: "Playwright Trace",
29+
type: ContentType.PLAYWRIGHT_TRACE,
30+
}),
31+
]),
32+
}),
33+
]),
34+
);
35+
36+
// regular attachment should not be in the test result
37+
expect(regularTest!.steps).toEqual(
38+
expect.arrayContaining([
39+
expect.objectContaining({
40+
name: "Given a passed step",
41+
status: "passed",
42+
}),
43+
]),
44+
);
45+
expect(regularTest!.steps!.find((s) => s.name === "Text attachment")).toBeUndefined();
46+
expect(regularTest!.attachments).toHaveLength(0);
47+
48+
// regular attachment should be in the after fixture of the group
49+
expect(groups).toHaveLength(2);
50+
const regularGroup = groups.find((g) => g.afters?.some((a) => a.steps?.some((s) => s.name === "Text attachment")));
51+
expect(regularGroup).toBeDefined();
52+
expect(regularGroup!.afters).toEqual(
53+
expect.arrayContaining([
54+
expect.objectContaining({
55+
steps: expect.arrayContaining([
56+
expect.objectContaining({
57+
name: "Text attachment",
58+
attachments: expect.arrayContaining([
59+
expect.objectContaining({
60+
name: "Text attachment",
61+
type: "text/plain",
62+
}),
63+
]),
64+
}),
65+
]),
66+
}),
67+
]),
68+
);
69+
70+
expect(Object.keys(attachments as object)).toHaveLength(2);
71+
});

packages/allure-js-commons/src/facade.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Status, StatusDetails } from "./model.js";
2-
import { type ContentType } from "./model.js";
2+
import { ContentType } from "./model.js";
33
import { type AttachmentOptions, type Label, type Link, type ParameterMode, type ParameterOptions } from "./model.js";
44
import { LabelName, LinkType } from "./model.js";
55
import { getGlobalTestRuntimeWithAutoconfig } from "./sdk/runtime/runtime.js";
@@ -100,7 +100,7 @@ export const globalError = (details: StatusDetails) => {
100100

101101
export const attachTrace = (name: string, path: string) => {
102102
return callRuntimeMethod("attachmentFromPath", name, path, {
103-
contentType: "application/vnd.allure.playwright-trace",
103+
contentType: ContentType.PLAYWRIGHT_TRACE,
104104
});
105105
};
106106

packages/allure-js-commons/src/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export enum ContentType {
168168
JPEG = "image/jpeg",
169169
MP4 = "video/mp4",
170170
IMAGEDIFF = "application/vnd.allure.image.diff",
171+
PLAYWRIGHT_TRACE = "application/vnd.allure.playwright-trace",
171172
}
172173

173174
/* eslint-disable no-shadow */

packages/allure-playwright/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ export class AllureReporter implements ReporterV2 {
768768
} else {
769769
const contentType =
770770
attachment.name === "trace" && attachment.contentType === "application/zip"
771-
? "application/vnd.allure.playwright-trace"
771+
? ContentType.PLAYWRIGHT_TRACE
772772
: attachment.contentType;
773773

774774
this.allureRuntime!.writeAttachment(testUuid, parentUuid, attachment.name, attachment.path!, {

0 commit comments

Comments
 (0)