Skip to content

Commit

Permalink
feature: ability to add attachments to tests or steps
Browse files Browse the repository at this point in the history
- `qase.attach` - add an attachment to test or step

```ts
it('test', () => {
  qase.attach({ paths: '/path/to/file' });
  qase.step('Step 1', () => {
    cy.visit('https://example.com');
    qase.attach({ name: 'attachment.txt', content: 'Hello, world!', contentType: 'text/plain' });
  });
});
```
  • Loading branch information
gibiw committed Sep 19, 2024
1 parent 1677510 commit 9b8ee10
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 11 deletions.
18 changes: 18 additions & 0 deletions qase-cypress/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# [email protected]

## What's new

Added the ability to add attachments to tests or steps:

- `qase.attach` - add an attachment to test or step

```ts
it('test', () => {
qase.attach({ paths: '/path/to/file' });
qase.step('Step 1', () => {
cy.visit('https://example.com');
qase.attach({ name: 'attachment.txt', content: 'Hello, world!', contentType: 'text/plain' });
});
});
```

# [email protected]

## What's new
Expand Down
2 changes: 1 addition & 1 deletion qase-cypress/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-qase-reporter",
"version": "2.2.0-beta.2",
"version": "2.2.0-beta.3",
"description": "Qase Cypress Reporter",
"homepage": "https://github.com/qase-tms/qase-javascript",
"sideEffects": false,
Expand Down
7 changes: 7 additions & 0 deletions qase-cypress/src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ module.exports = function(on) {
return null;
},
});

on('task', {
qaseAttach(value) {
MetadataManager.addAttach(value);
return null;
},
});
};
73 changes: 72 additions & 1 deletion qase-cypress/src/metadata/manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Metadata, StepStart } from './models';
import { Attach, Metadata, StepStart } from './models';
import { readFileSync, existsSync, unlinkSync, writeFileSync } from 'fs';
import { v4 as uuidv4 } from 'uuid';
import path from 'path';
import { Attachment, getMimeTypes } from 'qase-javascript-commons';

const metadataPath = 'qaseMetadata';

Expand All @@ -22,6 +24,8 @@ export class MetadataManager {
steps: [],
currentStepId: undefined,
firstStepName: undefined,
attachments: [],
stepAttachments: {},
};

