Skip to content

Commit 206b85c

Browse files
Dmitrii GridnevNickVolynkin
authored andcommitted
qase-playwright: add capturing logs
Update the reporter for attaching stdout and stderr as attachments .
1 parent e1edfcb commit 206b85c

File tree

5 files changed

+76
-32
lines changed

5 files changed

+76
-32
lines changed

qase-javascript-commons/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## What's new
44

5-
* Update the config of reporters. Added `framework.captureLogs` field. If it is set to `true`, the reporter will capture logs from the test framework.
5+
* Update the config of reporters. Added `captureLogs` field. If it is set to `true`, the reporter will capture logs from the test framework.
66
* Added `getMimeType` function to the commons package. It returns the MIME type of the file by its extension.
77

88

qase-playwright/changelog.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1+
2+
3+
## What's new
4+
5+
Capture `stdout` and `stderr` logs as attachments.
6+
To enable this feature, set environment variable `QASE_CAPTURE_LOGS=true` or
7+
add `captureLogs: true` to the reporter configuration:
8+
9+
```diff
10+
[
11+
'playwright-qase-reporter',
12+
{
13+
mode: 'testops',
14+
+ captureLogs: true,
15+
...
16+
},
17+
];
18+
```
19+
120
221

322
## What's new
423

5-
Added support for the version of [email protected].
24+
Upload test attachments to Qase:
25+
```js
26+
test('test', async ({ page }) => {
27+
// upload files by path
28+
qase.attach({ paths: '/path/to/file'});
29+
// list multiple files at once
30+
qase.attach({ paths: ['/path/to/file', '/path/to/another/file']});
31+
// upload contents directly from your code
32+
qase.attach({ name: 'attachment.txt', content: 'Hello, world!', contentType: 'text/plain' });
33+
await page.goto('https://example.com');
34+
});
35+
```
636

737
838

