Skip to content

Commit fc87cb8

Browse files
committed
playwright: attach correct attachments to correct steps
1 parent f991bc0 commit fc87cb8

File tree

1 file changed

+47
-66
lines changed

1 file changed

+47
-66
lines changed

packages/allure-playwright/src/index.ts

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class AllureReporter implements ReporterV2 {
6969
private processedDiffs: string[] = [];
7070
private readonly startedTestCasesTitlesCache: string[] = [];
7171
private readonly allureResultsUuids: Map<string, string> = new Map();
72-
private readonly attachmentSteps: Map<string, (string | undefined)[]> = new Map();
72+
private readonly attachmentTargets: Map<string, { stepUuid?: string; hookStep?: AttachStack }[]> = new Map();
7373
private beforeHooksStepsStack: Map<string, ShallowStepsStack> = new Map();
7474
private afterHooksStepsStack: Map<string, ShallowStepsStack> = new Map();
7575
private beforeHooksAttachmentsStack: Map<string, AttachStack[]> = new Map();
@@ -286,12 +286,6 @@ export class AllureReporter implements ReporterV2 {
286286
const isHookStep = isBeforeHookDescendant || isAfterHookDescendant;
287287
const testUuid = this.allureResultsUuids.get(test.id)!;
288288

289-
if (["test.attach", "attach"].includes(step.category) && !isHookStep) {
290-
const currentStep = this.allureRuntime?.currentStep(testUuid);
291-
this.attachmentSteps.set(testUuid, [...(this.attachmentSteps.get(testUuid) ?? []), currentStep]);
292-
return;
293-
}
294-
295289
if (this.#shouldIgnoreStep(step)) {
296290
return;
297291
}
@@ -310,18 +304,25 @@ export class AllureReporter implements ReporterV2 {
310304
: this.afterHooksStepsStack.get(test.id)!;
311305

312306
if (["test.attach", "attach"].includes(step.category)) {
307+
let hookStepWithUuid: AttachStack | undefined;
313308
stack.startStep(baseStep);
314309

315310
const attachStack = isBeforeHookDescendant ? this.beforeHooksAttachmentsStack : this.afterHooksAttachmentsStack;
316311

317312
stack.updateStep((stepResult) => {
313+
hookStepWithUuid = { ...step, uuid: stepResult.uuid as string } as AttachStack;
318314
stepResult.name = normalizeHookTitle(stepResult.name!);
319315
stepResult.stage = Stage.FINISHED;
320-
attachStack.set(test.id, [...(attachStack.get(test.id) ?? []), { ...step, uuid: stepResult.uuid as string }]);
316+
attachStack.set(test.id, [...(attachStack.get(test.id) ?? []), hookStepWithUuid]);
321317
});
322318
stack.stopStep();
319+
320+
const targets = this.attachmentTargets.get(test.id) ?? [];
321+
targets.push({ hookStep: hookStepWithUuid });
322+
this.attachmentTargets.set(test.id, targets);
323323
return;
324324
}
325+
325326
stack.startStep(baseStep);
326327
return;
327328
}
@@ -336,6 +337,16 @@ export class AllureReporter implements ReporterV2 {
336337
}
337338
return;
338339
}
340+
341+
if (["test.attach", "attach"].includes(step.category) && !isHookStep) {
342+
const parent = step.parent ? this.pwStepUuid.get(step.parent) ?? null : null;
343+
const targets = this.attachmentTargets.get(test.id) ?? [];
344+
targets.push({ stepUuid: parent ?? undefined });
345+
this.attachmentTargets.set(test.id, targets);
346+
347+
return;
348+
}
349+
339350
const parentUuid = step.parent ? this.pwStepUuid.get(step.parent) ?? null : null;
340351
const createdUuid = this.allureRuntime!.startStep(testUuid, parentUuid, baseStep);
341352

@@ -384,6 +395,7 @@ export class AllureReporter implements ReporterV2 {
384395
}
385396

386397
const stepUuid = this.pwStepUuid.get(step);
398+
387399
if (!stepUuid) {
388400
return;
389401
}
@@ -442,53 +454,6 @@ export class AllureReporter implements ReporterV2 {
442454
testResult.stage = Stage.FINISHED;
443455
});
444456

