Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: implement merge-files on html client side (#37615)
Pavel Feldman4f90d4347d19
diff --git a/docs/src/test-reporters-js.md b/docs/src/test-reporters-js.mdindex ed50f672af..973cc1d641 100644--- a/docs/src/test-reporters-js.md+++ b/docs/src/test-reporters-js.md@@ -253,7 +253,6 @@ 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 reporterdiff --git a/packages/html-reporter/src/common.css b/packages/html-reporter/src/common.cssindex ae31afd527..c34e279923 100644--- a/packages/html-reporter/src/common.css+++ b/packages/html-reporter/src/common.css@@ -40,12 +40,6 @@ svg {position: relative;}-.cursor-pointer,-.cursor-pointer > * {- cursor: pointer;- user-select: none;-}-.hbox {display: flex;flex: auto;diff --git a/packages/html-reporter/src/headerView.tsx b/packages/html-reporter/src/headerView.tsxindex bbed14b7a4..7cba8cbd2d 100644--- a/packages/html-reporter/src/headerView.tsx+++ b/packages/html-reporter/src/headerView.tsx@@ -26,6 +26,7 @@ import { filterWithQuery } from './filter';import { linkifyText } from '@web/renderUtils';import { Dialog } from '@web/shared/dialog';import { useDarkModeSetting } from '@web/theme';+import { useSetting } from '@web/uiUtils';export const HeaderView: React.FC<{title: string | undefined,@@ -117,40 +118,49 @@ const NavLink: React.FC<{};const SettingsButton: React.FC = () => {- const settingsRef = React.useRef<HTMLAnchorElement>(null);+ const settingsRef = React.useRef<HTMLDivElement>(null);const [settingsOpen, setSettingsOpen] = React.useState(false);const [darkMode, setDarkMode] = useDarkModeSetting();+ const [mergeFiles, setMergeFiles] = useSetting('mergeFiles', false);- return <>+ return <div+ role='button'+ ref={settingsRef}+ style={{ cursor: 'pointer' }}+ className='subnav-item'+ title='Settings'+ onClick={e => {+ setSettingsOpen(!settingsOpen);+ e.preventDefault();+ }}+ onMouseDown={preventDefault}><Dialogopen={settingsOpen}- width={200}+ width={150}verticalOffset={8}requestClose={() => setSettingsOpen(false)}anchor={settingsRef}dataTestId='settings-dialog'>- <div className='hbox cursor-pointer' style={{ alignItems: 'center' }}>- <input type='checkbox' id='dark-mode-setting' checked={darkMode} onChange={() => setDarkMode(!darkMode)}></input>- <label htmlFor='dark-mode-setting'>Dark mode</label>- </div>+ <label style={{ cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 4 }} onClick={stopPropagation}>+ <input type='checkbox' checked={darkMode} onChange={() => setDarkMode(!darkMode)}></input>+ Dark mode+ </label>+ <label style={{ cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 4 }} onClick={stopPropagation}>+ <input type='checkbox' checked={mergeFiles} onChange={() => setMergeFiles(!mergeFiles)}></input>+ Merge files+ </label></Dialog>- <a- ref={settingsRef}- style={{ cursor: 'pointer' }}- className='subnav-item'- title='Settings'- onClick={e => {- setSettingsOpen(!settingsOpen);- e.preventDefault();- }}- onMouseDown={preventDefault}>- {icons.settings()}- </a>- </>;+ {icons.settings()}+ </div>;};const preventDefault = (e: any) => {e.stopPropagation();e.preventDefault();};++const stopPropagation = (e: any) => {+ e.stopPropagation();+ e.stopImmediatePropagation();+};diff --git a/packages/html-reporter/src/reportView.tsx b/packages/html-reporter/src/reportView.tsxindex 7209ca929d..c21ded5c7d 100644--- a/packages/html-reporter/src/reportView.tsx+++ b/packages/html-reporter/src/reportView.tsx@@ -26,6 +26,7 @@ import './reportView.css';import { TestCaseView } from './testCaseView';import { TestFilesHeader, TestFilesView } from './testFilesView';import './theme.css';+import { useSetting } from '@web/uiUtils';declare global {interface Window {@@ -49,6 +50,7 @@ export const ReportView: React.FC<{const [expandedFiles, setExpandedFiles] = React.useState<Map<string, boolean>>(new Map());const [filterText, setFilterText] = React.useState(searchParams.get('q') || '');const [metadataVisible, setMetadataVisible] = React.useState(false);+ const [mergeFiles] = useSetting('mergeFiles', false);const testId = searchParams.get('testId');const q = searchParams.get('q')?.toString() || '';const filterParam = q ? '&q=' + q : '';@@ -66,15 +68,8 @@ export const ReportView: React.FC<{const filter = React.useMemo(() => Filter.parse(filterText), [filterText]);const filteredStats = React.useMemo(() => filter.empty() ? undefined : computeStats(report?.json().files || [], filter), [report, filter]);const testModel = React.useMemo(() => {- const result: TestModelSummary = { files: [], tests: [] };- for (const file of report?.json().files || []) {- const tests = file.tests.filter(t => filter.matches(t));- if (tests.length)- result.files.push({ ...file, tests });- result.tests.push(...tests);- }- return result;- }, [report, filter]);+ return mergeFiles ? createMergedFilesModel(report, filter) : createFilesModel(report, filter);+ }, [report, filter, mergeFiles]);const { prev, next } = React.useMemo(() => {const index = testModel.tests.findIndex(t => t.testId === testId);@@ -133,7 +128,7 @@ export const ReportView: React.FC<{<Route predicate={testFilesRoutePredicate}><TestFilesHeader report={report?.json()} filteredStats={filteredStats} metadataVisible={metadataVisible} toggleMetadataVisible={() => setMetadataVisible(visible => !visible)}/><TestFilesView- tests={testModel.files}+ files={testModel.files}expandedFiles={expandedFiles}setExpandedFiles={setExpandedFiles}projectNames={report?.json().projectNames || []}@@ -208,3 +203,46 @@ function computeStats(files: TestFileSummary[], filter: Filter): FilteredStats {}return stats;}++function createFilesModel(report: LoadedReport | undefined, filter: Filter): TestModelSummary {+ const result: TestModelSummary = { files: [], tests: [] };+ for (const file of report?.json().files || []) {+ const tests = file.tests.filter(t => filter.matches(t));+ if (tests.length)+ result.files.push({ ...file, tests });+ result.tests.push(...tests);+ }+ return result;+}++function createMergedFilesModel(report: LoadedReport | undefined, filter: Filter): TestModelSummary {+ const groups: TestFileSummary[] = [];+ const groupMap = new Map<string, TestFileSummary>();++ for (const file of report?.json().files || []) {+ const tests = file.tests.filter(t => filter.matches(t));+ for (const test of tests) {+ const describe = test.path[0] ?? '<anonymous>';+ let group = groupMap.get(describe);+ if (!group) {+ group = {+ fileId: describe,+ fileName: describe,+ tests: [],+ stats: { total: 0, expected: 0, unexpected: 0, flaky: 0, skipped: 0, ok: true }+ };+ groupMap.set(describe, group);+ groups.push(group);+ }+ const testCopy = { ...test, path: test.path.slice(1) };+ group.tests.push(testCopy);+ }+ }++ groups.sort((a, b) => a.fileName.localeCompare(b.fileName));++ const result: TestModelSummary = { files: groups, tests: [] };+ for (const group of groups)+ result.tests.push(...group.tests);+ return result;+}diff --git a/packages/html-reporter/src/testFilesView.tsx b/packages/html-reporter/src/testFilesView.tsxindex b19e2591b6..5852acb3d5 100644--- a/packages/html-reporter/src/testFilesView.tsx+++ b/packages/html-reporter/src/testFilesView.tsx@@ -27,20 +27,20 @@ import { HeaderView } from './headerView';import { clsx } from '@web/uiUtils';export const TestFilesView: React.FC<{- tests: TestFileSummary[],+ files: TestFileSummary[],expandedFiles: Map<string, boolean>,setExpandedFiles: (value: Map<string, boolean>) => void,projectNames: string[],-}> = ({ tests, expandedFiles, setExpandedFiles, projectNames }) => {+}> = ({ files, expandedFiles, setExpandedFiles, projectNames }) => {const filteredFiles = React.useMemo(() => {const result: { file: TestFileSummary, defaultExpanded: boolean }[] = [];let visibleTests = 0;- for (const file of tests) {+ for (const file of files) {visibleTests += file.tests.length;result.push({ file, defaultExpanded: visibleTests < 200 });}return result;- }, [tests]);+ }, [files]);return <>{filteredFiles.map(({ file, defaultExpanded }) => {return <TestFileViewdiff --git a/packages/html-reporter/src/types.d.ts b/packages/html-reporter/src/types.d.tsindex e6aebdc31b..270a8cb983 100644--- a/packages/html-reporter/src/types.d.ts+++ b/packages/html-reporter/src/types.d.ts@@ -40,7 +40,6 @@ 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.tsindex 4040ffc187..22d2a144de 100644--- a/packages/playwright/src/reporters/html.ts+++ b/packages/playwright/src/reporters/html.ts@@ -138,18 +138,10 @@ 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);}@@ -265,20 +257,8 @@ class HtmlBuilder {for (const projectSuite of projectSuites) {const projectName = projectSuite.project()!.name;for (const fileSuite of projectSuite.suites) {- 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);- }+ const fileName = this._relativeLocation(fileSuite.location)!.file;+ this._createEntryForSuite(data, projectName, fileSuite, fileName, true);}}if (!this._options.noSnippets)diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.tsindex aaf187f497..91c168cd73 100644--- a/packages/playwright/types/test.d.ts+++ b/packages/playwright/types/test.d.ts@@ -31,7 +31,6 @@ export type HtmlReporterOptions = {title?: string;noSnippets?: boolean;noCopyPrompt?: boolean;- noFiles?: boolean;};export type ReporterDescription = Readonly<diff --git a/packages/trace-viewer/src/ui/defaultSettingsView.tsx b/packages/trace-viewer/src/ui/defaultSettingsView.tsxindex 6334d3cded..547af5fdca 100644--- a/packages/trace-viewer/src/ui/defaultSettingsView.tsx+++ b/packages/trace-viewer/src/ui/defaultSettingsView.tsx@@ -27,8 +27,8 @@ export const DefaultSettingsView: React.FC<{}> = () => {shouldPopulateCanvasFromScreenshot,setShouldPopulateCanvasFromScreenshot,] = useSetting('shouldPopulateCanvasFromScreenshot', false);- const [hideFiles, setHideFiles] = useSetting('hideFiles', false);const [darkMode, setDarkMode] = useDarkModeSetting();+ const [mergeFiles, setMergeFiles] = useSetting('mergeFiles', false);return (<SettingsView@@ -41,9 +41,9 @@ export const DefaultSettingsView: React.FC<{}> = () => {},{type: 'check',- value: hideFiles,- set: setHideFiles,- name: 'Hide files'+ value: mergeFiles,+ set: setMergeFiles,+ name: 'Merge files'},{type: 'check',diff --git a/packages/trace-viewer/src/ui/uiModeView.tsx b/packages/trace-viewer/src/ui/uiModeView.tsxindex 1ef78ff0ee..4399d2d13e 100644--- a/packages/trace-viewer/src/ui/uiModeView.tsx+++ b/packages/trace-viewer/src/ui/uiModeView.tsx@@ -102,7 +102,7 @@ export const UIModeView: React.FC<{}> = ({const [singleWorker, setSingleWorker] = useSetting<boolean>('single-worker', false);const [updateSnapshots, setUpdateSnapshots] = useSetting<reporterTypes.FullConfig['updateSnapshots']>('updateSnapshots', 'missing');- const [hideFiles] = useSetting('hideFiles', false);+ const [mergeFiles] = useSetting('mergeFiles', false);const inputRef = React.useRef<HTMLInputElement>(null);@@ -241,15 +241,15 @@ export const UIModeView: React.FC<{}> = ({// Test tree is built from the model and filters.const { testTree } = React.useMemo(() => {if (!testModel)- return { testTree: new TestTree('', new TeleSuite('', 'root'), [], projectFilters, queryParams.pathSeparator, hideFiles) };- const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator, hideFiles);+ return { testTree: new TestTree('', new TeleSuite('', 'root'), [], projectFilters, queryParams.pathSeparator, mergeFiles) };+ const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator, mergeFiles);testTree.filterTree(filterText, statusFilters, isRunningTest ? runningState?.testIds : undefined);testTree.sortAndPropagateStatus();testTree.shortenRoot();testTree.flattenForSingleProject();setVisibleTestIds(testTree.testIds());return { testTree };- }, [filterText, testModel, statusFilters, projectFilters, setVisibleTestIds, runningState, isRunningTest, hideFiles]);+ }, [filterText, testModel, statusFilters, projectFilters, setVisibleTestIds, runningState, isRunningTest, mergeFiles]);const runTests = React.useCallback((mode: 'queue-if-busy' | 'bounce-if-busy', testIds: Set<string>) => {if (!testServerConnection || !testModel)@@ -326,7 +326,7 @@ export const UIModeView: React.FC<{}> = ({// run affected watched testsconst testModel = teleSuiteUpdater.asModel();- const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator, hideFiles);+ const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator, mergeFiles);const testIds: string[] = [];const set = new Set(params.testFiles);@@ -350,7 +350,7 @@ export const UIModeView: React.FC<{}> = ({runTests('queue-if-busy', new Set(testIds));});return () => disposable.dispose();- }, [runTests, testServerConnection, watchAll, watchedTreeIds, teleSuiteUpdater, projectFilters, hideFiles]);+ }, [runTests, testServerConnection, watchAll, watchedTreeIds, teleSuiteUpdater, projectFilters, mergeFiles]);// Shortcuts.React.useEffect(() => {diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.tsindex ded289bd04..8992eac3c0 100644--- a/tests/playwright-test/reporter-html.spec.ts+++ b/tests/playwright-test/reporter-html.spec.ts@@ -3204,16 +3204,8 @@ for (const useIntermediateMergeReport of [true, false] as const) {});}-test('should support noFiles option', async ({ runInlineTest, showReport, page }) => {+test('should support merge files 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', () => {@@ -3227,21 +3219,24 @@ test('should support noFiles option', async ({ runInlineTest, showReport, page }test('test 3', async ({}) => {});});`,- }, {}, { PLAYWRIGHT_HTML_OPEN: 'never' });+ }, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never' });await showReport();+ await page.getByRole('button', { name: 'Settings' }).click();+ await page.getByRole('checkbox', { name: 'Merge files' }).click();+await expect(page.locator('body')).toMatchAriaSnapshot(`+ - button "<anonymous>" [expanded]+ - region:+ - link "test 2"+ - link "a.test.js:6"- 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"`);});diff --git a/tests/playwright-test/ui-mode-test-tree.spec.ts b/tests/playwright-test/ui-mode-test-tree.spec.tsindex 993f7ccb79..93761b7d9a 100644--- a/tests/playwright-test/ui-mode-test-tree.spec.ts+++ b/tests/playwright-test/ui-mode-test-tree.spec.ts@@ -578,7 +578,7 @@ test('should resolve title conflicts', async ({ runUITest }) => {`);});-test('should hide file names', async ({ runUITest }) => {+test('should merge files', async ({ runUITest }) => {const { page } = await runUITest({'a.test.ts': `import { test } from '@playwright/test';@@ -605,7 +605,7 @@ test('should hide file names', async ({ runUITest }) => {});await page.getByText('Settings', { exact: true }).click();- await page.getByLabel('Hide files').click();+ await page.getByLabel('Merge files').click();await expect.poll(dumpTestTree(page)).toContain(`▼ ◯ <anonymous>diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.tsindex 7255ea295c..665c7ecd32 100644--- a/utils/generate_types/overrides-test.d.ts+++ b/utils/generate_types/overrides-test.d.ts@@ -30,7 +30,6 @@ export type HtmlReporterOptions = {title?: string;noSnippets?: boolean;noCopyPrompt?: boolean;- noFiles?: boolean;};export type ReporterDescription = Readonly<