try {
Expand Down Expand Up @@ -75,6 +79,31 @@ export class MetadataManager {
this.setMetadata(metadata);
}

public static addAttach(attach: Attach) {
const metadata = this.getMetadata() ?? {};

if (!metadata.attachments) {
metadata.attachments = [];
}
if (!metadata.stepAttachments) {
metadata.stepAttachments = {};
}

const attachments = this.prepareAttach(attach);

if (metadata.currentStepId) {
if (metadata.stepAttachments[metadata.currentStepId] === undefined) {
metadata.stepAttachments[metadata.currentStepId] = attachments;
} else {
metadata.stepAttachments[metadata.currentStepId]?.push(...attachments);
}
} else {
metadata.attachments.push(...attachments);
}

this.setMetadata(metadata);
}

public static setSuite(suite: string): void {
const metadata = this.getMetadata() ?? {};
metadata.suite = suite;
Expand Down Expand Up @@ -135,4 +164,46 @@ export class MetadataManager {
static isExists(): boolean {
return existsSync(metadataPath);
}

static prepareAttach(attach: Attach): Attachment[] {
const attachments: Attachment[] = [];
if (attach.paths) {
if (Array.isArray(attach.paths)) {
attach.paths.forEach((file) => {
const attachmentName = path.basename(file);
const contentType: string = getMimeTypes(file);
attachments.push({
file_name: attachmentName,
mime_type: contentType,
file_path: file,
content: '',
size: 0,
id: uuidv4(),
});
});
} else {
const attachmentName = path.basename(attach.paths);
const contentType: string = getMimeTypes(attach.paths);
attachments.push({
file_name: attachmentName,
mime_type: contentType,
file_path: attach.paths,
content: '',
size: 0,
id: uuidv4(),
});
}
} else if (attach.content) {
attachments.push({
file_name: attach.name ?? 'attachment',
mime_type: attach.contentType ?? 'application/octet-stream',
file_path: null,
content: attach.content,
size: 0,
id: uuidv4(),
});
}

return attachments;
}
}
12 changes: 12 additions & 0 deletions qase-cypress/src/metadata/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Attachment } from 'qase-javascript-commons';

export interface Metadata {
title?: string | undefined;
fields?: Record<string, string>;
Expand All @@ -9,6 +11,8 @@ export interface Metadata {
steps?: (StepStart | StepEnd)[];
currentStepId?: string | undefined;
firstStepName?: string | undefined;
attachments?: Attachment[];
stepAttachments?: Record<string, Attachment[]>;
}

export interface StepStart {
Expand All @@ -23,3 +27,11 @@ export interface StepEnd {
timestamp: number;
status: string;
}


export interface Attach {
name?: string;
paths?: string | string[];
content?: Buffer | string;
contentType?: string;
}
36 changes: 29 additions & 7 deletions qase-cypress/src/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const qase = (
qase.title = (
value: string,
) => {
cy.task('qaseTitle', value).then(() => {
return cy.task('qaseTitle', value).then(() => {
//
});
};
Expand All @@ -41,7 +41,7 @@ qase.title = (
qase.fields = (
values: Record<string, string>,
) => {
cy.task('qaseFields', values).then(() => {
return cy.task('qaseFields', values).then(() => {
//
});
};
Expand All @@ -55,7 +55,7 @@ qase.fields = (
* });
*/
qase.ignore = () => {
cy.task('qaseIgnore').then(() => {
return cy.task('qaseIgnore').then(() => {
//
});
};
Expand All @@ -72,7 +72,7 @@ qase.ignore = () => {
qase.parameters = (
values: Record<string, string>,
) => {
cy.task('qaseParameters', values).then(() => {
return cy.task('qaseParameters', values).then(() => {
//
});
};
Expand All @@ -89,7 +89,7 @@ qase.parameters = (
qase.groupParameters = (
values: Record<string, string>,
) => {
cy.task('qaseGroupParameters', values).then(() => {
return cy.task('qaseGroupParameters', values).then(() => {
//
});
};
Expand All @@ -106,7 +106,7 @@ qase.groupParameters = (
qase.suite = (
value: string,
) => {
cy.task('qaseSuite', value).then(() => {
return cy.task('qaseSuite', value).then(() => {
//
});
};
Expand All @@ -123,7 +123,7 @@ qase.suite = (
qase.comment = (
value: string,
) => {
cy.task('qaseComment', value).then(() => {
return cy.task('qaseComment', value).then(() => {
//
});
};
Expand All @@ -149,3 +149,25 @@ qase.step = <T = void>(name: string, body: () => T | PromiseLike<T>) => {
});
});
};

/**
* Attach a file to the test case or the step
* @param attach
* @example
* it('test', () => {
* qase.attach({ name: 'attachment.txt', content: 'Hello, world!', contentType: 'text/plain' });
* qase.attach({ paths: '/path/to/file'});
* qase.attach({ paths: ['/path/to/file', '/path/to/another/file']});
* cy.visit('https://example.com');
* });
*/
qase.attach = (attach: {
name?: string,
paths?: string | string[],
content?: Buffer | string,
contentType?: string,
}) => {
return cy.task('qaseAttach', attach).then(() => {
//
});
};
10 changes: 8 additions & 2 deletions qase-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export class CypressQaseReporter extends reporters.Base {
? CypressQaseReporter.findAttachments(ids, this.screenshotsFolder)
: undefined;

attachments?.push(...(metadata?.attachments ?? []));

let relations = {};
if (test.parent !== undefined) {
const data = [];
Expand Down Expand Up @@ -235,7 +237,7 @@ export class CypressQaseReporter extends reporters.Base {
relations: relations,
run_id: null,
signature: this.getSignature(test, ids),
steps: metadata?.steps ? this.getSteps(metadata.steps) : [],
steps: metadata?.steps ? this.getSteps(metadata.steps, metadata.stepAttachments ?? {}) : [],
id: uuidv4(),
execution: {
status: test.state
Expand Down Expand Up @@ -300,7 +302,7 @@ export class CypressQaseReporter extends reporters.Base {
return undefined;
}

private getSteps(steps: (StepStart | StepEnd)[]): TestStepType[] {
private getSteps(steps: (StepStart | StepEnd)[], attachments: Record<string, Attachment[]>): TestStepType[] {
const result: TestStepType[] = [];
const stepMap = new Map<string, TestStepType>();

Expand All @@ -316,6 +318,10 @@ export class CypressQaseReporter extends reporters.Base {
expected_result: null,
};

if (attachments[step.id]) {
newStep.attachments = attachments[step.id] ?? [];
}

const parentId = step.parentId;
if (parentId) {
newStep.parent_id = parentId;
Expand Down

0 comments on commit 9b8ee10

Please sign in to comment.