445-
const attachmentsInBeforeHooks = this.beforeHooksAttachmentsStack.get(test.id) ?? [];
446-
const attachmentsInAfterHooks = this.afterHooksAttachmentsStack.get(test.id) ?? [];
447-
const attachmentSteps = this.attachmentSteps.get(testUuid) ?? [];
448-
449-
const attachmentToStepMap = new Map<number, { stepUuid?: string; isHook: boolean; hookStep?: AttachStack }>();
450-
451-
let attachmentIndex = 0;
452-
453-
for (const hookStep of attachmentsInBeforeHooks) {
454-
attachmentToStepMap.set(attachmentIndex, {
455-
stepUuid: hookStep.uuid,
456-
isHook: true,
457-
hookStep,
458-
});
459-
attachmentIndex++;
460-
}
461-
462-
for (const stepUuid of attachmentSteps) {
463-
attachmentToStepMap.set(attachmentIndex, {
464-
stepUuid,
465-
isHook: false,
466-
});
467-
attachmentIndex++;
468-
}
469-
470-
for (const hookStep of attachmentsInAfterHooks) {
471-
attachmentToStepMap.set(attachmentIndex, {
472-
stepUuid: hookStep.uuid,
473-
isHook: true,
474-
hookStep,
475-
});
476-
attachmentIndex++;
477-
}
478-
479-
for (let i = 0; i < result.attachments.length; i++) {
480-
const attachment = result.attachments[i];
481-
const stepInfo = attachmentToStepMap.get(i);
482-
483-
if (stepInfo?.isHook) {
484-
continue;
485-
} else if (stepInfo?.stepUuid) {
486-
await this.processAttachment(testUuid, stepInfo.stepUuid, attachment);
487-
} else {
488-
await this.processAttachment(testUuid, undefined, attachment);
489-
}
490-
}
491-
492457
if (result.stdout.length > 0) {
493458
this.allureRuntime!.writeAttachment(
494459
testUuid,
@@ -513,23 +478,29 @@ export class AllureReporter implements ReporterV2 {
513478
);
514479
}
515480

516-
// FIXME: temp logic for labels override, we need it here to keep the reporter compatible with v2 API
517-
// in next iterations we need to implement the logic for every javascript integration
481+
const attachmentsInBeforeHooks = this.beforeHooksAttachmentsStack.get(test.id) ?? [];
482+
const attachmentTargets = this.attachmentTargets.get(test.id) ?? [];
483+
484+
let targetIndex = 0;
485+
486+
for (const attachment of result.attachments) {
487+
const isRuntimeMessage = attachment.contentType === ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE;
488+
const isTestAttach = !!attachment.body && !isRuntimeMessage;
489+
490+
const stepInfo =
491+
isTestAttach && targetIndex < attachmentTargets.length ? attachmentTargets[targetIndex++] : undefined;
518492

519-
for (let i = 0; i < result.attachments.length; i++) {
520-
const attachment = result.attachments[i];
521-
const stepInfo = attachmentToStepMap.get(i);
493+
if (isRuntimeMessage) {
494+
const stepUuid = stepInfo?.hookStep?.uuid ?? stepInfo?.stepUuid;
495+
await this.processAttachment(testUuid, stepUuid, attachment);
496+
continue;
497+
}
522498

523-
if (stepInfo?.isHook && stepInfo.hookStep) {
499+
if (stepInfo?.hookStep) {
524500
const hookStep = stepInfo.hookStep;
525501
const isBeforeHook = attachmentsInBeforeHooks.includes(hookStep);
526502
const targetStack = isBeforeHook ? beforeHooksStack : afterHooksStack;
527503

528-
if (attachment.contentType === ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE) {
529-
await this.processAttachment(testUuid, hookStep.uuid, attachment);
530-
continue;
531-
}
532-
533504
if (targetStack) {
534505
const stepResult = targetStack.findStepByUuid(hookStep.uuid);
535506
if (stepResult) {
@@ -541,9 +512,19 @@ export class AllureReporter implements ReporterV2 {
541512
});
542513
}
543514
}
515+
continue;
544516
}
517+
518+
if (stepInfo?.stepUuid) {
519+
await this.processAttachment(testUuid, stepInfo.stepUuid, attachment);
520+
continue;
521+
}
522+
523+
await this.processAttachment(testUuid, undefined, attachment);
545524
}
546525

526+
// FIXME: temp logic for labels override, we need it here to keep the reporter compatible with v2 API
527+
// in next iterations we need to implement the logic for every javascript integration
547528
this.allureRuntime!.updateTest(testUuid, (testResult) => {
548529
const mappedLabels = testResult.labels.reduce<Record<string, Label[]>>((acc, label) => {
549530
if (!acc[label.name]) {

0 commit comments

Comments
 (0)