qase-playwright/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "playwright-qase-reporter",
3-
"version": "2.0.0-beta.5",
3+
"version": "2.0.0-beta.6",
44
"description": "Qase TMS Playwright Reporter",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -43,7 +43,7 @@
4343
"author": "Aleksei Galagan <[email protected]> (https://github.com/alexneo2003/)",
4444
"license": "Apache-2.0",
4545
"dependencies": {
46-
"qase-javascript-commons": "^2.0.0-beta.4",
46+
"qase-javascript-commons": "^2.0.0-beta.5",
4747
"uuid": "^9.0.0"
4848
},
4949
"peerDependencies": {

qase-playwright/src/playwright.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from '@playwright/test';
22
import { v4 as uuidv4 } from 'uuid';
33
import { PlaywrightQaseReporter } from './reporter';
44
import * as path from 'path';
5+
import { getMimeTypes } from 'qase-javascript-commons';
56

67
export const ReporterContentType = 'application/qase.metadata+json';
78
const defaultContentType = 'application/octet-stream';
@@ -141,7 +142,7 @@ qase.attach = function(attach: {
141142

142143
for (const file of files) {
143144
const attachmentName = path.basename(file);
144-
const contentType = getContentType(path.extname(file));
145+
const contentType: string = getMimeTypes(file);
145146
addAttachment(attachmentName, contentType, file);
146147
}
147148

@@ -161,28 +162,6 @@ const addMetadata = (metadata: MetadataMessage): void => {
161162
});
162163
};
163164

164-
const getContentType = (extension: string): string => {
165-
const types: Record<string, string> = {
166-
'.html': 'text/html',
167-
'.css': 'text/css',
168-
'.js': 'application/javascript',
169-
'.json': 'application/json',
170-
'.png': 'image/png',
171-
'.jpg': 'image/jpeg',
172-
'.gif': 'image/gif',
173-
'.svg': 'image/svg+xml',
174-
'.wav': 'audio/wav',
175-
'.mp4': 'video/mp4',
176-
'.woff': 'application/font-woff',
177-
'.ttf': 'application/font-ttf',
178-
'.eot': 'application/vnd.ms-fontobject',
179-
'.otf': 'application/font-otf',
180-
'.wasm': 'application/wasm',
181-
};
182-
183-
return types[extension] ?? defaultContentType;
184-
};
185-
186165
const addAttachment = (name: string, contentType: string, filePath?: string, body?: string | Buffer): void => {
187166
const stepName = `step_attach_${uuidv4()}_${name}`;
188167

qase-playwright/src/reporter.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { MetadataMessage, ReporterContentType } from './playwright';
1717
type ArrayItemType<T> = T extends (infer R)[] ? R : never;
1818

1919
const stepAttachRegexp = /^step_attach_(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})_/i;
20+
const logMimeType = 'text/plain';
2021

2122
interface TestCaseMetadata {
2223
ids: number[];
@@ -50,9 +51,14 @@ export class PlaywrightQaseReporter implements Reporter {
5051
*/
5152
private static qaseIds: Map<string, number[]> = new Map<string, number[]>();
5253

53-
// private static transformSuiteTitle(test: TestCase) {
54-
// return test.titlePath().filter(Boolean);
55-
// }
54+
/**
55+
* @param {TestCase} test
56+
* @returns {string[]}
57+
* @private
58+
*/
59+
private static transformSuiteTitle(test: TestCase): string[] {
60+
return test.titlePath().filter(Boolean);
61+
}
5662

5763
/**
5864
* @type {Map<TestStep, TestCase>}
@@ -252,7 +258,7 @@ export class PlaywrightQaseReporter implements Reporter {
252258
public onTestEnd(test: TestCase, result: TestResult) {
253259
const testCaseMetadata = this.transformAttachments(result.attachments);
254260
const error = result.error ? PlaywrightQaseReporter.transformError(result.error) : null;
255-
261+
const suites = PlaywrightQaseReporter.transformSuiteTitle(test);
256262
const testResult: TestResultType = {
257263
attachments: testCaseMetadata.attachments,
258264
author: null,
@@ -275,13 +281,23 @@ export class PlaywrightQaseReporter implements Reporter {
275281
params: testCaseMetadata.parameters,
276282
relations: [],
277283
run_id: null,
278-
signature: '',
284+
signature: suites.join(':'),
279285
steps: this.transformSteps(result.steps, null),
280286
// suiteTitle: PlaywrightQaseReporter.transformSuiteTitle(test),
281287
testops_id: null,
282288
title: testCaseMetadata.title === '' ? test.title : testCaseMetadata.title,
283289
};
284290

291+
if (this.reporter.isCaptureLogs()) {
292+
if (result.stdout.length > 0) {
293+
testResult.attachments.push(this.convertLogsToAttachments(result.stdout, 'stdout.log'));
294+
}
295+
296+
if (result.stderr.length > 0) {
297+
testResult.attachments.push(this.convertLogsToAttachments(result.stderr, 'stderr.log'));
298+
}
299+
}
300+
285301
if (testCaseMetadata.ids.length > 0) {
286302
testResult.testops_id = testCaseMetadata.ids;
287303
} else {
@@ -302,4 +318,23 @@ export class PlaywrightQaseReporter implements Reporter {
302318
public static addIds(ids: number[], title: string): void {
303319
this.qaseIds.set(title, ids);
304320
}
321+
322+
/**
323+
* @param {(string | Buffer)[]} logs
324+
* @param {string} name
325+
* @returns {Attachment}
326+
* @private
327+
*/
328+
private convertLogsToAttachments(logs: (string | Buffer)[], name: string): Attachment {
329+
let content = '';
330+
for (const line of logs) {
331+
content = content + line.toString();
332+
}
333+
334+
return {
335+
file_name: name,
336+
mime_type: logMimeType,
337+
content: content,
338+
} as Attachment;
339+
}
305340
}

0 commit comments

Comments
 (0)