add attachments array

This commit is contained in:
Simon Knott 2024-10-16 09:17:06 +02:00
parent 606860d250
commit 13d305b959
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
6 changed files with 55 additions and 12 deletions

View file

@ -50,6 +50,16 @@ Start time of this particular test step.
List of steps inside this step.
## property: TestStep.attachments
* since: v1.10
- type: <[Array]<[Object]>>
- `name` <[string]> Attachment name.
- `contentType` <[string]> Content type of this attachment to properly present in the report, for example `'application/json'` or `'image/png'`.
- `path` ?<[string]> Optional path on the filesystem to the attached file.
- `body` ?<[Buffer]> Optional attachment body used instead of a file.
The list of files or buffers attached in the step execution through [`property: TestInfo.attachments`].
## property: TestStep.title
* since: v1.10
- type: <[string]>

View file

@ -72,6 +72,7 @@ export type AttachmentPayload = {
path?: string;
body?: string;
contentType: string;
stepId?: string;
};
export type TestEndPayload = {

View file

@ -512,6 +512,7 @@ class TeleTestStep implements reporterTypes.TestStep {
parent: reporterTypes.TestStep | undefined;
duration: number = -1;
steps: reporterTypes.TestStep[] = [];
attachments = [];
private _startTime: number = 0;

View file

@ -319,6 +319,7 @@ class JobDispatcher {
startTime: new Date(params.wallTime),
duration: -1,
steps: [],
attachments: [],
location: params.location,
};
steps.set(params.stepId, step);
@ -358,6 +359,7 @@ class JobDispatcher {
body: params.body !== undefined ? Buffer.from(params.body, 'base64') : undefined
};
data.result.attachments.push(attachment);
data.steps.get(params.stepId ?? '')?.attachments.push(attachment);
}
private _failTestWithErrors(test: TestCase, errors: TestError[]) {

View file

@ -237,20 +237,21 @@ export class TestInfoImpl implements TestInfo {
}
}
_parentStep(isStage?: boolean) {
if (isStage) {
// Predefined stages form a fixed hierarchy - use the current one as parent.
return this._findLastStageStep(this._steps);
}
return (
zones.zoneData<TestStepInternal>('stepZone')
?? this._findLastStageStep(this._steps) // If no parent step on stack, assume the current stage as parent.
);
}
_addStep(data: Omit<TestStepInternal, 'complete' | 'stepId' | 'steps'>): TestStepInternal {
const stepId = `${data.category}@${++this._lastStepId}`;
let parentStep: TestStepInternal | undefined;
if (data.isStage) {
// Predefined stages form a fixed hierarchy - use the current one as parent.
parentStep = this._findLastStageStep(this._steps);
} else {
parentStep = zones.zoneData<TestStepInternal>('stepZone');
if (!parentStep) {
// If no parent step on stack, assume the current stage as parent.
parentStep = this._findLastStageStep(this._steps);
}
}
const parentStep = this._parentStep(data.isStage);
const filteredStack = filteredStackTrace(captureRawStack());
data.boxedStack = parentStep?.boxedStack;
@ -414,7 +415,8 @@ export class TestInfoImpl implements TestInfo {
name: attachment.name,
contentType: attachment.contentType,
path: attachment.path,
body: attachment.body?.toString('base64')
body: attachment.body?.toString('base64'),
stepId: this._parentStep()?.stepId,
});
}

View file

@ -684,6 +684,33 @@ export interface TestStep {
*/
titlePath(): Array<string>;
/**
* The list of files or buffers attached in the step execution through
* [testInfo.attachments](https://playwright.dev/docs/api/class-testinfo#test-info-attachments).
*/
attachments: Array<{
/**
* Attachment name.
*/
name: string;
/**
* Content type of this attachment to properly present in the report, for example `'application/json'` or
* `'image/png'`.
*/
contentType: string;
/**
* Optional path on the filesystem to the attached file.
*/
path?: string;
/**
* Optional attachment body used instead of a file.
*/
body?: Buffer;
}>;
/**
* Step category to differentiate steps with different origin and verbosity. Built-in categories are:
* - `hook` for fixtures and hooks initialization and teardown