2021-08-05 22:36:47 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2021-08-08 00:47:03 +02:00
|
|
|
import { FullConfig, Location, Suite, TestCase, TestError, TestResult, TestStatus, TestStep } from '../../../types/testReporter';
|
2021-08-11 02:06:25 +02:00
|
|
|
import { calculateSha1 } from '../../utils/utils';
|
2021-08-08 00:47:03 +02:00
|
|
|
import { formatResultFailure } from './base';
|
2021-08-05 22:36:47 +02:00
|
|
|
import { serializePatterns, toPosixPath } from './json';
|
|
|
|
|
|
|
|
|
|
export type JsonStats = { expected: number, unexpected: number, flaky: number, skipped: number };
|
|
|
|
|
export type JsonLocation = Location;
|
|
|
|
|
|
|
|
|
|
export type JsonConfig = Omit<FullConfig, 'projects'> & {
|
|
|
|
|
projects: {
|
|
|
|
|
outputDir: string,
|
|
|
|
|
repeatEach: number,
|
|
|
|
|
retries: number,
|
|
|
|
|
metadata: any,
|
|
|
|
|
name: string,
|
|
|
|
|
testDir: string,
|
|
|
|
|
testIgnore: string[],
|
|
|
|
|
testMatch: string[],
|
|
|
|
|
timeout: number,
|
|
|
|
|
}[],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type JsonReport = {
|
|
|
|
|
config: JsonConfig,
|
|
|
|
|
stats: JsonStats,
|
|
|
|
|
suites: JsonSuite[],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type JsonSuite = {
|
|
|
|
|
title: string;
|
|
|
|
|
location?: JsonLocation;
|
|
|
|
|
suites: JsonSuite[];
|
|
|
|
|
tests: JsonTestCase[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type JsonTestCase = {
|
2021-08-11 02:06:25 +02:00
|
|
|
testId: string;
|
2021-08-05 22:36:47 +02:00
|
|
|
title: string;
|
|
|
|
|
location: JsonLocation;
|
|
|
|
|
expectedStatus: TestStatus;
|
|
|
|
|
timeout: number;
|
|
|
|
|
annotations: { type: string, description?: string }[];
|
|
|
|
|
retries: number;
|
|
|
|
|
results: JsonTestResult[];
|
|
|
|
|
ok: boolean;
|
|
|
|
|
outcome: 'skipped' | 'expected' | 'unexpected' | 'flaky';
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-08 00:47:03 +02:00
|
|
|
export type TestAttachment = {
|
|
|
|
|
name: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
body?: Buffer;
|
|
|
|
|
contentType: string;
|
|
|
|
|
sha1?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type JsonAttachment = {
|
|
|
|
|
name: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
body?: string;
|
|
|
|
|
contentType: string;
|
|
|
|
|
sha1?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-05 22:36:47 +02:00
|
|
|
export type JsonTestResult = {
|
|
|
|
|
retry: number;
|
|
|
|
|
workerIndex: number;
|
|
|
|
|
startTime: string;
|
|
|
|
|
duration: number;
|
|
|
|
|
status: TestStatus;
|
|
|
|
|
error?: TestError;
|
|
|
|
|
failureSnippet?: string;
|
2021-08-08 00:47:03 +02:00
|
|
|
attachments: JsonAttachment[];
|
2021-08-05 22:36:47 +02:00
|
|
|
stdout: (string | Buffer)[];
|
|
|
|
|
stderr: (string | Buffer)[];
|
|
|
|
|
steps: JsonTestStep[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type JsonTestStep = {
|
|
|
|
|
title: string;
|
|
|
|
|
category: string,
|
|
|
|
|
startTime: string;
|
|
|
|
|
duration: number;
|
|
|
|
|
error?: TestError;
|
|
|
|
|
steps: JsonTestStep[];
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-08 00:47:03 +02:00
|
|
|
class HtmlReporter {
|
2021-08-11 02:06:25 +02:00
|
|
|
private _reportFolder: string;
|
|
|
|
|
private _resourcesFolder: string;
|
2021-08-08 00:47:03 +02:00
|
|
|
private config!: FullConfig;
|
|
|
|
|
private suite!: Suite;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
2021-08-11 02:06:25 +02:00
|
|
|
this._reportFolder = path.resolve(process.cwd(), process.env[`PLAYWRIGHT_HTML_REPORT`] || 'playwright-report');
|
|
|
|
|
this._resourcesFolder = path.join(this._reportFolder, 'resources');
|
|
|
|
|
fs.mkdirSync(this._resourcesFolder, { recursive: true });
|
2021-08-05 22:36:47 +02:00
|
|
|
const appFolder = path.join(__dirname, '..', '..', 'web', 'htmlReport');
|
|
|
|
|
for (const file of fs.readdirSync(appFolder))
|
2021-08-11 02:06:25 +02:00
|
|
|
fs.copyFileSync(path.join(appFolder, file), path.join(this._reportFolder, file));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onBegin(config: FullConfig, suite: Suite) {
|
|
|
|
|
this.config = config;
|
|
|
|
|
this.suite = suite;
|
2021-08-08 00:47:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onEnd() {
|
2021-08-05 22:36:47 +02:00
|
|
|
const stats: JsonStats = { expected: 0, unexpected: 0, skipped: 0, flaky: 0 };
|
2021-08-08 00:47:03 +02:00
|
|
|
this.suite.allTests().forEach(t => {
|
|
|
|
|
++stats[t.outcome()];
|
|
|
|
|
});
|
2021-08-05 22:36:47 +02:00
|
|
|
const output: JsonReport = {
|
|
|
|
|
config: {
|
|
|
|
|
...this.config,
|
|
|
|
|
rootDir: toPosixPath(this.config.rootDir),
|
|
|
|
|
projects: this.config.projects.map(project => {
|
|
|
|
|
return {
|
|
|
|
|
outputDir: toPosixPath(project.outputDir),
|
|
|
|
|
repeatEach: project.repeatEach,
|
|
|
|
|
retries: project.retries,
|
|
|
|
|
metadata: project.metadata,
|
|
|
|
|
name: project.name,
|
|
|
|
|
testDir: toPosixPath(project.testDir),
|
|
|
|
|
testIgnore: serializePatterns(project.testIgnore),
|
|
|
|
|
testMatch: serializePatterns(project.testMatch),
|
|
|
|
|
timeout: project.timeout,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
stats,
|
2021-08-08 00:47:03 +02:00
|
|
|
suites: await Promise.all(this.suite.suites.map(s => this._serializeSuite(s)))
|
2021-08-05 22:36:47 +02:00
|
|
|
};
|
2021-08-11 02:06:25 +02:00
|
|
|
fs.writeFileSync(path.join(this._reportFolder, 'report.json'), JSON.stringify(output));
|
2021-08-05 22:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _relativeLocation(location: Location | undefined): Location {
|
|
|
|
|
if (!location)
|
|
|
|
|
return { file: '', line: 0, column: 0 };
|
|
|
|
|
return {
|
|
|
|
|
file: toPosixPath(path.relative(this.config.rootDir, location.file)),
|
|
|
|
|
line: location.line,
|
|
|
|
|
column: location.column,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 00:47:03 +02:00
|
|
|
private async _serializeSuite(suite: Suite): Promise<JsonSuite> {
|
2021-08-05 22:36:47 +02:00
|
|
|
return {
|
|
|
|
|
title: suite.title,
|
|
|
|
|
location: this._relativeLocation(suite.location),
|
2021-08-08 00:47:03 +02:00
|
|
|
suites: await Promise.all(suite.suites.map(s => this._serializeSuite(s))),
|
|
|
|
|
tests: await Promise.all(suite.tests.map(t => this._serializeTest(t))),
|
2021-08-05 22:36:47 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 00:47:03 +02:00
|
|
|
private async _serializeTest(test: TestCase): Promise<JsonTestCase> {
|
2021-08-11 02:06:25 +02:00
|
|
|
const testId = calculateSha1(test.titlePath().join('|'));
|
2021-08-05 22:36:47 +02:00
|
|
|
return {
|
2021-08-11 02:06:25 +02:00
|
|
|
testId,
|
2021-08-05 22:36:47 +02:00
|
|
|
title: test.title,
|
|
|
|
|
location: this._relativeLocation(test.location),
|
|
|
|
|
expectedStatus: test.expectedStatus,
|
|
|
|
|
timeout: test.timeout,
|
|
|
|
|
annotations: test.annotations,
|
|
|
|
|
retries: test.retries,
|
|
|
|
|
ok: test.ok(),
|
|
|
|
|
outcome: test.outcome(),
|
2021-08-11 02:06:25 +02:00
|
|
|
results: await Promise.all(test.results.map(r => this._serializeResult(testId, test, r))),
|
2021-08-05 22:36:47 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 02:06:25 +02:00
|
|
|
private async _serializeResult(testId: string, test: TestCase, result: TestResult): Promise<JsonTestResult> {
|
2021-08-05 22:36:47 +02:00
|
|
|
return {
|
|
|
|
|
retry: result.retry,
|
|
|
|
|
workerIndex: result.workerIndex,
|
|
|
|
|
startTime: result.startTime.toISOString(),
|
|
|
|
|
duration: result.duration,
|
|
|
|
|
status: result.status,
|
|
|
|
|
error: result.error,
|
|
|
|
|
failureSnippet: formatResultFailure(test, result, '').join('') || undefined,
|
2021-08-11 02:06:25 +02:00
|
|
|
attachments: await this._createAttachments(testId, result),
|
2021-08-05 22:36:47 +02:00
|
|
|
stdout: result.stdout,
|
|
|
|
|
stderr: result.stderr,
|
2021-08-17 22:57:26 +02:00
|
|
|
steps: serializeSteps(result.steps)
|
2021-08-05 22:36:47 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 02:06:25 +02:00
|
|
|
private async _createAttachments(testId: string, result: TestResult): Promise<JsonAttachment[]> {
|
|
|
|
|
const attachments: JsonAttachment[] = [];
|
|
|
|
|
for (const attachment of result.attachments) {
|
2021-08-08 00:47:03 +02:00
|
|
|
if (attachment.path) {
|
2021-08-11 02:06:25 +02:00
|
|
|
const sha1 = calculateSha1(attachment.path) + path.extname(attachment.path);
|
|
|
|
|
fs.copyFileSync(attachment.path, path.join(this._resourcesFolder, sha1));
|
|
|
|
|
attachments.push({
|
|
|
|
|
...attachment,
|
|
|
|
|
body: undefined,
|
|
|
|
|
sha1
|
|
|
|
|
});
|
|
|
|
|
} else if (attachment.body && isTextAttachment(attachment.contentType)) {
|
|
|
|
|
attachments.push({ ...attachment, body: attachment.body.toString() });
|
|
|
|
|
} else {
|
|
|
|
|
const sha1 = calculateSha1(attachment.body!) + '.dat';
|
|
|
|
|
fs.writeFileSync(path.join(this._resourcesFolder, sha1), attachment.body);
|
|
|
|
|
attachments.push({
|
2021-08-08 00:47:03 +02:00
|
|
|
...attachment,
|
|
|
|
|
body: undefined,
|
|
|
|
|
sha1
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-11 02:06:25 +02:00
|
|
|
|
|
|
|
|
if (result.stdout.length)
|
|
|
|
|
attachments.push(this._stdioAttachment(testId, result, 'stdout'));
|
|
|
|
|
if (result.stderr.length)
|
|
|
|
|
attachments.push(this._stdioAttachment(testId, result, 'stderr'));
|
|
|
|
|
return attachments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _stdioAttachment(testId: string, result: TestResult, type: 'stdout' | 'stderr'): JsonAttachment {
|
|
|
|
|
const sha1 = `${testId}.${result.retry}.${type}`;
|
|
|
|
|
const fileName = path.join(this._resourcesFolder, sha1);
|
|
|
|
|
for (const chunk of type === 'stdout' ? result.stdout : result.stderr) {
|
|
|
|
|
if (typeof chunk === 'string')
|
|
|
|
|
fs.appendFileSync(fileName, chunk + '\n');
|
|
|
|
|
else
|
|
|
|
|
fs.appendFileSync(fileName, chunk);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
name: type,
|
|
|
|
|
contentType: 'application/octet-stream',
|
|
|
|
|
sha1
|
|
|
|
|
};
|
2021-08-08 00:47:03 +02:00
|
|
|
}
|
2021-08-05 22:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 22:57:26 +02:00
|
|
|
function serializeSteps(steps: TestStep[]): JsonTestStep[] {
|
|
|
|
|
return steps.map(step => {
|
|
|
|
|
return {
|
|
|
|
|
title: step.title,
|
|
|
|
|
category: step.category,
|
|
|
|
|
startTime: step.startTime.toISOString(),
|
|
|
|
|
duration: step.duration,
|
|
|
|
|
error: step.error,
|
|
|
|
|
steps: serializeSteps(step.steps),
|
|
|
|
|
};
|
|
|
|
|
});
|
2021-08-05 22:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-11 02:06:25 +02:00
|
|
|
function isTextAttachment(contentType: string) {
|
|
|
|
|
if (contentType.startsWith('text/'))
|
|
|
|
|
return true;
|
|
|
|
|
if (contentType.includes('json'))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2021-08-08 00:47:03 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-05 22:36:47 +02:00
|
|
|
export default HtmlReporter;
|