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 copy prompt button (#37443)

Pavel Feldman45b487f37150
diff --git a/docs/src/test-reporters-js.md b/docs/src/test-reporters-js.md
index f3393c6a25..973cc1d641 100644
--- a/docs/src/test-reporters-js.md
+++ b/docs/src/test-reporters-js.md
@@ -252,6 +252,7 @@ HTML report supports the following configuration options and environment variabl
| `PLAYWRIGHT_HTML_HOST` | `host` | When report opens in the browser, it will be served bound to this hostname. | `localhost`
| `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_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/reportView.tsx b/packages/html-reporter/src/reportView.tsx
index c68e130a5b..7209ca929d 100644
--- a/packages/html-reporter/src/reportView.tsx
+++ b/packages/html-reporter/src/reportView.tsx
@@ -52,7 +52,7 @@ export const ReportView: React.FC<{
const testId = searchParams.get('testId');
const q = searchParams.get('q')?.toString() || '';
const filterParam = q ? '&q=' + q : '';
- const reportTitle = report?.json()?.title;
+ const reportTitle = report?.json()?.options.title;
const testIdToFileIdMap = React.useMemo(() => {
const map = new Map<string, string>();
@@ -181,10 +181,12 @@ const TestCaseViewLoader: React.FC<{
</div>;
}
+ const { projectNames, metadata, options } = report.json();
return <div className='test-case-column'>
<TestCaseView
- projectNames={report.json().projectNames}
- testRunMetadata={report.json().metadata}
+ projectNames={projectNames}
+ testRunMetadata={metadata}
+ options={options}
next={next}
prev={prev}
test={test}
diff --git a/packages/html-reporter/src/testCaseView.tsx b/packages/html-reporter/src/testCaseView.tsx
index c068a030b1..75a8e1c742 100644
--- a/packages/html-reporter/src/testCaseView.tsx
+++ b/packages/html-reporter/src/testCaseView.tsx
@@ -15,7 +15,7 @@
*/
import type { TestAnnotation } from '@playwright/test';
-import type { TestCase, TestCaseSummary } from './types';
+import type { HTMLReportOptions, TestCase, TestCaseSummary } from './types';
import * as React from 'react';
import { TabbedPane } from './tabbedPane';
import { AutoChip } from './chip';
@@ -39,7 +39,8 @@ export const TestCaseView: React.FC<{
next: TestCaseSummary | undefined,
prev: TestCaseSummary | undefined,
run: number,
-}> = ({ projectNames, test, testRunMetadata, run, next, prev }) => {
+ options?: HTMLReportOptions,
+}> = ({ projectNames, test, testRunMetadata, run, next, prev, options }) => {
const [selectedResultIndex, setSelectedResultIndex] = React.useState(run);
const searchParams = React.useContext(SearchParamsContext);
@@ -83,7 +84,7 @@ export const TestCaseView: React.FC<{
{!!visibleAnnotations.length && <AutoChip header='Annotations' dataTestId='test-case-annotations'>
{visibleAnnotations.map((annotation, index) => <TestCaseAnnotationView key={index} annotation={annotation} />)}
</AutoChip>}
- <TestResultView test={test!} result={result} testRunMetadata={testRunMetadata} />
+ <TestResultView test={test!} result={result} testRunMetadata={testRunMetadata} options={options} />
</>;
},
})) || []} selectedTab={String(selectedResultIndex)} setSelectedTab={id => setSelectedResultIndex(+id)} />
diff --git a/packages/html-reporter/src/testFilesView.tsx b/packages/html-reporter/src/testFilesView.tsx
index efffa58a95..b19e2591b6 100644
--- a/packages/html-reporter/src/testFilesView.tsx
+++ b/packages/html-reporter/src/testFilesView.tsx
@@ -93,7 +93,7 @@ export const TestFilesHeader: React.FC<{
</>;
return <>
- <HeaderView title={report.title} leftSuperHeader={leftSuperHeader} rightSuperHeader={rightSuperHeader} />
+ <HeaderView title={report.options.title} leftSuperHeader={leftSuperHeader} rightSuperHeader={rightSuperHeader} />
{!isMetadataInTopLine && metadataToggleButton}
{metadataVisible && <MetadataView metadata={report.metadata}/>}
{!!report.errors.length && <AutoChip header='Errors' dataTestId='report-errors'>
diff --git a/packages/html-reporter/src/testResultView.tsx b/packages/html-reporter/src/testResultView.tsx
index 8fa4ab017d..1b5d7c6c87 100644
--- a/packages/html-reporter/src/testResultView.tsx
+++ b/packages/html-reporter/src/testResultView.tsx
@@ -14,7 +14,7 @@
limitations under the License.
*/
-import type { TestAttachment, TestCase, TestResult, TestStep } from './types';
+import type { HTMLReportOptions, TestAttachment, TestCase, TestResult, TestStep } from './types';
import * as React from 'react';
import { TreeItem } from './treeItem';
import { msToString } from './utils';
@@ -74,7 +74,8 @@ export const TestResultView: React.FC<{
test: TestCase,
result: TestResult,
testRunMetadata: MetadataWithCommitInfo | undefined,
-}> = ({ test, result, testRunMetadata }) => {
+ options?: HTMLReportOptions,
+}> = ({ test, result, testRunMetadata, options }) => {
const { screenshots, videos, traces, otherAttachments, diffs, errors, otherAttachmentAnchors, screenshotAnchors, errorContext } = React.useMemo(() => {
const attachments = result.attachments.filter(a => !a.name.startsWith('_'));
const screenshots = new Set(attachments.filter(a => a.contentType.startsWith('image/')));
@@ -91,6 +92,9 @@ export const TestResultView: React.FC<{
}, [result]);
const prompt = useAsyncMemo(async () => {
+ if (options?.noCopyPrompt)
+ return undefined;
+
const stdoutAttachment = result.attachments.find(a => a.name === 'stdout');
const stderrAttachment = result.attachments.find(a => a.name === 'stderr');
const stdout = stdoutAttachment?.body && stdoutAttachment.contentType === 'text/plain' ? stdoutAttachment.body : undefined;
diff --git a/packages/html-reporter/src/types.d.ts b/packages/html-reporter/src/types.d.ts
index 1acf73d780..270a8cb983 100644
--- a/packages/html-reporter/src/types.d.ts
+++ b/packages/html-reporter/src/types.d.ts
@@ -36,15 +36,21 @@ export type Location = {
column: number;
};
+export type HTMLReportOptions = {
+ title?: string;
+ noCopyPrompt?: boolean;
+ noSnippets?: boolean;
+};
+
export type HTMLReport = {
metadata: Metadata;
- title: string | undefined;
files: TestFileSummary[];
stats: Stats;
projectNames: string[];
startTime: number;
duration: number;
errors: string[]; // Top-level errors that are not attributed to any test.
+ options: HTMLReportOptions;
};
export type TestFile = {
diff --git a/packages/playwright/src/reporters/html.ts b/packages/playwright/src/reporters/html.ts
index 5e92291f8b..04be226d86 100644
--- a/packages/playwright/src/reporters/html.ts
+++ b/packages/playwright/src/reporters/html.ts
@@ -31,7 +31,7 @@ import { resolveReporterOutputPath, stripAnsiEscapes } from '../util';
import type { ReporterV2 } from './reporterV2';
import type { HtmlReporterOptions as HtmlReporterConfigOptions, Metadata, TestAnnotation } from '../../types/test';
import type * as api from '../../types/testReporter';
-import type { HTMLReport, Location, Stats, TestAttachment, TestCase, TestCaseSummary, TestFile, TestFileSummary, TestResult, TestStep } from '@html-reporter/types';
+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';
@@ -131,7 +131,18 @@ class HtmlReporter implements ReporterV2 {
noSnippets = true;
noSnippets = noSnippets || this._options.noSnippets;
- const builder = new HtmlBuilder(this.config, this._outputFolder, this._attachmentsBaseURL, process.env.PLAYWRIGHT_HTML_TITLE || this._options.title, 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, {
+ title: process.env.PLAYWRIGHT_HTML_TITLE || this._options.title,
+ noSnippets,
+ noCopyPrompt,
+ });
this._buildResult = await builder.build(this.config.metadata, projectSuites, result, this._topLevelErrors);
}
@@ -228,17 +239,15 @@ class HtmlBuilder {
private _dataZipFile: ZipFile;
private _hasTraces = false;
private _attachmentsBaseURL: string;
- private _title: string | undefined;
- private _noSnippets: boolean;
+ private _options: HTMLReportOptions;
- constructor(config: api.FullConfig, outputDir: string, attachmentsBaseURL: string, title: string | undefined, noSnippets: boolean = false) {
+ constructor(config: api.FullConfig, outputDir: string, attachmentsBaseURL: string, options: HTMLReportOptions) {
this._config = config;
this._reportFolder = outputDir;
- this._noSnippets = noSnippets;
+ this._options = options;
fs.mkdirSync(this._reportFolder, { recursive: true });
this._dataZipFile = new yazl.ZipFile();
this._attachmentsBaseURL = attachmentsBaseURL;
- this._title = title;
}
async build(metadata: Metadata, projectSuites: api.Suite[], result: api.FullResult, topLevelErrors: api.TestError[]): Promise<{ ok: boolean, singleTestId: string | undefined }> {
@@ -264,7 +273,7 @@ class HtmlBuilder {
}
}
}
- if (!this._noSnippets)
+ if (!this._options.noSnippets)
createSnippets(this._stepsInFile);
let ok = true;
@@ -296,13 +305,13 @@ class HtmlBuilder {
}
const htmlReport: HTMLReport = {
metadata,
- title: this._title,
startTime: result.startTime.getTime(),
duration: result.duration,
files: [...data.values()].map(e => e.testFileSummary),
projectNames: projectSuites.map(r => r.project()!.name),
stats: { ...[...data.values()].reduce((a, e) => addStats(a, e.testFileSummary.stats), emptyStats()) },
errors: topLevelErrors.map(error => formatError(internalScreen, error).message),
+ options: this._options,
};
htmlReport.files.sort((f1, f2) => {
const w1 = f1.stats.unexpected * 1000 + f1.stats.flaky;
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index 71a1ac0cdc..91c168cd73 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -22,7 +22,16 @@ export type BlobReporterOptions = { outputDir?: string, fileName?: string };
export type ListReporterOptions = { printSteps?: boolean };
export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean };
export type JsonReporterOptions = { outputFile?: string };
-export type HtmlReporterOptions = { outputFolder?: string, open?: 'always' | 'never' | 'on-failure', host?: string, port?: number, attachmentsBaseURL?: string, title?: string, noSnippets?: boolean };
+export type HtmlReporterOptions = {
+ outputFolder?: string;
+ open?: 'always' | 'never' | 'on-failure';
+ host?: string;
+ port?: number;
+ attachmentsBaseURL?: string;
+ title?: string;
+ noSnippets?: boolean;
+ noCopyPrompt?: boolean;
+};
export type ReporterDescription = Readonly<
['blob'] | ['blob', BlobReporterOptions] |
diff --git a/packages/web/src/shared/prompts.ts b/packages/web/src/shared/prompts.ts
index 1abe7cfcc0..f6807af40d 100644
--- a/packages/web/src/shared/prompts.ts
+++ b/packages/web/src/shared/prompts.ts
@@ -42,7 +42,7 @@ export async function copyPrompt<ErrorInfo extends { message: string }>({
buildCodeFrame(error: ErrorInfo): Promise<string | undefined>;
stdout?: string;
stderr?: string;
-}) {
+}): Promise<string | undefined> {
const meaningfulSingleLineErrors = new Set(errors.filter(e => e.message && !e.message.includes('\n')).map(e => e.message!));
for (const error of errors) {
for (const singleLineError of meaningfulSingleLineErrors.keys()) {
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index 03f9babb43..665c7ecd32 100644
--- a/utils/generate_types/overrides-test.d.ts
+++ b/utils/generate_types/overrides-test.d.ts
@@ -21,7 +21,16 @@ export type BlobReporterOptions = { outputDir?: string, fileName?: string };
export type ListReporterOptions = { printSteps?: boolean };
export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean };
export type JsonReporterOptions = { outputFile?: string };
-export type HtmlReporterOptions = { outputFolder?: string, open?: 'always' | 'never' | 'on-failure', host?: string, port?: number, attachmentsBaseURL?: string, title?: string, noSnippets?: boolean };
+export type HtmlReporterOptions = {
+ outputFolder?: string;
+ open?: 'always' | 'never' | 'on-failure';
+ host?: string;
+ port?: number;
+ attachmentsBaseURL?: string;
+ title?: string;
+ noSnippets?: boolean;
+ noCopyPrompt?: boolean;
+};
export type ReporterDescription = Readonly<
['blob'] | ['blob', BlobReporterOptions] |