From 5329341ef2481676da933e446f0340b396345d3e Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Fri, 18 Oct 2024 09:42:22 +0200 Subject: [PATCH] allow collecting attachments before completing step --- packages/playwright/src/worker/testInfo.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.ts index 3d15b1fbc1..028bc3ff1f 100644 --- a/packages/playwright/src/worker/testInfo.ts +++ b/packages/playwright/src/worker/testInfo.ts @@ -30,7 +30,8 @@ import type { Attachment } from './testTracing'; import type { StackFrame } from '@protocol/channels'; export interface TestStepInternal { - complete(result: { error?: Error | unknown, attachments?: Attachment[] }): void; + complete(result: { error?: Error | unknown }): void; + attachments: Attachment[]; stepId: string; title: string; category: 'hook' | 'fixture' | 'test.step' | 'expect' | 'attach' | string; @@ -248,7 +249,7 @@ export class TestInfoImpl implements TestInfo { ); } - _addStep(data: Omit): TestStepInternal { + _addStep(data: Omit): TestStepInternal { const stepId = `${data.category}@${++this._lastStepId}`; const parentStep = this._parentStep(data.isStage); @@ -261,10 +262,12 @@ export class TestInfoImpl implements TestInfo { } data.location = data.location || filteredStack[0]; + const attachments: Attachment[] = []; const step: TestStepInternal = { stepId, ...data, steps: [], + attachments, complete: result => { if (step.endWallTime) return; @@ -292,7 +295,7 @@ export class TestInfoImpl implements TestInfo { } } - for (const attachment of result.attachments ?? []) + for (const attachment of attachments ?? []) this._attach(attachment, stepId); const payload: StepEndPayload = { @@ -303,7 +306,7 @@ export class TestInfoImpl implements TestInfo { }; this._onStepEnd(payload); const errorForTrace = step.error ? { name: '', message: step.error.message || '', stack: step.error.stack } : undefined; - this._tracing.appendAfterActionForStep(stepId, errorForTrace, result.attachments); + this._tracing.appendAfterActionForStep(stepId, errorForTrace, attachments); } }; const parentStepList = parentStep ? parentStep.steps : this._steps; @@ -407,7 +410,8 @@ export class TestInfoImpl implements TestInfo { category: 'attach', }); const attachment = await normalizeAndSaveAttachment(this.outputPath(), name, options); - step.complete({ attachments: [attachment] }); + step.attachments.push(attachment); + step.complete({}); } private _attach(attachment: TestInfo['attachments'][0], stepId: string | undefined) {