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-reporter): add doNotInlineAssets option for CSP-compatible reports (#39821)

Yury Semikhatsky56c23302d261
diff --git a/docs/src/test-reporters-js.md b/docs/src/test-reporters-js.md
index 45da71e19d..b5c50d8660 100644
--- a/docs/src/test-reporters-js.md
+++ b/docs/src/test-reporters-js.md
@@ -254,6 +254,7 @@ HTML report supports the following configuration options and environment variabl
| `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_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`
+| `PLAYWRIGHT_HTML_DO_NOT_INLINE_ASSETS` | `doNotInlineAssets` | If true, JavaScript, CSS and report data are written as separate files alongside `index.html` instead of being embedded inline. Use this when serving the report under a strict [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP) that disallows inline scripts and styles. Supports `true`, `1`, `false`, and `0`. | `false`
### Blob reporter
diff --git a/packages/html-reporter/bundle.ts b/packages/html-reporter/bundle.ts
index 63530cfaad..4ca42f6c29 100644
--- a/packages/html-reporter/bundle.ts
+++ b/packages/html-reporter/bundle.ts
@@ -14,41 +14,18 @@
* limitations under the License.
*/
-import fs from 'fs';
-import path from 'path';
-import type { Plugin, UserConfig } from 'vite';
+import type { Plugin } from 'vite';
export function bundle(): Plugin {
- let config: UserConfig;
return {
name: 'playwright-bundle',
- config(c) {
- config = c;
- },
transformIndexHtml: {
handler(html, ctx) {
if (!ctx || !ctx.bundle)
return html;
- html = html.replace(/(?=<!--)([\s\S]*?)-->/, '');
- for (const [name, value] of Object.entries(ctx.bundle)) {
- if (name.endsWith('.map'))
- continue;
- if ('code' in value)
- html = html.replace(/<script type="module".*<\/script>/, () => `<script type="module">${value.code}</script>`);
- else
- html = html.replace(/<link rel="stylesheet"[^>]*>/, () => `<style type='text/css'>${value.source}</style>`);
- }
- return html;
+ // Strip the license comment block.
+ return html.replace(/(?=<!--)([\s\S]*?)-->/, '');
},
},
- closeBundle: () => {
- if (fs.existsSync(path.join(config.build!.outDir!, 'index.html'))) {
- const targetDir = path.join(__dirname, '..', 'playwright-core', 'lib', 'vite', 'htmlReport');
- fs.mkdirSync(targetDir, { recursive: true });
- fs.copyFileSync(
- path.join(config.build!.outDir!, 'index.html'),
- path.join(targetDir, 'index.html'));
- }
- },
};
}
diff --git a/packages/html-reporter/src/index.tsx b/packages/html-reporter/src/index.tsx
index f7263ea865..e113660e21 100644
--- a/packages/html-reporter/src/index.tsx
+++ b/packages/html-reporter/src/index.tsx
@@ -60,7 +60,7 @@ class ZipReport implements LoadedReport {
private _json!: HTMLReport;
async load() {
- const zipURI = document.getElementById('playwrightReportBase64')!.textContent;
+ const zipURI = (document.getElementById('playwrightReportBase64') as HTMLTemplateElement).content.textContent;
const zipReader = new zipjs.ZipReader(new zipjs.Data64URIReader(zipURI), { useWebWorkers: false });
for (const entry of await zipReader.getEntries())
this._entries.set(entry.filename, entry);
diff --git a/packages/html-reporter/vite.config.ts b/packages/html-reporter/vite.config.ts
index c37eb2cd87..fe96d45f1f 100644
--- a/packages/html-reporter/vite.config.ts
+++ b/packages/html-reporter/vite.config.ts
@@ -21,6 +21,7 @@ import path from 'path';
// https://vitejs.dev/config/
export default defineConfig({
+ base: '',
plugins: [
react(),
bundle()
@@ -32,7 +33,7 @@ export default defineConfig({
},
},
build: {
- outDir: path.resolve(__dirname, 'dist'),
+ outDir: path.resolve(__dirname, '../playwright-core/lib/vite/htmlReport'),
emptyOutDir: true,
assetsInlineLimit: 100000000,
chunkSizeWarningLimit: 100000000,
@@ -41,6 +42,8 @@ export default defineConfig({
output: {
manualChunks: undefined,
inlineDynamicImports: true,
+ entryFileNames: 'report.js',
+ assetFileNames: (assetInfo) => assetInfo.names.some(n => n.endsWith('.css')) ? 'report.css' : '[name][extname]',
},
},
},
diff --git a/packages/playwright/src/reporters/html.ts b/packages/playwright/src/reporters/html.ts
index bfeabe9ef2..a72587bcb2 100644
--- a/packages/playwright/src/reporters/html.ts
+++ b/packages/playwright/src/reporters/html.ts
@@ -32,7 +32,6 @@ import type { ReportConfigureParams, ReportEndParams, ReporterV2 } from './repor
import type { HtmlReporterOptions as HtmlReporterConfigOptions, Metadata, TestAnnotation } from '../../types/test';
import type * as api from '../../types/testReporter';
import type { HTMLReport, HTMLReportOptions, Location, Stats, TestAttachment, TestCase, TestCaseSummary, TestFile, TestFileSummary, TestResult, TestStep } from '@html-reporter/types';
-import type { ZipFile } from 'playwright-core/lib/zipBundle';
import type { TransformCallback } from 'stream';
type TestEntry = {
@@ -142,21 +141,11 @@ class HtmlReporter implements ReporterV2 {
async onEnd(result: api.FullResult) {
const projectSuites = this.suite.suites;
await removeFolders([this._outputFolder]);
- let noSnippets: boolean | undefined;
- if (process.env.PLAYWRIGHT_HTML_NO_SNIPPETS === 'false' || process.env.PLAYWRIGHT_HTML_NO_SNIPPETS === '0')
- noSnippets = false;
- else if (process.env.PLAYWRIGHT_HTML_NO_SNIPPETS)
- noSnippets = true;
- noSnippets = noSnippets || this._options.noSnippets;
-
- let noCopyPrompt: boolean | undefined;
- if (process.env.PLAYWRIGHT_HTML_NO_COPY_PROMPT === 'false' || process.env.PLAYWRIGHT_HTML_NO_COPY_PROMPT === '0')
- noCopyPrompt = false;
- else if (process.env.PLAYWRIGHT_HTML_NO_COPY_PROMPT)
- noCopyPrompt = true;
- noCopyPrompt = noCopyPrompt || this._options.noCopyPrompt;
-
- const builder = new HtmlBuilder(this.config, this._outputFolder, this._attachmentsBaseURL, {
+ const noSnippets = parseBooleanEnvVar('PLAYWRIGHT_HTML_NO_SNIPPETS') ?? this._options.noSnippets;
+ const noCopyPrompt = parseBooleanEnvVar('PLAYWRIGHT_HTML_NO_COPY_PROMPT') ?? this._options.noCopyPrompt;
+ const doNotInlineAssets = parseBooleanEnvVar('PLAYWRIGHT_HTML_DO_NOT_INLINE_ASSETS') ?? this._options.doNotInlineAssets ?? false;
+
+ const builder = new HtmlBuilder(this.config, this._outputFolder, this._attachmentsBaseURL, doNotInlineAssets, {
title: process.env.PLAYWRIGHT_HTML_TITLE || this._options.title,
noSnippets,
noCopyPrompt,
@@ -204,6 +193,15 @@ function getHtmlReportOptionProcessEnv(): HtmlReportOpenOption | undefined {
return htmlOpenEnv;
}
+function parseBooleanEnvVar(name: string): boolean | undefined {
+ const value = process.env[name];
+ if (value === 'false' || value === '0')
+ return false;
+ if (value)
+ return true;
+ return undefined;
+}
+
function standaloneDefaultFolder(): string {
return reportFolderFromEnv() ?? resolveReporterOutputPath('playwright-report', process.cwd(), undefined);
}
@@ -255,17 +253,18 @@ class HtmlBuilder {
private _config: api.FullConfig;
private _reportFolder: string;
private _stepsInFile = new MultiMap<string, TestStep>();
- private _dataZipFile: ZipFile;
+ private _dataZipFile = new yazl.ZipFile();
private _hasTraces = false;
private _attachmentsBaseURL: string;
private _options: HTMLReportOptions;
+ private _doNotInlineAssets: boolean;
- constructor(config: api.FullConfig, outputDir: string, attachmentsBaseURL: string, options: HTMLReportOptions) {
+ constructor(config: api.FullConfig, outputDir: string, attachmentsBaseURL: string, doNotInlineAssets: boolean, options: HTMLReportOptions) {
this._config = config;
this._reportFolder = outputDir;
this._options = options;
+ this._doNotInlineAssets = doNotInlineAssets;
fs.mkdirSync(this._reportFolder, { recursive: true });
- this._dataZipFile = new yazl.ZipFile();
this._attachmentsBaseURL = attachmentsBaseURL;
}
@@ -338,9 +337,7 @@ class HtmlBuilder {
singleTestId = testFile.tests[0].testId;
}
- // Copy app.
- const appFolder = path.join(require.resolve('playwright-core'), '..', 'lib', 'vite', 'htmlReport');
- await copyFileAndMakeWritable(path.join(appFolder, 'index.html'), path.join(this._reportFolder, 'index.html'));
+ const reportIndexFile = await this._writeStaticAssets();
// Copy trace viewer.
if (this._hasTraces) {
@@ -360,22 +357,44 @@ class HtmlBuilder {
}
}
- await this._writeReportData(path.join(this._reportFolder, 'index.html'));
-
+ await this._writeReportData(reportIndexFile);
return { ok, singleTestId };
}
+ private async _writeStaticAssets() {
+ const appFolder = path.join(require.resolve('playwright-core'), '..', 'lib', 'vite', 'htmlReport');
+ const reportIndexFile = path.join(this._reportFolder, 'index.html');
+ if (this._doNotInlineAssets) {
+ const html = await fs.promises.readFile(path.join(appFolder, 'index.html'), 'utf-8');
+ await Promise.all([
+ fs.promises.writeFile(reportIndexFile, html),
+ fs.promises.copyFile(path.join(appFolder, 'report.js'), path.join(this._reportFolder, 'report.js')),
+ fs.promises.copyFile(path.join(appFolder, 'report.css'), path.join(this._reportFolder, 'report.css')),
+ ]);
+ } else {
+ let html = await fs.promises.readFile(path.join(appFolder, 'index.html'), 'utf-8');
+ const [js, css] = await Promise.all([
+ fs.promises.readFile(path.join(appFolder, 'report.js'), 'utf-8'),
+ fs.promises.readFile(path.join(appFolder, 'report.css'), 'utf-8'),
+ ]);
+ html = html.replace(/<script type="module"[^>]*><\/script>/, () => `<script type="module">${js}</script>`);
+ html = html.replace(/<link rel="stylesheet"[^>]*>/, () => `<style type='text/css'>${css}</style>`);
+ await fs.promises.writeFile(reportIndexFile, html);
+ }
+ return reportIndexFile;
+ }
+
private async _writeReportData(filePath: string) {
- fs.appendFileSync(filePath, '<script id="playwrightReportBase64" type="application/zip">data:application/zip;base64,');
+ fs.appendFileSync(filePath, '<template id="playwrightReportBase64">data:application/zip;base64,');
await new Promise(f => {
- this._dataZipFile!.end(undefined, () => {
- this._dataZipFile!.outputStream
+ this._dataZipFile.end(undefined, () => {
+ this._dataZipFile.outputStream
.pipe(new Base64Encoder())
.pipe(fs.createWriteStream(filePath, { flags: 'a' })).on('close', f);
});
});
- fs.appendFileSync(filePath, '</script>');
+ fs.appendFileSync(filePath, '</template>');
}
private _addDataFile(fileName: string, data: any) {
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index 9fa429d5ed..2172696333 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;
+ doNotInlineAssets?: boolean;
};
export type ReporterDescription = Readonly<
diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts
index fc9be4c718..b23269b057 100644
--- a/tests/playwright-test/reporter-html.spec.ts
+++ b/tests/playwright-test/reporter-html.spec.ts
@@ -3122,6 +3122,32 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await expect(page.getByTestId('test-snippet')).not.toBeVisible();
});
+ test('should generate unpacked report with doNotInlineAssets', async ({ runInlineTest, showReport, page }, testInfo) => {
+ const result = await runInlineTest({
+ 'example.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('passes', async ({}) => {});
+ `,
+ }, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never', PLAYWRIGHT_HTML_DO_NOT_INLINE_ASSETS: '1' });
+ expect(result.exitCode).toBe(0);
+
+ const reportFolder = testInfo.outputPath('playwright-report');
+ expect(fs.existsSync(path.join(reportFolder, 'report.js'))).toBe(true);
+ expect(fs.existsSync(path.join(reportFolder, 'report.css'))).toBe(true);
+ const html = fs.readFileSync(path.join(reportFolder, 'index.html'), 'utf-8');
+ expect(html).toContain('src="./report.js"');
+ expect(html).toContain('href="./report.css"');
+ // Report data is stored in a <template> element (not a <script>), so no inline scripts or styles.
+ expect(html).toContain('<template id="playwrightReportBase64">');
+ expect(html).not.toMatch(/<script[^>]*>[^<\s]/);
+ expect(html).not.toMatch(/<style[\s>]/);
+
+ await showReport();
+ await expect(page.locator('.subnav-item:has-text("Passed") .counter')).toHaveText('1');
+ await page.getByRole('link', { name: 'passes' }).click();
+ await expect(page.getByText('example.spec.ts')).toBeVisible();
+ });
+
test('worker test list', async ({ runInlineTest, showReport, page }) => {
const result = await runInlineTest({
'playwright.config.ts': `
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index aad0cc9306..c944cadba5 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;
+ doNotInlineAssets?: boolean;
};
export type ReporterDescription = Readonly<