Skip to content

Commit 8454625

Browse files
authored
playwright: fix attachments writing when fail (#1389)
1 parent 625fadc commit 8454625

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

packages/allure-playwright/src/index.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ import {
4545
readImageAsBase64,
4646
} from "allure-js-commons/sdk/reporter";
4747
import { allurePlaywrightLegacyApi } from "./legacy.js";
48-
import type { AllurePlaywrightReporterConfig, AttachStack, ReporterV2 } from "./model.js";
48+
import type { AllurePlaywrightReporterConfig, AttachStack, AttachmentTarget, ReporterV2 } from "./model.js";
4949
import {
5050
AFTER_HOOKS_ROOT_STEP_TITLE,
5151
BEFORE_HOOKS_ROOT_STEP_TITLE,
5252
diffEndRegexp,
5353
isAfterHookStep,
5454
isBeforeHookStep,
5555
isDescendantOfStepWithTitle,
56+
normalizeAttachStepTitle,
5657
normalizeHookTitle,
5758
statusToAllureStats,
5859
} from "./utils.js";
@@ -69,7 +70,7 @@ export class AllureReporter implements ReporterV2 {
6970
private processedDiffs: string[] = [];
7071
private readonly startedTestCasesTitlesCache: string[] = [];
7172
private readonly allureResultsUuids: Map<string, string> = new Map();
72-
private readonly attachmentTargets: Map<string, { stepUuid?: string; hookStep?: AttachStack }[]> = new Map();
73+
private readonly attachmentTargets: Map<string, AttachmentTarget[]> = new Map();
7374
private beforeHooksStepsStack: Map<string, ShallowStepsStack> = new Map();
7475
private afterHooksStepsStack: Map<string, ShallowStepsStack> = new Map();
7576
private beforeHooksAttachmentsStack: Map<string, AttachStack[]> = new Map();
@@ -301,10 +302,11 @@ export class AllureReporter implements ReporterV2 {
301302
if (["test.attach", "attach"].includes(step.category) && !isHookStep) {
302303
const parent = step.parent ? this.pwStepUuid.get(step.parent) ?? null : null;
303304
const targets = this.attachmentTargets.get(test.id) ?? [];
304-
targets.push({ stepUuid: parent ?? undefined });
305+
targets.push({ name: normalizeAttachStepTitle(step.title), stepUuid: parent ?? undefined });
305306
this.attachmentTargets.set(test.id, targets);
306307
return;
307308
}
309+
308310
if (isHookStep) {
309311
const stack = isBeforeHookDescendant
310312
? this.beforeHooksStepsStack.get(test.id)!
@@ -325,7 +327,7 @@ export class AllureReporter implements ReporterV2 {
325327
stack.stopStep();
326328

327329
const targets = this.attachmentTargets.get(test.id) ?? [];
328-
targets.push({ hookStep: hookStepWithUuid });
330+
targets.push({ name: normalizeAttachStepTitle(step.title), hookStep: hookStepWithUuid });
329331
this.attachmentTargets.set(test.id, targets);
330332
return;
331333
}
@@ -477,13 +479,27 @@ export class AllureReporter implements ReporterV2 {
477479
}
478480

479481
const attachmentsInBeforeHooks = this.beforeHooksAttachmentsStack.get(test.id) ?? [];
480-
const attachmentTargets = this.attachmentTargets.get(test.id) ?? [];
481482

482-
let targetIndex = 0;
483+
// FIFO
484+
const targets = this.attachmentTargets.get(test.id) ?? [];
485+
const targetsByName = new Map<string, AttachmentTarget[]>();
486+
for (const t of targets) {
487+
const q = targetsByName.get(t.name) ?? [];
488+
q.push(t);
489+
targetsByName.set(t.name, q);
490+
}
491+
492+
const takeByName = (name: string): AttachmentTarget | undefined => {
493+
const target = targetsByName.get(name);
494+
if (!target || target.length === 0) {
495+
return undefined;
496+
}
497+
return target.shift();
498+
};
483499

484500
for (const attachment of result.attachments) {
485501
const isRuntimeMessage = attachment.contentType === ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE;
486-
const stepInfo = targetIndex < attachmentTargets.length ? attachmentTargets[targetIndex++] : undefined;
502+
const stepInfo = takeByName(attachment.name);
487503

488504
if (isRuntimeMessage) {
489505
const stepUuid = stepInfo?.hookStep?.uuid ?? stepInfo?.stepUuid;
@@ -558,6 +574,7 @@ export class AllureReporter implements ReporterV2 {
558574
});
559575
this.allureRuntime!.stopTest(testUuid, { duration: result.duration });
560576
this.allureRuntime!.writeTest(testUuid);
577+
this.attachmentTargets.delete(test.id);
561578
}
562579

563580
async addSkippedResults() {

packages/allure-playwright/src/model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export interface AttachStack extends TestStep {
1818
uuid: string;
1919
}
2020

21+
export type AttachmentTarget = {
22+
name: string;
23+
stepUuid?: string;
24+
hookStep?: AttachStack;
25+
};
26+
2127
export interface ReporterV2 {
2228
onConfigure(config: FullConfig): void;
2329

packages/allure-playwright/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ export const diffEndRegexp = /-((expected)|(diff)|(actual))\.png$/;
4545
export const normalizeHookTitle = (title: string) => {
4646
return title.replace(/^[aA]ttach\s"(.+)"$/, "$1");
4747
};
48+
49+
export const normalizeAttachStepTitle = (title: string): string => {
50+
const match = title.match(/["'`]{1}(.+?)["'`]{1}/);
51+
if (match?.[1]) {
52+
return match[1];
53+
}
54+
return title.replace(/^(test\.)?attach\s*/i, "").trim();
55+
};

0 commit comments

Comments
 (0)