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
feat(html): add keyboard shortcuts (#36609)
Pavel Feldman2066c25457ba
diff --git a/packages/html-reporter/src/reportView.tsx b/packages/html-reporter/src/reportView.tsxindex 31b3df975f..867bb0fbf0 100644--- a/packages/html-reporter/src/reportView.tsx+++ b/packages/html-reporter/src/reportView.tsx@@ -18,9 +18,9 @@ import type { FilteredStats, TestCase, TestCaseSummary, TestFile, TestFileSummarimport * as React from 'react';import './colors.css';import './common.css';-import { Filter } from './filter';+import { Filter, filterWithQuery } from './filter';import { HeaderView, GlobalFilterView } from './headerView';-import { Route, SearchParamsContext } from './links';+import { navigate, Route, SearchParamsContext, testResultHref } from './links';import type { LoadedReport } from './loadedReport';import './reportView.css';import { TestCaseView } from './testCaseView';@@ -49,6 +49,10 @@ 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 testId = searchParams.get('testId');+ const q = searchParams.get('q')?.toString() || '';+ const filterParam = q ? '&q=' + q : '';+ const reportTitle = report?.json()?.title;const testIdToFileIdMap = React.useMemo(() => {const map = new Map<string, string>();@@ -61,7 +65,7 @@ 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 filteredTests = React.useMemo(() => {+ 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));@@ -72,7 +76,49 @@ export const ReportView: React.FC<{return result;}, [report, filter]);- const reportTitle = report?.json()?.title;+ const { prev, next } = React.useMemo(() => {+ const index = testModel.tests.findIndex(t => t.testId === testId);+ const prev = index > 0 ? testModel.tests[index - 1] : undefined;+ const next = index < testModel.tests.length - 1 ? testModel.tests[index + 1] : undefined;+ return { prev, next };+ }, [testId, testModel]);++ React.useEffect(() => {+ const handleKeyDown = (event: KeyboardEvent) => {+ if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement)+ return;++ switch (event.key) {+ case 'a':+ event.preventDefault();+ navigate('#?');+ break;+ case 'p':+ event.preventDefault();+ navigate(filterWithQuery(q, 's:passed', false));+ break;+ case 'f':+ event.preventDefault();+ navigate(filterWithQuery(q, 's:failed', false));+ break;+ case 'ArrowLeft':+ if (prev) {+ event.preventDefault();+ navigate(testResultHref({ test: prev }) + filterParam);+ }+ break;+ case 'ArrowRight':+ if (next) {+ event.preventDefault();+ navigate(testResultHref({ test: next }) + filterParam);+ }+ break;+ }+ };++ document.addEventListener('keydown', handleKeyDown);+ return () => document.removeEventListener('keydown', handleKeyDown);+ }, [prev, next, filterParam, q]);React.useEffect(() => {if (reportTitle)@@ -87,14 +133,14 @@ export const ReportView: React.FC<{<Route predicate={testFilesRoutePredicate}><TestFilesHeader report={report?.json()} filteredStats={filteredStats} metadataVisible={metadataVisible} toggleMetadataVisible={() => setMetadataVisible(visible => !visible)}/><TestFilesView- tests={filteredTests.files}+ tests={testModel.files}expandedFiles={expandedFiles}setExpandedFiles={setExpandedFiles}projectNames={report?.json().projectNames || []}/></Route><Route predicate={testCaseRoutePredicate}>- {!!report && <TestCaseViewLoader report={report} tests={filteredTests.tests} testIdToFileIdMap={testIdToFileIdMap} />}+ {!!report && <TestCaseViewLoader report={report} next={next} prev={prev} testId={testId} testIdToFileIdMap={testIdToFileIdMap} />}</Route></main></div>;@@ -102,21 +148,15 @@ export const ReportView: React.FC<{const TestCaseViewLoader: React.FC<{report: LoadedReport,- tests: TestCaseSummary[],+ testId: string | null,+ next?: TestCaseSummary,+ prev?: TestCaseSummary,testIdToFileIdMap: Map<string, string>,-}> = ({ report, testIdToFileIdMap, tests }) => {+}> = ({ report, testIdToFileIdMap, next, prev, testId }) => {const searchParams = React.useContext(SearchParamsContext);const [test, setTest] = React.useState<TestCase | 'loading' | 'not-found'>('loading');- const testId = searchParams.get('testId');const run = +(searchParams.get('run') || '0');- const { prev, next } = React.useMemo(() => {- const index = tests.findIndex(t => t.testId === testId);- const prev = index > 0 ? tests[index - 1] : undefined;- const next = index < tests.length - 1 ? tests[index + 1] : undefined;- return { prev, next };- }, [testId, tests]);-React.useEffect(() => {(async () => {if (!testId || (typeof test === 'object' && testId === test.testId))diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.tsindex e4194be748..3845c4c266 100644--- a/tests/playwright-test/reporter-html.spec.ts+++ b/tests/playwright-test/reporter-html.spec.ts@@ -3064,6 +3064,68 @@ for (const useIntermediateMergeReport of [true, false] as const) {await page.getByRole('link', { name: 'fail without snippet' }).click();await expect(page.getByTestId('test-snippet')).not.toBeVisible();});++ test('should support keyboard shortcuts', async ({ runInlineTest, showReport, page }) => {+ await runInlineTest({+ 'playwright.config.ts': `+ module.exports = { name: 'project-name' };+ `,+ 'a.test.js': `+ import { test, expect } from '@playwright/test';+ test('passes', async ({}) => {});+ test('fails', async ({}) => {+ expect(1).toBe(2);+ });+ test('skipped', async ({}) => {+ test.skip('Does not work')+ });+ test('flaky', async ({}, testInfo) => {+ expect(testInfo.retry).toBe(1);+ });+ `,+ }, { reporter: 'dot,html', retries: 1 }, { PLAYWRIGHT_HTML_OPEN: 'never' });++ await showReport();++ // Focus the page+ await page.getByRole('link', { name: 'All' }).click();++ await test.step('next', async () => {+ await page.keyboard.press('ArrowRight');+ await expect(page.locator('.header-title')).toHaveText('fails');+ await page.keyboard.press('ArrowRight');+ await expect(page.locator('.header-title')).toHaveText('flaky');+ await page.keyboard.press('ArrowRight');+ await expect(page.locator('.header-title')).toHaveText('passes');+ // Bounce+ await page.keyboard.press('ArrowRight');+ await expect(page.locator('.header-title')).toHaveText('passes');+ });++ await test.step('prev', async () => {+ await page.keyboard.press('ArrowLeft');+ await expect(page.locator('.header-title')).toHaveText('flaky');+ await page.keyboard.press('ArrowLeft');+ await expect(page.locator('.header-title')).toHaveText('fails');+ await page.keyboard.press('ArrowLeft');+ await expect(page.locator('.header-title')).toHaveText('fails');+ });++ await test.step('p', async () => {+ await page.keyboard.press('p');+ await expect(page.locator('.test-file-test')).toHaveCount(1);+ });++ await test.step('a', async () => {+ await page.keyboard.press('a');+ await expect(page.locator('.test-file-test')).toHaveCount(3);+ });++ await test.step('f', async () => {+ await page.keyboard.press('f');+ await expect(page.locator('.test-file-test')).toHaveCount(1);+ });+ });});}