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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-19 02:50:25 +02:00
|
|
|
import { colors } from 'playwright-core/lib/utilsBundle';
|
2021-08-05 22:36:47 +02:00
|
|
|
import fs from 'fs';
|
2022-04-19 06:47:18 +02:00
|
|
|
import { open } from '../utilsBundle';
|
2021-08-05 22:36:47 +02:00
|
|
|
import path from 'path';
|
2022-04-06 23:57:14 +02:00
|
|
|
import type { TransformCallback } from 'stream';
|
|
|
|
|
import { Transform } from 'stream';
|
2023-01-24 02:44:23 +01:00
|
|
|
import type { FullConfig, Reporter, Suite } from '../../types/testReporter';
|
2023-01-13 22:50:38 +01:00
|
|
|
import { HttpServer, assert, calculateSha1, monotonicTime, copyFileAndMakeWritable, removeFolders } from 'playwright-core/lib/utils';
|
2022-04-06 23:57:14 +02:00
|
|
|
import type { JsonAttachment, JsonReport, JsonSuite, JsonTestCase, JsonTestResult, JsonTestStep } from './raw';
|
|
|
|
|
import RawReporter from './raw';
|
2021-11-02 00:39:54 +01:00
|
|
|
import { stripAnsiEscapes } from './base';
|
2022-06-17 17:09:49 +02:00
|
|
|
import { getPackageJsonPath, sanitizeForFilePath } from '../util';
|
2023-01-27 02:26:47 +01:00
|
|
|
import type { FullConfigInternal, Metadata } from '../common/types';
|
2022-04-19 02:50:25 +02:00
|
|
|
import type { ZipFile } from 'playwright-core/lib/zipBundle';
|
|
|
|
|
import { yazl } from 'playwright-core/lib/zipBundle';
|
2022-06-17 17:09:49 +02:00
|
|
|
import { mime } from 'playwright-core/lib/utilsBundle';
|
2022-09-21 03:41:51 +02:00
|
|
|
import type { HTMLReport, Stats, TestAttachment, TestCase, TestCaseSummary, TestFile, TestFileSummary, TestResult, TestStep } from '@html-reporter/types';
|
2021-08-05 22:36:47 +02:00
|
|
|
|
2021-10-18 05:58:06 +02:00
|
|
|
type TestEntry = {
|
|
|
|
|
testCase: TestCase;
|
|
|
|
|
testCaseSummary: TestCaseSummary
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-18 03:03:13 +01:00
|
|
|
const kMissingContentType = 'x-playwright/missing';
|
|
|
|
|
|
2022-03-29 23:19:31 +02:00
|
|
|
type HtmlReportOpenOption = 'always' | 'never' | 'on-failure';
|
|
|
|
|
type HtmlReporterOptions = {
|
|
|
|
|
outputFolder?: string,
|
|
|
|
|
open?: HtmlReportOpenOption,
|
2022-09-26 05:36:38 +02:00
|
|
|
host?: string,
|
|
|
|
|
port?: number,
|
2022-03-29 23:19:31 +02:00
|
|
|
};
|
|
|
|
|
|
2023-01-24 02:44:23 +01:00
|
|
|
class HtmlReporter implements Reporter {
|
2022-04-02 03:32:34 +02:00
|
|
|
private config!: FullConfigInternal;
|
2021-08-08 00:47:03 +02:00
|
|
|
private suite!: Suite;
|
2022-12-20 23:13:10 +01:00
|
|
|
private _montonicStartTime: number = 0;
|
2022-03-29 23:19:31 +02:00
|
|
|
private _options: HtmlReporterOptions;
|
2022-06-19 00:47:26 +02:00
|
|
|
private _outputFolder!: string;
|
|
|
|
|
private _open: string | undefined;
|
2022-07-25 22:20:33 +02:00
|
|
|
private _buildResult: { ok: boolean, singleTestId: string | undefined } | undefined;
|
2021-10-15 17:15:30 +02:00
|
|
|
|
2022-03-29 23:19:31 +02:00
|
|
|
constructor(options: HtmlReporterOptions = {}) {
|
|
|
|
|
this._options = options;
|
2021-10-15 17:15:30 +02:00
|
|
|
}
|
2021-08-08 00:47:03 +02:00
|
|
|
|
2021-11-03 16:25:16 +01:00
|
|
|
printsToStdio() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 00:38:23 +01:00
|
|
|
onBegin(config: FullConfig, suite: Suite) {
|
2022-12-20 23:13:10 +01:00
|
|
|
this._montonicStartTime = monotonicTime();
|
2022-04-02 03:32:34 +02:00
|
|
|
this.config = config as FullConfigInternal;
|
2022-06-19 00:47:26 +02:00
|
|
|
const { outputFolder, open } = this._resolveOptions();
|
|
|
|
|
this._outputFolder = outputFolder;
|
|
|
|
|
this._open = open;
|
|
|
|
|
const reportedWarnings = new Set<string>();
|
|
|
|
|
for (const project of config.projects) {
|
|
|
|
|
if (outputFolder.startsWith(project.outputDir) || project.outputDir.startsWith(outputFolder)) {
|
|
|
|
|
const key = outputFolder + '|' + project.outputDir;
|
|
|
|
|
if (reportedWarnings.has(key))
|
|
|
|
|
continue;
|
|
|
|
|
reportedWarnings.add(key);
|
|
|
|
|
console.log(colors.red(`Configuration Error: HTML reporter output folder clashes with the tests output folder:`));
|
|
|
|
|
console.log(`
|
|
|
|
|
html reporter folder: ${colors.bold(outputFolder)}
|
|
|
|
|
test results folder: ${colors.bold(project.outputDir)}`);
|
|
|
|
|
console.log('');
|
|
|
|
|
console.log(`HTML reporter will clear its output directory prior to being generated, which will lead to the artifact loss.
|
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-11 02:06:25 +02:00
|
|
|
this.suite = suite;
|
2021-08-08 00:47:03 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 23:19:31 +02:00
|
|
|
_resolveOptions(): { outputFolder: string, open: HtmlReportOpenOption } {
|
|
|
|
|
let { outputFolder } = this._options;
|
|
|
|
|
if (outputFolder)
|
2022-04-02 03:32:34 +02:00
|
|
|
outputFolder = path.resolve(this.config._configDir, outputFolder);
|
2022-03-29 23:19:31 +02:00
|
|
|
return {
|
2022-04-02 03:32:34 +02:00
|
|
|
outputFolder: reportFolderFromEnv() ?? outputFolder ?? defaultReportFolder(this.config._configDir),
|
2022-03-29 23:19:31 +02:00
|
|
|
open: process.env.PW_TEST_HTML_REPORT_OPEN as any || this._options.open || 'on-failure',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 00:47:03 +02:00
|
|
|
async onEnd() {
|
2022-12-20 23:13:10 +01:00
|
|
|
const duration = monotonicTime() - this._montonicStartTime;
|
2021-09-14 05:34:46 +02:00
|
|
|
const projectSuites = this.suite.suites;
|
|
|
|
|
const reports = projectSuites.map(suite => {
|
|
|
|
|
const rawReporter = new RawReporter();
|
2022-05-03 01:28:14 +02:00
|
|
|
const report = rawReporter.generateProjectReport(this.config, suite);
|
2021-09-14 05:34:46 +02:00
|
|
|
return report;
|
2021-08-08 00:47:03 +02:00
|
|
|
});
|
2022-06-19 00:47:26 +02:00
|
|
|
await removeFolders([this._outputFolder]);
|
|
|
|
|
const builder = new HtmlBuilder(this._outputFolder);
|
2022-12-20 23:13:10 +01:00
|
|
|
this._buildResult = await builder.build({ ...this.config.metadata, duration }, reports);
|
2022-07-25 22:20:33 +02:00
|
|
|
}
|
2021-10-18 05:58:06 +02:00
|
|
|
|
2023-01-24 02:44:23 +01:00
|
|
|
async onExit() {
|
2023-01-26 00:38:23 +01:00
|
|
|
if (process.env.CI || !this._buildResult)
|
2021-10-30 01:24:08 +02:00
|
|
|
return;
|
|
|
|
|
|
2023-01-26 00:38:23 +01:00
|
|
|
const { ok, singleTestId } = this._buildResult;
|
2022-06-19 00:47:26 +02:00
|
|
|
const shouldOpen = this._open === 'always' || (!ok && this._open === 'on-failure');
|
2021-10-30 01:24:08 +02:00
|
|
|
if (shouldOpen) {
|
2022-09-26 05:36:38 +02:00
|
|
|
await showHTMLReport(this._outputFolder, this._options.host, this._options.port, singleTestId);
|
2021-10-30 01:24:08 +02:00
|
|
|
} else {
|
2022-06-19 00:47:26 +02:00
|
|
|
const relativeReportPath = this._outputFolder === standaloneDefaultFolder() ? '' : ' ' + path.relative(process.cwd(), this._outputFolder);
|
2021-10-30 01:24:08 +02:00
|
|
|
console.log('');
|
|
|
|
|
console.log('To open last HTML report run:');
|
|
|
|
|
console.log(colors.cyan(`
|
2022-03-29 23:19:31 +02:00
|
|
|
npx playwright show-report${relativeReportPath}
|
2021-10-15 06:09:41 +02:00
|
|
|
`));
|
2021-09-14 22:55:31 +02:00
|
|
|
}
|
2021-08-05 22:36:47 +02:00
|
|
|
}
|
2021-09-14 05:34:46 +02:00
|
|
|
}
|
2021-08-05 22:36:47 +02:00
|
|
|
|
2022-03-29 23:19:31 +02:00
|
|
|
function reportFolderFromEnv(): string | undefined {
|
2021-10-15 17:15:30 +02:00
|
|
|
if (process.env[`PLAYWRIGHT_HTML_REPORT`])
|
|
|
|
|
return path.resolve(process.cwd(), process.env[`PLAYWRIGHT_HTML_REPORT`]);
|
2022-03-29 23:19:31 +02:00
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function defaultReportFolder(searchForPackageJson: string): string {
|
|
|
|
|
let basePath = getPackageJsonPath(searchForPackageJson);
|
|
|
|
|
if (basePath)
|
|
|
|
|
basePath = path.dirname(basePath);
|
|
|
|
|
else
|
|
|
|
|
basePath = process.cwd();
|
|
|
|
|
return path.resolve(basePath, 'playwright-report');
|
2021-11-12 09:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 23:19:31 +02:00
|
|
|
function standaloneDefaultFolder(): string {
|
|
|
|
|
return reportFolderFromEnv() ?? defaultReportFolder(process.cwd());
|
2021-10-15 06:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-27 21:45:42 +02:00
|
|
|
export async function showHTMLReport(reportFolder: string | undefined, host: string = 'localhost', port?: number, testId?: string) {
|
2022-03-29 23:19:31 +02:00
|
|
|
const folder = reportFolder ?? standaloneDefaultFolder();
|
2021-10-15 06:09:41 +02:00
|
|
|
try {
|
|
|
|
|
assert(fs.statSync(folder).isDirectory());
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(colors.red(`No report found at "${folder}"`));
|
|
|
|
|
process.exit(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-23 20:23:39 +02:00
|
|
|
const server = startHtmlReportServer(folder);
|
2022-11-02 23:12:48 +01:00
|
|
|
let url = await server.start({ port, host, preferredPort: port ? undefined : 9323 });
|
2021-10-23 20:23:39 +02:00
|
|
|
console.log('');
|
|
|
|
|
console.log(colors.cyan(` Serving HTML report at ${url}. Press Ctrl+C to quit.`));
|
2021-11-01 17:54:53 +01:00
|
|
|
if (testId)
|
|
|
|
|
url += `#?testId=${testId}`;
|
2022-06-17 18:38:23 +02:00
|
|
|
await open(url, { wait: true }).catch(() => console.log(`Failed to open browser on ${url}`));
|
2021-10-23 20:23:39 +02:00
|
|
|
await new Promise(() => {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function startHtmlReportServer(folder: string): HttpServer {
|
2021-10-15 06:09:41 +02:00
|
|
|
const server = new HttpServer();
|
|
|
|
|
server.routePrefix('/', (request, response) => {
|
|
|
|
|
let relativePath = new URL('http://localhost' + request.url).pathname;
|
2021-10-23 20:23:39 +02:00
|
|
|
if (relativePath.startsWith('/trace/file')) {
|
|
|
|
|
const url = new URL('http://localhost' + request.url!);
|
|
|
|
|
try {
|
2021-11-23 21:37:55 +01:00
|
|
|
return server.serveFile(request, response, url.searchParams.get('path')!);
|
2021-10-23 20:23:39 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-31 04:07:52 +01:00
|
|
|
if (relativePath.endsWith('/stall.js'))
|
|
|
|
|
return true;
|
2021-10-15 06:09:41 +02:00
|
|
|
if (relativePath === '/')
|
|
|
|
|
relativePath = '/index.html';
|
|
|
|
|
const absolutePath = path.join(folder, ...relativePath.split('/'));
|
2021-11-23 21:37:55 +01:00
|
|
|
return server.serveFile(request, response, absolutePath);
|
2021-10-15 06:09:41 +02:00
|
|
|
});
|
2021-10-23 20:23:39 +02:00
|
|
|
return server;
|
2021-10-15 06:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-14 05:34:46 +02:00
|
|
|
class HtmlBuilder {
|
|
|
|
|
private _reportFolder: string;
|
|
|
|
|
private _tests = new Map<string, JsonTestCase>();
|
2021-10-18 05:58:06 +02:00
|
|
|
private _testPath = new Map<string, string[]>();
|
2022-04-18 20:31:58 +02:00
|
|
|
private _dataZipFile: ZipFile;
|
2021-10-18 17:03:04 +02:00
|
|
|
private _hasTraces = false;
|
2021-09-14 05:34:46 +02:00
|
|
|
|
2021-11-02 00:14:52 +01:00
|
|
|
constructor(outputDir: string) {
|
2022-03-29 23:19:31 +02:00
|
|
|
this._reportFolder = outputDir;
|
2021-11-02 00:14:52 +01:00
|
|
|
fs.mkdirSync(this._reportFolder, { recursive: true });
|
|
|
|
|
this._dataZipFile = new yazl.ZipFile();
|
2021-10-15 06:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-20 23:13:10 +01:00
|
|
|
async build(metadata: Metadata & { duration: number }, rawReports: JsonReport[]): Promise<{ ok: boolean, singleTestId: string | undefined }> {
|
2021-10-13 20:07:29 +02:00
|
|
|
|
2021-10-18 05:58:06 +02:00
|
|
|
const data = new Map<string, { testFile: TestFile, testFileSummary: TestFileSummary }>();
|
2021-09-14 05:34:46 +02:00
|
|
|
for (const projectJson of rawReports) {
|
|
|
|
|
for (const file of projectJson.suites) {
|
2021-10-18 22:34:02 +02:00
|
|
|
const fileName = file.location!.file;
|
|
|
|
|
const fileId = file.fileId;
|
2021-10-18 05:58:06 +02:00
|
|
|
let fileEntry = data.get(fileId);
|
|
|
|
|
if (!fileEntry) {
|
|
|
|
|
fileEntry = {
|
2022-03-09 04:08:31 +01:00
|
|
|
testFile: { fileId, fileName, tests: [] },
|
|
|
|
|
testFileSummary: { fileId, fileName, tests: [], stats: emptyStats() },
|
2021-10-18 05:58:06 +02:00
|
|
|
};
|
|
|
|
|
data.set(fileId, fileEntry);
|
|
|
|
|
}
|
|
|
|
|
const { testFile, testFileSummary } = fileEntry;
|
|
|
|
|
const testEntries: TestEntry[] = [];
|
2022-03-09 04:08:31 +01:00
|
|
|
this._processJsonSuite(file, fileId, projectJson.project.name, [], testEntries);
|
2021-10-18 05:58:06 +02:00
|
|
|
for (const test of testEntries) {
|
|
|
|
|
testFile.tests.push(test.testCase);
|
|
|
|
|
testFileSummary.tests.push(test.testCaseSummary);
|
|
|
|
|
}
|
2021-09-14 05:34:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 05:58:06 +02:00
|
|
|
let ok = true;
|
|
|
|
|
for (const [fileId, { testFile, testFileSummary }] of data) {
|
|
|
|
|
const stats = testFileSummary.stats;
|
|
|
|
|
for (const test of testFileSummary.tests) {
|
|
|
|
|
if (test.outcome === 'expected')
|
|
|
|
|
++stats.expected;
|
|
|
|
|
if (test.outcome === 'skipped')
|
|
|
|
|
++stats.skipped;
|
|
|
|
|
if (test.outcome === 'unexpected')
|
|
|
|
|
++stats.unexpected;
|
|
|
|
|
if (test.outcome === 'flaky')
|
|
|
|
|
++stats.flaky;
|
|
|
|
|
++stats.total;
|
|
|
|
|
stats.duration += test.duration;
|
|
|
|
|
}
|
|
|
|
|
stats.ok = stats.unexpected + stats.flaky === 0;
|
|
|
|
|
if (!stats.ok)
|
|
|
|
|
ok = false;
|
|
|
|
|
|
2021-12-15 19:39:49 +01:00
|
|
|
const testCaseSummaryComparator = (t1: TestCaseSummary, t2: TestCaseSummary) => {
|
2021-10-18 05:58:06 +02:00
|
|
|
const w1 = (t1.outcome === 'unexpected' ? 1000 : 0) + (t1.outcome === 'flaky' ? 1 : 0);
|
|
|
|
|
const w2 = (t2.outcome === 'unexpected' ? 1000 : 0) + (t2.outcome === 'flaky' ? 1 : 0);
|
|
|
|
|
if (w2 - w1)
|
|
|
|
|
return w2 - w1;
|
|
|
|
|
return t1.location.line - t2.location.line;
|
2021-12-15 19:39:49 +01:00
|
|
|
};
|
|
|
|
|
testFileSummary.tests.sort(testCaseSummaryComparator);
|
2021-08-05 22:36:47 +02:00
|
|
|
|
2021-11-02 00:14:52 +01:00
|
|
|
this._addDataFile(fileId + '.json', testFile);
|
2021-09-14 22:55:31 +02:00
|
|
|
}
|
2021-10-18 05:58:06 +02:00
|
|
|
const htmlReport: HTMLReport = {
|
2022-05-03 01:28:14 +02:00
|
|
|
metadata,
|
2021-10-18 05:58:06 +02:00
|
|
|
files: [...data.values()].map(e => e.testFileSummary),
|
2021-10-18 17:03:04 +02:00
|
|
|
projectNames: rawReports.map(r => r.project.name),
|
2022-12-20 23:13:10 +01:00
|
|
|
stats: { ...[...data.values()].reduce((a, e) => addStats(a, e.testFileSummary.stats), emptyStats()), duration: metadata.duration }
|
2021-08-05 22:36:47 +02:00
|
|
|
};
|
2021-10-18 05:58:06 +02:00
|
|
|
htmlReport.files.sort((f1, f2) => {
|
|
|
|
|
const w1 = f1.stats.unexpected * 1000 + f1.stats.flaky;
|
|
|
|
|
const w2 = f2.stats.unexpected * 1000 + f2.stats.flaky;
|
|
|
|
|
return w2 - w1;
|
|
|
|
|
});
|
2021-10-18 17:03:04 +02:00
|
|
|
|
2021-11-02 00:14:52 +01:00
|
|
|
this._addDataFile('report.json', htmlReport);
|
2021-10-18 17:03:04 +02:00
|
|
|
|
|
|
|
|
// Copy app.
|
|
|
|
|
const appFolder = path.join(require.resolve('playwright-core'), '..', 'lib', 'webpack', 'htmlReport');
|
2022-11-13 21:46:35 +01:00
|
|
|
await copyFileAndMakeWritable(path.join(appFolder, 'index.html'), path.join(this._reportFolder, 'index.html'));
|
2021-10-18 17:03:04 +02:00
|
|
|
|
|
|
|
|
// Copy trace viewer.
|
|
|
|
|
if (this._hasTraces) {
|
|
|
|
|
const traceViewerFolder = path.join(require.resolve('playwright-core'), '..', 'lib', 'webpack', 'traceViewer');
|
|
|
|
|
const traceViewerTargetFolder = path.join(this._reportFolder, 'trace');
|
|
|
|
|
fs.mkdirSync(traceViewerTargetFolder, { recursive: true });
|
|
|
|
|
for (const file of fs.readdirSync(traceViewerFolder)) {
|
|
|
|
|
if (file.endsWith('.map'))
|
|
|
|
|
continue;
|
2022-11-13 21:46:35 +01:00
|
|
|
await copyFileAndMakeWritable(path.join(traceViewerFolder, file), path.join(traceViewerTargetFolder, file));
|
2021-10-18 17:03:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 00:14:52 +01:00
|
|
|
// Inline report data.
|
|
|
|
|
const indexFile = path.join(this._reportFolder, 'index.html');
|
|
|
|
|
fs.appendFileSync(indexFile, '<script>\nwindow.playwrightReportBase64 = "data:application/zip;base64,');
|
|
|
|
|
await new Promise(f => {
|
|
|
|
|
this._dataZipFile!.end(undefined, () => {
|
|
|
|
|
this._dataZipFile!.outputStream
|
|
|
|
|
.pipe(new Base64Encoder())
|
|
|
|
|
.pipe(fs.createWriteStream(indexFile, { flags: 'a' })).on('close', f);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
fs.appendFileSync(indexFile, '";</script>');
|
|
|
|
|
|
2021-11-01 17:54:53 +01:00
|
|
|
let singleTestId: string | undefined;
|
|
|
|
|
if (htmlReport.stats.total === 1) {
|
|
|
|
|
const testFile: TestFile = data.values().next().value.testFile;
|
|
|
|
|
singleTestId = testFile.tests[0].testId;
|
|
|
|
|
}
|
2021-11-02 00:14:52 +01:00
|
|
|
|
2021-11-01 17:54:53 +01:00
|
|
|
return { ok, singleTestId };
|
2021-08-05 22:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-02 00:14:52 +01:00
|
|
|
private _addDataFile(fileName: string, data: any) {
|
|
|
|
|
this._dataZipFile.addBuffer(Buffer.from(JSON.stringify(data)), fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 04:08:31 +01:00
|
|
|
private _processJsonSuite(suite: JsonSuite, fileId: string, projectName: string, path: string[], outTests: TestEntry[]) {
|
2021-10-18 05:58:06 +02:00
|
|
|
const newPath = [...path, suite.title];
|
2022-03-09 04:08:31 +01:00
|
|
|
suite.suites.map(s => this._processJsonSuite(s, fileId, projectName, newPath, outTests));
|
2021-12-15 19:39:49 +01:00
|
|
|
suite.tests.forEach(t => outTests.push(this._createTestEntry(t, projectName, newPath)));
|
2021-10-18 05:58:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-01 18:53:42 +01:00
|
|
|
private _createTestEntry(test: JsonTestCase, projectName: string, path: string[]): TestEntry {
|
2021-09-14 05:34:46 +02:00
|
|
|
const duration = test.results.reduce((a, r) => a + r.duration, 0);
|
|
|
|
|
this._tests.set(test.testId, test);
|
2021-10-18 22:34:02 +02:00
|
|
|
const location = test.location;
|
2021-11-01 18:53:42 +01:00
|
|
|
path = [...path.slice(1)];
|
2021-10-18 05:58:06 +02:00
|
|
|
this._testPath.set(test.testId, path);
|
|
|
|
|
|
2022-07-12 04:47:15 +02:00
|
|
|
const results = test.results.map(r => this._createTestResult(r));
|
|
|
|
|
|
2021-08-05 22:36:47 +02:00
|
|
|
return {
|
2021-10-18 05:58:06 +02:00
|
|
|
testCase: {
|
|
|
|
|
testId: test.testId,
|
|
|
|
|
title: test.title,
|
|
|
|
|
projectName,
|
|
|
|
|
location,
|
|
|
|
|
duration,
|
2021-12-08 03:35:06 +01:00
|
|
|
annotations: test.annotations,
|
2021-10-18 05:58:06 +02:00
|
|
|
outcome: test.outcome,
|
|
|
|
|
path,
|
2022-07-12 04:47:15 +02:00
|
|
|
results,
|
2021-10-18 05:58:06 +02:00
|
|
|
ok: test.outcome === 'expected' || test.outcome === 'flaky',
|
|
|
|
|
},
|
|
|
|
|
testCaseSummary: {
|
|
|
|
|
testId: test.testId,
|
|
|
|
|
title: test.title,
|
|
|
|
|
projectName,
|
|
|
|
|
location,
|
|
|
|
|
duration,
|
2021-12-08 03:35:06 +01:00
|
|
|
annotations: test.annotations,
|
2021-10-18 05:58:06 +02:00
|
|
|
outcome: test.outcome,
|
|
|
|
|
path,
|
|
|
|
|
ok: test.outcome === 'expected' || test.outcome === 'flaky',
|
2022-07-12 04:47:15 +02:00
|
|
|
results: results.map(result => {
|
|
|
|
|
return { attachments: result.attachments.map(a => ({ name: a.name, contentType: a.contentType, path: a.path })) };
|
|
|
|
|
}),
|
2021-10-18 05:58:06 +02:00
|
|
|
},
|
2021-08-05 22:36:47 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 00:28:04 +01:00
|
|
|
private _serializeAttachments(attachments: JsonAttachment[]) {
|
2021-10-16 01:15:06 +02:00
|
|
|
let lastAttachment: TestAttachment | undefined;
|
2022-03-23 00:28:04 +01:00
|
|
|
return attachments.map(a => {
|
|
|
|
|
if (a.name === 'trace')
|
|
|
|
|
this._hasTraces = true;
|
|
|
|
|
|
|
|
|
|
if ((a.name === 'stdout' || a.name === 'stderr') && a.contentType === 'text/plain') {
|
|
|
|
|
if (lastAttachment &&
|
|
|
|
|
lastAttachment.name === a.name &&
|
|
|
|
|
lastAttachment.contentType === a.contentType) {
|
|
|
|
|
lastAttachment.body += stripAnsiEscapes(a.body as string);
|
|
|
|
|
return null;
|
2021-12-08 17:51:44 +01:00
|
|
|
}
|
2022-03-23 00:28:04 +01:00
|
|
|
a.body = stripAnsiEscapes(a.body as string);
|
|
|
|
|
lastAttachment = a as TestAttachment;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
2021-12-08 17:51:44 +01:00
|
|
|
|
2022-03-23 00:28:04 +01:00
|
|
|
if (a.path) {
|
|
|
|
|
let fileName = a.path;
|
|
|
|
|
try {
|
|
|
|
|
const buffer = fs.readFileSync(a.path);
|
|
|
|
|
const sha1 = calculateSha1(buffer) + path.extname(a.path);
|
|
|
|
|
fileName = 'data/' + sha1;
|
|
|
|
|
fs.mkdirSync(path.join(this._reportFolder, 'data'), { recursive: true });
|
|
|
|
|
fs.writeFileSync(path.join(this._reportFolder, 'data', sha1), buffer);
|
|
|
|
|
} catch (e) {
|
2021-09-15 01:26:31 +02:00
|
|
|
return {
|
2022-03-23 00:28:04 +01:00
|
|
|
name: `Missing attachment "${a.name}"`,
|
|
|
|
|
contentType: kMissingContentType,
|
|
|
|
|
body: `Attachment file ${fileName} is missing`,
|
2021-09-15 01:26:31 +02:00
|
|
|
};
|
|
|
|
|
}
|
2022-03-23 00:28:04 +01:00
|
|
|
return {
|
|
|
|
|
name: a.name,
|
|
|
|
|
contentType: a.contentType,
|
|
|
|
|
path: fileName,
|
|
|
|
|
body: a.body,
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-10-16 01:15:06 +02:00
|
|
|
|
2022-03-23 00:28:04 +01:00
|
|
|
if (a.body instanceof Buffer) {
|
|
|
|
|
if (isTextContentType(a.contentType)) {
|
|
|
|
|
// Content type is like this: "text/html; charset=UTF-8"
|
|
|
|
|
const charset = a.contentType.match(/charset=(.*)/)?.[1];
|
|
|
|
|
try {
|
|
|
|
|
const body = a.body.toString(charset as any || 'utf-8');
|
|
|
|
|
return {
|
|
|
|
|
name: a.name,
|
|
|
|
|
contentType: a.contentType,
|
|
|
|
|
body,
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Invalid encoding, fall through and save to file.
|
2021-11-02 00:39:54 +01:00
|
|
|
}
|
2021-10-16 01:15:06 +02:00
|
|
|
}
|
2021-12-08 17:51:44 +01:00
|
|
|
|
2022-03-23 00:28:04 +01:00
|
|
|
fs.mkdirSync(path.join(this._reportFolder, 'data'), { recursive: true });
|
2022-06-17 17:09:49 +02:00
|
|
|
const extension = sanitizeForFilePath(path.extname(a.name).replace(/^\./, '')) || mime.getExtension(a.contentType) || 'dat';
|
|
|
|
|
const sha1 = calculateSha1(a.body) + '.' + extension;
|
2022-03-23 00:28:04 +01:00
|
|
|
fs.writeFileSync(path.join(this._reportFolder, 'data', sha1), a.body);
|
2021-12-08 17:51:44 +01:00
|
|
|
return {
|
|
|
|
|
name: a.name,
|
|
|
|
|
contentType: a.contentType,
|
2022-03-23 00:28:04 +01:00
|
|
|
path: 'data/' + sha1,
|
2021-12-08 17:51:44 +01:00
|
|
|
};
|
2022-03-23 00:28:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// string
|
|
|
|
|
return {
|
|
|
|
|
name: a.name,
|
|
|
|
|
contentType: a.contentType,
|
|
|
|
|
body: a.body,
|
|
|
|
|
};
|
|
|
|
|
}).filter(Boolean) as TestAttachment[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _createTestResult(result: JsonTestResult): TestResult {
|
|
|
|
|
return {
|
|
|
|
|
duration: result.duration,
|
|
|
|
|
startTime: result.startTime,
|
|
|
|
|
retry: result.retry,
|
|
|
|
|
steps: result.steps.map(s => this._createTestStep(s)),
|
|
|
|
|
errors: result.errors,
|
|
|
|
|
status: result.status,
|
|
|
|
|
attachments: this._serializeAttachments(result.attachments),
|
2021-08-05 22:36:47 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 05:34:46 +02:00
|
|
|
private _createTestStep(step: JsonTestStep): TestStep {
|
2021-08-11 02:06:25 +02:00
|
|
|
return {
|
2021-09-14 05:34:46 +02:00
|
|
|
title: step.title,
|
|
|
|
|
startTime: step.startTime,
|
|
|
|
|
duration: step.duration,
|
2021-10-19 07:14:01 +02:00
|
|
|
snippet: step.snippet,
|
2021-09-14 05:34:46 +02:00
|
|
|
steps: step.steps.map(s => this._createTestStep(s)),
|
2021-10-19 07:14:01 +02:00
|
|
|
location: step.location,
|
2022-01-04 06:17:17 +01:00
|
|
|
error: step.error,
|
|
|
|
|
count: step.count
|
2021-08-11 02:06:25 +02:00
|
|
|
};
|
2021-08-08 00:47:03 +02:00
|
|
|
}
|
2021-09-01 01:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-14 22:55:31 +02:00
|
|
|
const emptyStats = (): Stats => {
|
|
|
|
|
return {
|
|
|
|
|
total: 0,
|
|
|
|
|
expected: 0,
|
|
|
|
|
unexpected: 0,
|
|
|
|
|
flaky: 0,
|
|
|
|
|
skipped: 0,
|
2021-10-18 05:58:06 +02:00
|
|
|
ok: true,
|
|
|
|
|
duration: 0,
|
2021-09-14 22:55:31 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-18 17:03:04 +02:00
|
|
|
const addStats = (stats: Stats, delta: Stats): Stats => {
|
|
|
|
|
stats.total += delta.total;
|
|
|
|
|
stats.skipped += delta.skipped;
|
|
|
|
|
stats.expected += delta.expected;
|
|
|
|
|
stats.unexpected += delta.unexpected;
|
|
|
|
|
stats.flaky += delta.flaky;
|
|
|
|
|
stats.ok = stats.ok && delta.ok;
|
|
|
|
|
stats.duration += delta.duration;
|
|
|
|
|
return stats;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-02 00:14:52 +01:00
|
|
|
class Base64Encoder extends Transform {
|
|
|
|
|
private _remainder: Buffer | undefined;
|
|
|
|
|
|
|
|
|
|
override _transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void {
|
|
|
|
|
if (this._remainder) {
|
|
|
|
|
chunk = Buffer.concat([this._remainder, chunk]);
|
|
|
|
|
this._remainder = undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const remaining = chunk.length % 3;
|
|
|
|
|
if (remaining) {
|
|
|
|
|
this._remainder = chunk.slice(chunk.length - remaining);
|
|
|
|
|
chunk = chunk.slice(0, chunk.length - remaining);
|
|
|
|
|
}
|
|
|
|
|
chunk = chunk.toString('base64');
|
|
|
|
|
this.push(Buffer.from(chunk));
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override _flush(callback: TransformCallback): void {
|
|
|
|
|
if (this._remainder)
|
|
|
|
|
this.push(Buffer.from(this._remainder.toString('base64')));
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 17:51:44 +01:00
|
|
|
function isTextContentType(contentType: string) {
|
|
|
|
|
return contentType.startsWith('text/') || contentType.startsWith('application/json');
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 22:36:47 +02:00
|
|
|
export default HtmlReporter;
|