feat(html reporter): open test details page when running a single test (#9910)

This commit is contained in:
Dmitry Gozman 2021-11-01 09:54:53 -07:00 committed by GitHub
parent ff8494c7e3
commit 13844a5b0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -128,14 +128,14 @@ class HtmlReporter {
const reportFolder = htmlReportFolder(this._outputFolder); const reportFolder = htmlReportFolder(this._outputFolder);
await removeFolders([reportFolder]); await removeFolders([reportFolder]);
const builder = new HtmlBuilder(reportFolder, this.config.rootDir); const builder = new HtmlBuilder(reportFolder, this.config.rootDir);
const ok = builder.build(reports); const { ok, singleTestId } = builder.build(reports);
if (process.env.PWTEST_SKIP_TEST_OUTPUT || process.env.CI) if (process.env.PWTEST_SKIP_TEST_OUTPUT || process.env.CI)
return; return;
const shouldOpen = this._open === 'always' || (!ok && this._open === 'on-failure'); const shouldOpen = this._open === 'always' || (!ok && this._open === 'on-failure');
if (shouldOpen) { if (shouldOpen) {
await showHTMLReport(reportFolder); await showHTMLReport(reportFolder, singleTestId);
} else { } else {
console.log(''); console.log('');
console.log('To open last HTML report run:'); console.log('To open last HTML report run:');
@ -154,7 +154,7 @@ export function htmlReportFolder(outputFolder?: string): string {
return path.resolve(process.cwd(), 'playwright-report'); return path.resolve(process.cwd(), 'playwright-report');
} }
export async function showHTMLReport(reportFolder: string | undefined) { export async function showHTMLReport(reportFolder: string | undefined, testId?: string) {
const folder = reportFolder || htmlReportFolder(); const folder = reportFolder || htmlReportFolder();
try { try {
assert(fs.statSync(folder).isDirectory()); assert(fs.statSync(folder).isDirectory());
@ -164,9 +164,11 @@ export async function showHTMLReport(reportFolder: string | undefined) {
return; return;
} }
const server = startHtmlReportServer(folder); const server = startHtmlReportServer(folder);
const url = await server.start(9323); let url = await server.start(9323);
console.log(''); console.log('');
console.log(colors.cyan(` Serving HTML report at ${url}. Press Ctrl+C to quit.`)); console.log(colors.cyan(` Serving HTML report at ${url}. Press Ctrl+C to quit.`));
if (testId)
url += `#?testId=${testId}`;
open(url); open(url);
process.on('SIGINT', () => process.exit(0)); process.on('SIGINT', () => process.exit(0));
await new Promise(() => {}); await new Promise(() => {});
@ -204,7 +206,7 @@ class HtmlBuilder {
this._dataFolder = path.join(this._reportFolder, 'data'); this._dataFolder = path.join(this._reportFolder, 'data');
} }
build(rawReports: JsonReport[]): boolean { build(rawReports: JsonReport[]): { ok: boolean, singleTestId: string | undefined } {
fs.mkdirSync(this._dataFolder, { recursive: true }); fs.mkdirSync(this._dataFolder, { recursive: true });
const data = new Map<string, { testFile: TestFile, testFileSummary: TestFileSummary }>(); const data = new Map<string, { testFile: TestFile, testFileSummary: TestFileSummary }>();
@ -292,7 +294,12 @@ class HtmlBuilder {
} }
} }
return ok; let singleTestId: string | undefined;
if (htmlReport.stats.total === 1) {
const testFile: TestFile = data.values().next().value.testFile;
singleTestId = testFile.tests[0].testId;
}
return { ok, singleTestId };
} }
private _processJsonSuite(suite: JsonSuite, fileId: string, projectName: string, path: string[], out: TestEntry[]) { private _processJsonSuite(suite: JsonSuite, fileId: string, projectName: string, path: string[], out: TestEntry[]) {