2021-08-05 13:36:47 -07: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.
|
|
|
|
*/
|
|
|
|
|
2021-09-14 13:55:31 -07:00
|
|
|
import colors from 'colors/safe';
|
2021-08-05 13:36:47 -07:00
|
|
|
import fs from 'fs';
|
2021-09-14 13:55:31 -07:00
|
|
|
import open from 'open';
|
2021-08-05 13:36:47 -07:00
|
|
|
import path from 'path';
|
2021-10-14 05:55:08 -04:00
|
|
|
import { FullConfig, Suite } from '../../types/testReporter';
|
2021-10-11 10:52:17 -04:00
|
|
|
import { HttpServer } from 'playwright-core/src/utils/httpServer';
|
|
|
|
import { calculateSha1, removeFolders } from 'playwright-core/src/utils/utils';
|
|
|
|
import { toPosixPath } from './json';
|
2021-09-14 16:26:31 -07:00
|
|
|
import RawReporter, { JsonReport, JsonSuite, JsonTestCase, JsonTestResult, JsonTestStep, JsonAttachment } from './raw';
|
2021-08-05 13:36:47 -07:00
|
|
|
|
2021-09-14 13:55:31 -07:00
|
|
|
export type Stats = {
|
|
|
|
total: number;
|
|
|
|
expected: number;
|
|
|
|
unexpected: number;
|
|
|
|
flaky: number;
|
|
|
|
skipped: number;
|
|
|
|
ok: boolean;
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type Location = {
|
|
|
|
file: string;
|
|
|
|
line: number;
|
|
|
|
column: number;
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type ProjectTreeItem = {
|
|
|
|
name: string;
|
|
|
|
suites: SuiteTreeItem[];
|
2021-09-14 13:55:31 -07:00
|
|
|
stats: Stats;
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type SuiteTreeItem = {
|
2021-08-05 13:36:47 -07:00
|
|
|
title: string;
|
2021-09-13 20:34:46 -07:00
|
|
|
location?: Location;
|
|
|
|
duration: number;
|
|
|
|
suites: SuiteTreeItem[];
|
|
|
|
tests: TestTreeItem[];
|
2021-09-14 13:55:31 -07:00
|
|
|
stats: Stats;
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type TestTreeItem = {
|
|
|
|
testId: string,
|
|
|
|
fileId: string,
|
2021-08-05 13:36:47 -07:00
|
|
|
title: string;
|
2021-09-13 20:34:46 -07:00
|
|
|
location: Location;
|
|
|
|
duration: number;
|
2021-08-05 13:36:47 -07:00
|
|
|
outcome: 'skipped' | 'expected' | 'unexpected' | 'flaky';
|
2021-09-14 13:55:31 -07:00
|
|
|
ok: boolean;
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
|
2021-09-14 16:26:31 -07:00
|
|
|
export type TestAttachment = JsonAttachment;
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type TestFile = {
|
|
|
|
fileId: string;
|
|
|
|
path: string;
|
|
|
|
tests: TestCase[];
|
2021-08-07 15:47:03 -07:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type TestCase = {
|
|
|
|
testId: string,
|
|
|
|
title: string;
|
|
|
|
location: Location;
|
|
|
|
results: TestResult[];
|
2021-08-07 15:47:03 -07:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type TestResult = {
|
2021-08-05 13:36:47 -07:00
|
|
|
retry: number;
|
|
|
|
startTime: string;
|
|
|
|
duration: number;
|
2021-09-13 20:34:46 -07:00
|
|
|
steps: TestStep[];
|
|
|
|
error?: string;
|
2021-09-14 16:26:31 -07:00
|
|
|
attachments: TestAttachment[];
|
2021-09-13 20:34:46 -07:00
|
|
|
status: 'passed' | 'failed' | 'timedOut' | 'skipped';
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
export type TestStep = {
|
2021-08-05 13:36:47 -07:00
|
|
|
title: string;
|
|
|
|
startTime: string;
|
|
|
|
duration: number;
|
2021-09-03 13:08:17 -07:00
|
|
|
log?: string[];
|
2021-09-13 20:34:46 -07:00
|
|
|
error?: string;
|
|
|
|
steps: TestStep[];
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
|
2021-08-07 15:47:03 -07:00
|
|
|
class HtmlReporter {
|
|
|
|
private config!: FullConfig;
|
|
|
|
private suite!: Suite;
|
|
|
|
|
2021-08-10 17:06:25 -07:00
|
|
|
onBegin(config: FullConfig, suite: Suite) {
|
|
|
|
this.config = config;
|
|
|
|
this.suite = suite;
|
2021-08-07 15:47:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async onEnd() {
|
2021-09-13 20:34:46 -07:00
|
|
|
const projectSuites = this.suite.suites;
|
|
|
|
const reports = projectSuites.map(suite => {
|
|
|
|
const rawReporter = new RawReporter();
|
|
|
|
const report = rawReporter.generateProjectReport(this.config, suite);
|
|
|
|
return report;
|
2021-08-07 15:47:03 -07:00
|
|
|
});
|
2021-09-13 20:34:46 -07:00
|
|
|
const reportFolder = path.resolve(process.cwd(), process.env[`PLAYWRIGHT_HTML_REPORT`] || 'playwright-report');
|
2021-09-14 13:55:31 -07:00
|
|
|
await removeFolders([reportFolder]);
|
2021-09-13 20:34:46 -07:00
|
|
|
new HtmlBuilder(reports, reportFolder, this.config.rootDir);
|
2021-09-14 13:55:31 -07:00
|
|
|
|
2021-09-14 16:26:31 -07:00
|
|
|
if (!process.env.CI && !process.env.PWTEST_SKIP_TEST_OUTPUT) {
|
2021-09-14 13:55:31 -07:00
|
|
|
const server = new HttpServer();
|
|
|
|
server.routePrefix('/', (request, response) => {
|
2021-10-13 10:07:29 -08:00
|
|
|
let relativePath = new URL('http://localhost' + request.url).pathname;
|
2021-09-14 13:55:31 -07:00
|
|
|
if (relativePath === '/')
|
|
|
|
relativePath = '/index.html';
|
|
|
|
const absolutePath = path.join(reportFolder, ...relativePath.split('/'));
|
|
|
|
return server.serveFile(response, absolutePath);
|
|
|
|
});
|
2021-10-13 12:31:54 -08:00
|
|
|
const url = await server.start(9323);
|
2021-09-14 13:55:31 -07:00
|
|
|
console.log('');
|
|
|
|
console.log(colors.cyan(` Serving HTML report at ${url}. Press Ctrl+C to quit.`));
|
|
|
|
console.log('');
|
|
|
|
open(url);
|
|
|
|
process.on('SIGINT', () => process.exit(0));
|
|
|
|
await new Promise(() => {});
|
|
|
|
}
|
2021-08-05 13:36:47 -07:00
|
|
|
}
|
2021-09-13 20:34:46 -07:00
|
|
|
}
|
2021-08-05 13:36:47 -07:00
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
class HtmlBuilder {
|
|
|
|
private _reportFolder: string;
|
|
|
|
private _tests = new Map<string, JsonTestCase>();
|
|
|
|
private _rootDir: string;
|
2021-09-14 16:26:31 -07:00
|
|
|
private _dataFolder: string;
|
2021-09-13 20:34:46 -07:00
|
|
|
|
|
|
|
constructor(rawReports: JsonReport[], outputDir: string, rootDir: string) {
|
|
|
|
this._rootDir = rootDir;
|
|
|
|
this._reportFolder = path.resolve(process.cwd(), outputDir);
|
2021-09-14 16:26:31 -07:00
|
|
|
this._dataFolder = path.join(this._reportFolder, 'data');
|
|
|
|
fs.mkdirSync(this._dataFolder, { recursive: true });
|
2021-10-13 10:07:29 -08:00
|
|
|
|
|
|
|
// Copy app.
|
2021-10-13 18:27:50 -08:00
|
|
|
const appFolder = path.join(require.resolve('playwright-core'), '..', 'lib', 'webpack', 'htmlReport');
|
2021-09-13 20:34:46 -07:00
|
|
|
for (const file of fs.readdirSync(appFolder))
|
|
|
|
fs.copyFileSync(path.join(appFolder, file), path.join(this._reportFolder, file));
|
|
|
|
|
2021-10-13 10:07:29 -08:00
|
|
|
// Copy trace viewer.
|
2021-10-13 18:27:50 -08:00
|
|
|
const traceViewerFolder = path.join(require.resolve('playwright-core'), '..', 'lib', 'webpack', 'traceViewer');
|
2021-10-13 10:07:29 -08:00
|
|
|
const traceViewerTargetFolder = path.join(this._reportFolder, 'trace');
|
|
|
|
fs.mkdirSync(traceViewerTargetFolder, { recursive: true });
|
|
|
|
// TODO (#9471): remove file filter when the babel build is fixed.
|
|
|
|
for (const file of fs.readdirSync(traceViewerFolder)) {
|
|
|
|
if (fs.statSync(path.join(traceViewerFolder, file)).isFile())
|
|
|
|
fs.copyFileSync(path.join(traceViewerFolder, file), path.join(traceViewerTargetFolder, file));
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
const projects: ProjectTreeItem[] = [];
|
|
|
|
for (const projectJson of rawReports) {
|
|
|
|
const suites: SuiteTreeItem[] = [];
|
|
|
|
for (const file of projectJson.suites) {
|
|
|
|
const relativeFileName = this._relativeLocation(file.location).file;
|
|
|
|
const fileId = calculateSha1(projectJson.project.name + ':' + relativeFileName);
|
|
|
|
const tests: JsonTestCase[] = [];
|
|
|
|
suites.push(this._createSuiteTreeItem(file, fileId, tests));
|
|
|
|
const testFile: TestFile = {
|
|
|
|
fileId,
|
|
|
|
path: relativeFileName,
|
|
|
|
tests: tests.map(t => this._createTestCase(t))
|
|
|
|
};
|
2021-09-14 16:26:31 -07:00
|
|
|
fs.writeFileSync(path.join(this._dataFolder, fileId + '.json'), JSON.stringify(testFile, undefined, 2));
|
2021-09-13 20:34:46 -07:00
|
|
|
}
|
|
|
|
projects.push({
|
|
|
|
name: projectJson.project.name,
|
|
|
|
suites,
|
2021-09-14 13:55:31 -07:00
|
|
|
stats: suites.reduce((a, s) => addStats(a, s.stats), emptyStats()),
|
2021-09-13 20:34:46 -07:00
|
|
|
});
|
|
|
|
}
|
2021-09-14 16:26:31 -07:00
|
|
|
fs.writeFileSync(path.join(this._dataFolder, 'projects.json'), JSON.stringify(projects, undefined, 2));
|
2021-09-13 20:34:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private _createTestCase(test: JsonTestCase): TestCase {
|
2021-08-05 13:36:47 -07:00
|
|
|
return {
|
2021-09-13 20:34:46 -07:00
|
|
|
testId: test.testId,
|
|
|
|
title: test.title,
|
|
|
|
location: this._relativeLocation(test.location),
|
2021-09-14 16:26:31 -07:00
|
|
|
results: test.results.map(r => this._createTestResult(test, r))
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
private _createSuiteTreeItem(suite: JsonSuite, fileId: string, testCollector: JsonTestCase[]): SuiteTreeItem {
|
|
|
|
const suites = suite.suites.map(s => this._createSuiteTreeItem(s, fileId, testCollector));
|
|
|
|
const tests = suite.tests.map(t => this._createTestTreeItem(t, fileId));
|
|
|
|
testCollector.push(...suite.tests);
|
2021-09-14 13:55:31 -07:00
|
|
|
const stats = suites.reduce<Stats>((a, s) => addStats(a, s.stats), emptyStats());
|
|
|
|
for (const test of tests) {
|
|
|
|
if (test.outcome === 'expected')
|
|
|
|
++stats.expected;
|
2021-09-14 16:26:31 -07:00
|
|
|
if (test.outcome === 'skipped')
|
|
|
|
++stats.skipped;
|
2021-09-14 13:55:31 -07:00
|
|
|
if (test.outcome === 'unexpected')
|
|
|
|
++stats.unexpected;
|
|
|
|
if (test.outcome === 'flaky')
|
|
|
|
++stats.flaky;
|
|
|
|
++stats.total;
|
|
|
|
}
|
|
|
|
stats.ok = stats.unexpected + stats.flaky === 0;
|
2021-08-05 13:36:47 -07:00
|
|
|
return {
|
|
|
|
title: suite.title,
|
|
|
|
location: this._relativeLocation(suite.location),
|
2021-09-13 20:34:46 -07:00
|
|
|
duration: suites.reduce((a, s) => a + s.duration, 0) + tests.reduce((a, t) => a + t.duration, 0),
|
2021-09-14 13:55:31 -07:00
|
|
|
stats,
|
2021-09-13 20:34:46 -07:00
|
|
|
suites,
|
|
|
|
tests
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
private _createTestTreeItem(test: JsonTestCase, fileId: string): TestTreeItem {
|
|
|
|
const duration = test.results.reduce((a, r) => a + r.duration, 0);
|
|
|
|
this._tests.set(test.testId, test);
|
2021-08-05 13:36:47 -07:00
|
|
|
return {
|
2021-09-13 20:34:46 -07:00
|
|
|
testId: test.testId,
|
|
|
|
fileId: fileId,
|
2021-08-05 13:36:47 -07:00
|
|
|
location: this._relativeLocation(test.location),
|
2021-09-13 20:34:46 -07:00
|
|
|
title: test.title,
|
|
|
|
duration,
|
2021-09-14 13:55:31 -07:00
|
|
|
outcome: test.outcome,
|
|
|
|
ok: test.ok
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:26:31 -07:00
|
|
|
private _createTestResult(test: JsonTestCase, result: JsonTestResult): TestResult {
|
2021-08-05 13:36:47 -07:00
|
|
|
return {
|
|
|
|
duration: result.duration,
|
2021-09-13 20:34:46 -07:00
|
|
|
startTime: result.startTime,
|
|
|
|
retry: result.retry,
|
|
|
|
steps: result.steps.map(s => this._createTestStep(s)),
|
2021-09-14 13:55:31 -07:00
|
|
|
error: result.error,
|
2021-08-05 13:36:47 -07:00
|
|
|
status: result.status,
|
2021-09-14 16:26:31 -07:00
|
|
|
attachments: result.attachments.map(a => {
|
|
|
|
if (a.path) {
|
|
|
|
const fileName = 'data/' + test.testId + path.extname(a.path);
|
|
|
|
try {
|
|
|
|
fs.copyFileSync(a.path, path.join(this._reportFolder, fileName));
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: a.name,
|
|
|
|
contentType: a.contentType,
|
|
|
|
path: fileName,
|
|
|
|
body: a.body,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
})
|
2021-08-05 13:36:47 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
private _createTestStep(step: JsonTestStep): TestStep {
|
2021-08-10 17:06:25 -07:00
|
|
|
return {
|
2021-09-13 20:34:46 -07:00
|
|
|
title: step.title,
|
|
|
|
startTime: step.startTime,
|
|
|
|
duration: step.duration,
|
|
|
|
steps: step.steps.map(s => this._createTestStep(s)),
|
|
|
|
log: step.log,
|
2021-09-14 13:55:31 -07:00
|
|
|
error: step.error
|
2021-08-10 17:06:25 -07:00
|
|
|
};
|
2021-08-07 15:47:03 -07:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:34:46 -07:00
|
|
|
private _relativeLocation(location: Location | undefined): Location {
|
|
|
|
if (!location)
|
|
|
|
return { file: '', line: 0, column: 0 };
|
|
|
|
return {
|
|
|
|
file: toPosixPath(path.relative(this._rootDir, location.file)),
|
|
|
|
line: location.line,
|
|
|
|
column: location.column,
|
|
|
|
};
|
2021-08-31 16:34:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:55:31 -07:00
|
|
|
const emptyStats = (): Stats => {
|
|
|
|
return {
|
|
|
|
total: 0,
|
|
|
|
expected: 0,
|
|
|
|
unexpected: 0,
|
|
|
|
flaky: 0,
|
|
|
|
skipped: 0,
|
|
|
|
ok: true
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
return stats;
|
|
|
|
};
|
|
|
|
|
2021-08-05 13:36:47 -07:00
|
|
|
export default HtmlReporter;
|