test(app): Playwright and axe browser suite on Chromium (D14)
@playwright/test 1.64.1 and @axe-core/playwright 4.14.0, dev only and pinned. pnpm e2e:ui runs a Vite server of its own on 127.0.0.1:5191 with no backend: the Dialog contract on a dev-only test page and the local tool's dialogs, desktop and 375 px, FR and EN, axe at zero serious violation outside one listed pre-existing finding. Requests outside the allowlist are aborted and fail the test.
This commit is contained in:
1 parent
6f885eccb2
commit
72ee9f9971
11 files changed
+740
-3
No files matched your search
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* Shared pieces of the Playwright suites (docs/development/BROWSER_TESTS.md):
|
||||
* the two Chromium projects, the request guard every test runs under, the axe
|
||||
* gate, and the focus and layout probes.
|
||||
*/
|
||||
import AxeBuilder from '@axe-core/playwright'
|
||||
import {
|
||||
devices,
|
||||
expect,
|
||||
test as base,
|
||||
type Page,
|
||||
type Project,
|
||||
} from '@playwright/test'
|
||||
|
||||
/** Desktop and the 375 × 667 phone of the V1-1 contract (§12.5); Chromium only until V1-5. */
|
||||
export const PROJECTS: Project[] = [
|
||||
{
|
||||
name: 'chromium-desktop',
|
||||
use: {
|
||||
...devices['Desktop Chrome'],
|
||||
viewport: { width: 1280, height: 800 },
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'chromium-375',
|
||||
use: {
|
||||
...devices['Desktop Chrome'],
|
||||
viewport: { width: 375, height: 667 },
|
||||
deviceScaleFactor: 2,
|
||||
isMobile: true,
|
||||
hasTouch: true,
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
export interface GuardOptions {
|
||||
/** Origins the page may reach; every other request is aborted and fails the test. */
|
||||
allowedOrigins: string[]
|
||||
/** Strings no request URL may carry (fixture names, emails, search terms). */
|
||||
forbiddenInUrls: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Every test runs under a request guard: a request to an origin outside the
|
||||
* allowlist (the DEV stack, a CDN, anything) is aborted, and the test fails
|
||||
* listing it, as it fails on a URL holding a fixture value.
|
||||
*/
|
||||
export const test = base.extend<GuardOptions & { requestGuard: void }>({
|
||||
allowedOrigins: [[], { option: true }],
|
||||
forbiddenInUrls: [[], { option: true }],
|
||||
requestGuard: [
|
||||
async ({ context, allowedOrigins, forbiddenInUrls }, use) => {
|
||||
const refused: string[] = []
|
||||
const leaking: string[] = []
|
||||
await context.route(
|
||||
(url) => !allowedOrigins.includes(url.origin),
|
||||
(route) => {
|
||||
refused.push(route.request().url())
|
||||
return route.abort('blockedbyclient')
|
||||
},
|
||||
)
|
||||
context.on('request', (request) => {
|
||||
const url = decodeURIComponent(request.url())
|
||||
if (forbiddenInUrls.some((value) => value && url.includes(value)))
|
||||
leaking.push(url)
|
||||
})
|
||||
await use()
|
||||
expect(refused, 'requests outside the allowed origins').toEqual([])
|
||||
expect(leaking, 'request URLs carrying a fixture value').toEqual([])
|
||||
},
|
||||
{ auto: true },
|
||||
],
|
||||
})
|
||||
|
||||
export { expect }
|
||||
|
||||
/**
|
||||
* Serious violations found before V1-1.3, outside its code, listed in TODO RR-7
|
||||
* until their own fix: the scan lets these exact nodes through and fails on any
|
||||
* other. The language switcher's buttons show « FR » / « EN » but are named
|
||||
* « Passer en français » / « Switch to English » (WCAG 2.5.3 label in name).
|
||||
*/
|
||||
const KNOWN_VIOLATIONS: { id: string; target: RegExp }[] = [
|
||||
{
|
||||
id: 'label-content-name-mismatch',
|
||||
target: /^button\[aria-label="(Passer en français|Switch to English)"\]$/,
|
||||
},
|
||||
]
|
||||
|
||||
const isKnown = (id: string, target: string) =>
|
||||
KNOWN_VIOLATIONS.some((known) => known.id === id && known.target.test(target))
|
||||
|
||||
/**
|
||||
* Zero serious or critical axe violation on the page as it stands (WCAG 2.0 to
|
||||
* 2.2 A and AA rules plus axe best practices), known ones aside; the failure
|
||||
* message lists them.
|
||||
*/
|
||||
export async function expectAccessible(
|
||||
page: Page,
|
||||
state: string,
|
||||
): Promise<void> {
|
||||
// Colours are measured at rest: an entrance animation still running would blend them.
|
||||
await page.waitForFunction(() =>
|
||||
document
|
||||
.getAnimations()
|
||||
.every((animation) => animation.playState !== 'running'),
|
||||
)
|
||||
const { violations } = await new AxeBuilder({ page })
|
||||
.withTags([
|
||||
'wcag2a',
|
||||
'wcag2aa',
|
||||
'wcag21a',
|
||||
'wcag21aa',
|
||||
'wcag22aa',
|
||||
'best-practice',
|
||||
])
|
||||
.analyze()
|
||||
const blocking = violations
|
||||
.filter((v) => v.impact === 'serious' || v.impact === 'critical')
|
||||
.flatMap((v) =>
|
||||
v.nodes
|
||||
.map((node) => node.target.join(' '))
|
||||
.filter((target) => !isKnown(v.id, target))
|
||||
.map((target) => `${v.impact} ${v.id}: ${v.help} → ${target}`),
|
||||
)
|
||||
expect(blocking, `axe on ${state}`).toEqual([])
|
||||
}
|
||||
|
||||
/**
|
||||
* Where the focus is: in the open modal dialog, in the browser's own interface
|
||||
* (the document lost focus, as Tab past a modal dialog's last control does), or
|
||||
* on something else of the page.
|
||||
*/
|
||||
export function focusOwner(page: Page): Promise<string> {
|
||||
return page.evaluate(() => {
|
||||
const active = document.activeElement
|
||||
if (!document.hasFocus()) return 'browser'
|
||||
if (!active || active === document.body) return 'body'
|
||||
if (active.closest('dialog:modal')) return 'dialog'
|
||||
return `outside:${active.tagName.toLowerCase()}:${active.textContent?.trim().slice(0, 40)}`
|
||||
})
|
||||
}
|
||||
|
||||
/** Accessible name of the focused element (aria-label, else its text). */
|
||||
export function focusedName(page: Page): Promise<string> {
|
||||
return page.evaluate(() => {
|
||||
const active = document.activeElement as HTMLElement | null
|
||||
return (
|
||||
active?.getAttribute('aria-label') ?? active?.textContent?.trim() ?? ''
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/** No horizontal page scroll: the document is no wider than the viewport. */
|
||||
export async function expectNoHorizontalOverflow(
|
||||
page: Page,
|
||||
state: string,
|
||||
): Promise<void> {
|
||||
const { scrollWidth, clientWidth } = await page.evaluate(() => ({
|
||||
scrollWidth: document.documentElement.scrollWidth,
|
||||
clientWidth: document.documentElement.clientWidth,
|
||||
}))
|
||||
expect(scrollWidth, `horizontal overflow on ${state}`).toBeLessThanOrEqual(
|
||||
clientWidth,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user