Skip to content

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

feat(ui): allow inlining file suites (#37505)

Pavel Feldmanec8edbc371ac
diff --git a/packages/playwright/src/isomorphic/testTree.ts b/packages/playwright/src/isomorphic/testTree.ts
index 6f38a99588..713b0d6d5d 100644
--- a/packages/playwright/src/isomorphic/testTree.ts
+++ b/packages/playwright/src/isomorphic/testTree.ts
@@ -60,7 +60,7 @@ export class TestTree {
private _treeItemByTestId = new Map<string, TestItem | TestCaseItem>();
readonly pathSeparator: string;
- constructor(rootFolder: string, rootSuite: reporterTypes.Suite | undefined, loadErrors: reporterTypes.TestError[], projectFilters: Map<string, boolean> | undefined, pathSeparator: string) {
+ constructor(rootFolder: string, rootSuite: reporterTypes.Suite | undefined, loadErrors: reporterTypes.TestError[], projectFilters: Map<string, boolean> | undefined, pathSeparator: string, hideFiles: boolean) {
const filterProjects = projectFilters && [...projectFilters.values()].some(Boolean);
this.pathSeparator = pathSeparator;
this.rootItem = {
@@ -77,11 +77,11 @@ export class TestTree {
};
this._treeItemById.set(rootFolder, this.rootItem);
- const visitSuite = (project: reporterTypes.FullProject, parentSuite: reporterTypes.Suite, parentGroup: GroupItem) => {
- for (const suite of parentSuite.suites) {
+ const visitSuite = (project: reporterTypes.FullProject, parentSuite: reporterTypes.Suite, parentGroup: GroupItem, mode: 'tests' | 'suites' | 'all') => {
+ for (const suite of mode === 'tests' ? [] : parentSuite.suites) {
if (!suite.title) {
// Flatten anonymous describes.
- visitSuite(project, suite, parentGroup);
+ visitSuite(project, suite, parentGroup, 'all');
continue;
}
@@ -101,10 +101,10 @@ export class TestTree {
};
this._addChild(parentGroup, group);
}
- visitSuite(project, suite, group);
+ visitSuite(project, suite, group, 'all');
}
- for (const test of parentSuite.tests) {
+ for (const test of mode === 'suites' ? [] : parentSuite.tests) {
const title = test.title;
let testCaseItem = parentGroup.children.find(t => t.kind !== 'group' && t.title === title) as TestCaseItem;
if (!testCaseItem) {
@@ -163,8 +163,16 @@ export class TestTree {
if (filterProjects && !projectFilters.get(projectSuite.title))
continue;
for (const fileSuite of projectSuite.suites) {
- const fileItem = this._fileItem(fileSuite.location!.file.split(pathSeparator), true);
- visitSuite(projectSuite.project()!, fileSuite, fileItem);
+ if (hideFiles) {
+ visitSuite(projectSuite.project()!, fileSuite, this.rootItem, 'suites');
+ if (fileSuite.tests.length) {
+ const defaultDescribeItem = this._defaultDescribeItem();
+ visitSuite(projectSuite.project()!, fileSuite, defaultDescribeItem, 'tests');
+ }
+ } else {
+ const fileItem = this._fileItem(fileSuite.location!.file.split(pathSeparator), true);
+ visitSuite(projectSuite.project()!, fileSuite, fileItem, 'all');
+ }
}
}
@@ -238,6 +246,26 @@ export class TestTree {
return fileItem;
}
+ private _defaultDescribeItem(): GroupItem {
+ let defaultDescribeItem = this._treeItemById.get('<anonymous>') as GroupItem;
+ if (!defaultDescribeItem) {
+ defaultDescribeItem = {
+ kind: 'group',
+ subKind: 'describe',
+ id: '<anonymous>',
+ title: '<anonymous>',
+ location: { file: '', line: 0, column: 0 },
+ duration: 0,
+ parent: this.rootItem,
+ children: [],
+ status: 'none',
+ hasLoadErrors: false,
+ };
+ this._addChild(this.rootItem, defaultDescribeItem);
+ }
+ return defaultDescribeItem;
+ }
+
sortAndPropagateStatus() {
sortAndPropagateStatus(this.rootItem);
}
diff --git a/packages/trace-viewer/src/ui/defaultSettingsView.tsx b/packages/trace-viewer/src/ui/defaultSettingsView.tsx
index 9d55570878..6334d3cded 100644
--- a/packages/trace-viewer/src/ui/defaultSettingsView.tsx
+++ b/packages/trace-viewer/src/ui/defaultSettingsView.tsx
@@ -27,6 +27,7 @@ export const DefaultSettingsView: React.FC<{}> = () => {
shouldPopulateCanvasFromScreenshot,
setShouldPopulateCanvasFromScreenshot,
] = useSetting('shouldPopulateCanvasFromScreenshot', false);
+ const [hideFiles, setHideFiles] = useSetting('hideFiles', false);
const [darkMode, setDarkMode] = useDarkModeSetting();
return (
@@ -38,6 +39,12 @@ export const DefaultSettingsView: React.FC<{}> = () => {
set: setDarkMode,
name: 'Dark mode'
},
+ {
+ type: 'check',
+ value: hideFiles,
+ set: setHideFiles,
+ name: 'Hide files'
+ },
{
type: 'check',
value: shouldPopulateCanvasFromScreenshot,
diff --git a/packages/trace-viewer/src/ui/uiModeView.tsx b/packages/trace-viewer/src/ui/uiModeView.tsx
index 78ae617520..b5938bd045 100644
--- a/packages/trace-viewer/src/ui/uiModeView.tsx
+++ b/packages/trace-viewer/src/ui/uiModeView.tsx
@@ -102,6 +102,7 @@ export const UIModeView: React.FC<{}> = ({
const [showBrowser, setShowBrowser] = useSetting<boolean>('show-browser', false);
const [updateSnapshots, setUpdateSnapshots] = useSetting<reporterTypes.FullConfig['updateSnapshots']>('updateSnapshots', 'missing');
+ const [hideFiles] = useSetting('hideFiles', false);
const inputRef = React.useRef<HTMLInputElement>(null);
@@ -240,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) };
- const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator);
+ return { testTree: new TestTree('', new TeleSuite('', 'root'), [], projectFilters, queryParams.pathSeparator, hideFiles) };
+ const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator, hideFiles);
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]);
+ }, [filterText, testModel, statusFilters, projectFilters, setVisibleTestIds, runningState, isRunningTest, hideFiles]);
const runTests = React.useCallback((mode: 'queue-if-busy' | 'bounce-if-busy', testIds: Set<string>) => {
if (!testServerConnection || !testModel)
@@ -326,7 +327,7 @@ export const UIModeView: React.FC<{}> = ({
// run affected watched tests
const testModel = teleSuiteUpdater.asModel();
- const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator);
+ const testTree = new TestTree('', testModel.rootSuite, testModel.loadErrors, projectFilters, queryParams.pathSeparator, hideFiles);
const testIds: string[] = [];
const set = new Set(params.testFiles);
@@ -350,7 +351,7 @@ export const UIModeView: React.FC<{}> = ({
runTests('queue-if-busy', new Set(testIds));
});
return () => disposable.dispose();
- }, [runTests, testServerConnection, watchAll, watchedTreeIds, teleSuiteUpdater, projectFilters]);
+ }, [runTests, testServerConnection, watchAll, watchedTreeIds, teleSuiteUpdater, projectFilters, hideFiles]);
// Shortcuts.
React.useEffect(() => {
diff --git a/tests/playwright-test/ui-mode-test-tree.spec.ts b/tests/playwright-test/ui-mode-test-tree.spec.ts
index 36942507b6..993f7ccb79 100644
--- a/tests/playwright-test/ui-mode-test-tree.spec.ts
+++ b/tests/playwright-test/ui-mode-test-tree.spec.ts
@@ -577,3 +577,57 @@ test('should resolve title conflicts', async ({ runUITest }) => {
- treeitem "[icon-circle-outline] bar 2"
`);
});
+
+test('should hide file names', async ({ runUITest }) => {
+ const { page } = await runUITest({
+ 'a.test.ts': `
+ import { test } from '@playwright/test';
+
+ test("first", () => {});
+
+ test.describe("group", () => {
+ test("second", () => {});
+ });
+
+ test("third", () => {});
+ `,
+ 'b.test.ts': `
+ import { test } from '@playwright/test';
+
+ test("fourth", () => {});
+
+ test.describe("group", () => {
+ test("fifth", () => {});
+ });
+
+ test("sixth", () => {});
+ `
+ });
+
+ await page.getByText('Settings', { exact: true }).click();
+ await page.getByLabel('Hide files').click();
+
+ await expect.poll(dumpTestTree(page)).toContain(`
+ ▼ ◯ <anonymous>
+ ◯ first
+ ◯ third
+ ◯ fourth
+ ◯ sixth
+ ▼ ◯ group
+ ◯ second
+ ◯ fifth
+ `);
+ await expect(page.getByTestId('test-tree')).toMatchAriaSnapshot(`
+ - tree:
+ - treeitem "[icon-circle-outline] <anonymous>" [expanded]:
+ - group:
+ - treeitem "[icon-circle-outline] first"
+ - treeitem "[icon-circle-outline] third"
+ - treeitem "[icon-circle-outline] fourth"
+ - treeitem "[icon-circle-outline] sixth"
+ - treeitem "[icon-circle-outline] group" [expanded]:
+ - group:
+ - treeitem "[icon-circle-outline] second"
+ - treeitem "[icon-circle-outline] fifth"
+ `);
+});