Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(html): allow hiding files in html report (#37482)

Pavel Feldman8a91226126ef
diff --git a/docs/src/test-reporters-js.md b/docs/src/test-reporters-js.md
index 973cc1d641..ed50f672af 100644
--- a/docs/src/test-reporters-js.md
+++ b/docs/src/test-reporters-js.md
@@ -253,6 +253,7 @@ HTML report supports the following configuration options and environment variabl
| `PLAYWRIGHT_HTML_PORT` | `port` | When report opens in the browser, it will be served on this port. | `9323` or any available port when `9323` is not available.
| `PLAYWRIGHT_HTML_ATTACHMENTS_BASE_URL` | `attachmentsBaseURL` | A separate location where attachments from the `data` subdirectory are uploaded. Only needed when you upload report and `data` separately to different locations. | `data/`
| `PLAYWRIGHT_HTML_NO_COPY_PROMPT` | `noCopyPrompt` | If true, disable rendering of the Copy prompt for errors. Supports `true`, `1`, `false`, and `0`. | `false`
+| `PLAYWRIGHT_HTML_NO_FILES` | `noFiles` | If true, omit file-based grouping and render describe suites on the top. Supports `true`, `1`, `false`, and `0`. | `false`
| `PLAYWRIGHT_HTML_NO_SNIPPETS` | `noSnippets` | If true, disable rendering code snippets in the action log. If there is a top level error, that report section with code snippet will still render. Supports `true`, `1`, `false`, and `0`. | `false`
### Blob reporter
diff --git a/packages/html-reporter/src/types.d.ts b/packages/html-reporter/src/types.d.ts
index 270a8cb983..e6aebdc31b 100644
--- a/packages/html-reporter/src/types.d.ts
+++ b/packages/html-reporter/src/types.d.ts
@@ -40,6 +40,7 @@ export type HTMLReportOptions = {
title?: string;
noCopyPrompt?: boolean;
noSnippets?: boolean;
+ noFiles?: boolean;
};
export type HTMLReport = {
diff --git a/packages/playwright/src/reporters/html.ts b/packages/playwright/src/reporters/html.ts
index 04be226d86..4040ffc187 100644
--- a/packages/playwright/src/reporters/html.ts
+++ b/packages/playwright/src/reporters/html.ts
@@ -138,10 +138,18 @@ class HtmlReporter implements ReporterV2 {
noCopyPrompt = true;
noCopyPrompt = noCopyPrompt || this._options.noCopyPrompt;
+ let noFiles: boolean | undefined;
+ if (process.env.PLAYWRIGHT_HTML_NO_FILES === 'false' || process.env.PLAYWRIGHT_HTML_NO_FILES === '0')
+ noFiles = false;
+ else if (process.env.PLAYWRIGHT_HTML_NO_FILES)
+ noFiles = true;
+ noFiles = noFiles || this._options.noFiles;
+
const builder = new HtmlBuilder(this.config, this._outputFolder, this._attachmentsBaseURL, {
title: process.env.PLAYWRIGHT_HTML_TITLE || this._options.title,
noSnippets,
noCopyPrompt,
+ noFiles,
});
this._buildResult = await builder.build(this.config.metadata, projectSuites, result, this._topLevelErrors);
}
@@ -232,6 +240,8 @@ export function startHtmlReportServer(folder: string): HttpServer {
return server;
}
+type DataMap = Map<string, { testFile: TestFile, testFileSummary: TestFileSummary }>;
+
class HtmlBuilder {
private _config: api.FullConfig;
private _reportFolder: string;
@@ -251,25 +261,23 @@ class HtmlBuilder {
}
async build(metadata: Metadata, projectSuites: api.Suite[], result: api.FullResult, topLevelErrors: api.TestError[]): Promise<{ ok: boolean, singleTestId: string | undefined }> {
- const data = new Map<string, { testFile: TestFile, testFileSummary: TestFileSummary }>();
+ const data: DataMap = new Map();
for (const projectSuite of projectSuites) {
+ const projectName = projectSuite.project()!.name;
for (const fileSuite of projectSuite.suites) {
- const fileName = this._relativeLocation(fileSuite.location)!.file;
- const fileId = calculateSha1(toPosixPath(fileName)).slice(0, 20);
- let fileEntry = data.get(fileId);
- if (!fileEntry) {
- fileEntry = {
- testFile: { fileId, fileName, tests: [] },
- testFileSummary: { fileId, fileName, tests: [], stats: emptyStats() },
- };
- data.set(fileId, fileEntry);
- }
- const { testFile, testFileSummary } = fileEntry;
- const testEntries: TestEntry[] = [];
- this._processSuite(fileSuite, projectSuite.project()!.name, [], testEntries);
- for (const test of testEntries) {
- testFile.tests.push(test.testCase);
- testFileSummary.tests.push(test.testCaseSummary);
+ if (this._options.noFiles) {
+ for (const describeSuite of fileSuite.suites) {
+ const groupName = describeSuite.title;
+ this._createEntryForSuite(data, projectName, describeSuite, groupName, true);
+ }
+ const hasTestsOutsideGroups = fileSuite.tests.length > 0;
+ if (hasTestsOutsideGroups) {
+ const fileName = '<anonymous>';
+ this._createEntryForSuite(data, projectName, fileSuite, fileName, false);
+ }
+ } else {
+ const fileName = this._relativeLocation(fileSuite.location)!.file;
+ this._createEntryForSuite(data, projectName, fileSuite, fileName, true);
}
}
}
@@ -396,13 +404,33 @@ class HtmlBuilder {
this._dataZipFile.addBuffer(Buffer.from(JSON.stringify(data)), fileName);
}
- private _processSuite(suite: api.Suite, projectName: string, path: string[], outTests: TestEntry[]) {
+ private _createEntryForSuite(data: DataMap, projectName: string, suite: api.Suite, fileName: string, deep: boolean) {
+ const fileId = calculateSha1(fileName).slice(0, 20);
+ let fileEntry = data.get(fileId);
+ if (!fileEntry) {
+ fileEntry = {
+ testFile: { fileId, fileName, tests: [] },
+ testFileSummary: { fileId, fileName, tests: [], stats: emptyStats() },
+ };
+ data.set(fileId, fileEntry);
+ }
+
+ const { testFile, testFileSummary } = fileEntry;
+ const testEntries: TestEntry[] = [];
+ this._processSuite(suite, projectName, [], deep, testEntries);
+ for (const test of testEntries) {
+ testFile.tests.push(test.testCase);
+ testFileSummary.tests.push(test.testCaseSummary);
+ }
+ }
+
+ private _processSuite(suite: api.Suite, projectName: string, path: string[], deep: boolean, outTests: TestEntry[]) {
const newPath = [...path, suite.title];
suite.entries().forEach(e => {
if (e.type === 'test')
outTests.push(this._createTestEntry(e, projectName, newPath));
- else
- this._processSuite(e, projectName, newPath, outTests);
+ else if (deep)
+ this._processSuite(e, projectName, newPath, deep, outTests);
});
}
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index 91c168cd73..aaf187f497 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -31,6 +31,7 @@ export type HtmlReporterOptions = {
title?: string;
noSnippets?: boolean;
noCopyPrompt?: boolean;
+ noFiles?: boolean;
};
export type ReporterDescription = Readonly<
diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts
index 6ee8959a00..ded289bd04 100644
--- a/tests/playwright-test/reporter-html.spec.ts
+++ b/tests/playwright-test/reporter-html.spec.ts
@@ -3204,6 +3204,47 @@ for (const useIntermediateMergeReport of [true, false] as const) {
});
}
+test('should support noFiles option', async ({ runInlineTest, showReport, page }) => {
+ await runInlineTest({
+ 'playwright.config.ts': `
+ import { defineConfig } from '@playwright/test';
+ export default defineConfig({
+ name: 'project-name',
+ reporter: [['html', { noFiles: true }]]
+ });
+ module.exports = { name: 'project-name', reporter: [['html', { noFiles: true }]] };
+ `,
+ 'a.test.js': `
+ import { test, expect } from '@playwright/test';
+ test.describe('describe', () => {
+ test('test 1', async ({}) => {});
+ });
+ test('test 2', async ({}) => {});
+ `,
+ 'b.test.js': `
+ import { test, expect } from '@playwright/test';
+ test.describe('describe', () => {
+ test('test 3', async ({}) => {});
+ });
+ `,
+ }, {}, { PLAYWRIGHT_HTML_OPEN: 'never' });
+
+ await showReport();
+
+ await expect(page.locator('body')).toMatchAriaSnapshot(`
+ - button "describe" [expanded]
+ - region:
+ - link "test 1"
+ - link "a.test.js:4"
+ - link "test 3"
+ - link "b.test.js:4"
+ - button "<anonymous>" [expanded]
+ - region:
+ - link "test 2"
+ - link "a.test.js:6"
+ `);
+});
+
function readAllFromStream(stream: NodeJS.ReadableStream): Promise<Buffer> {
return new Promise(resolve => {
const chunks: Buffer[] = [];
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index 665c7ecd32..7255ea295c 100644
--- a/utils/generate_types/overrides-test.d.ts
+++ b/utils/generate_types/overrides-test.d.ts
@@ -30,6 +30,7 @@ export type HtmlReporterOptions = {
title?: string;
noSnippets?: boolean;
noCopyPrompt?: boolean;
+ noFiles?: boolean;
};
export type ReporterDescription = Readonly<