/** * Every section variant in every template (WEBSITE-1.0-PLAN §3, "Checks per variant", and §13): the * demo church's homepage with all eight sections showing, one variant changed at a time. For each, * axe finds no violations (WCAG 2.2 AA) and the section looks as it did last time at phone, tablet * and desktop width: 26 variants × 4 templates × 3 widths = 312 screenshots, compared with * `toHaveScreenshot` against the baselines in e2e/variants.spec.ts-snapshots/. * * The baselines aren't in git (about 20 MB, and each operating system draws text a little * differently): they're kept on the machine that runs the release checks. Take them at a release * (`npx playwright test variants --update-snapshots`, then look through them), and the next * release's run shows every section that changed. The e2e server freezes the clock (E2E_NOW), so * the demo's dates never change between runs. What comes from outside is masked: the video, the * map (OpenStreetMap) and the messages' thumbnails. */ import AxeBuilder from "@axe-core/playwright"; import { expect, test, type Page } from "@playwright/test"; import { SECTION_TYPES, SECTION_VARIANTS, TEMPLATE_VARIANTS } from "../backend/src/site/variants.js"; const BACKEND = "http://127.0.0.1:5310"; const TEMPLATES = Object.keys(TEMPLATE_VARIANTS) as (keyof typeof TEMPLATE_VARIANTS)[]; const WIDTHS = [["phone", { width: 390, height: 844 }], ["tablet", { width: 820, height: 1180 }], ["desktop", { width: 1366, height: 900 }]] as const; // Every section showing, in a fixed order, so the nth
in
is the nth type here. const HOME = SECTION_TYPES.map((section) => ({ section, hidden: false })); async function design(page: Page, variants: Record) { const res = await page.request.post(`${BACKEND}/test/design`, { data: { variants, home: HOME } }); expect(res.ok()).toBeTruthy(); } /** * Lazy images load only once scrolled to: go through the page, then wait for every image. The * site's own images must really have loaded (a broken one is "complete" too). */ async function loadImages(page: Page) { await page.evaluate(async () => { for (let y = 0; y < document.body.scrollHeight; y += 500) { window.scrollTo(0, y); await new Promise((r) => setTimeout(r, 40)); } window.scrollTo(0, 0); }); await page.waitForFunction(() => Array.from(document.images).every((i) => i.complete), null, { timeout: 30_000 }); const broken = await page.evaluate(() => Array.from(document.images) .filter((i) => new URL(i.currentSrc || i.src, location.href).origin === location.origin && i.naturalWidth === 0) .map((i) => i.currentSrc || i.src)); expect(broken, "images that didn't load").toEqual([]); } /** * A section's screenshot is as big as its box, rounded where it starts and ends on the page. With * fractional heights above it (text, aspect ratios), the same section came out a pixel taller or * shorter from run to run, which fails `toHaveScreenshot` however small the difference. So the * header and every section get their own height rounded up first: each section then starts on a * whole pixel and is the same size every run. */ async function wholePixels(page: Page) { await page.evaluate(() => { // The header and the sections, not what holds them (main grows to fit). const blocks = Array.from(document.querySelectorAll("header:not(main header), main > section")); for (const b of blocks) b.style.height = ""; const heights = blocks.map((b) => b.getBoundingClientRect().height); blocks.forEach((b, n) => { if (getComputedStyle(b).position !== "fixed") b.style.height = `${Math.ceil(heights[n]!)}px`; }); }); } for (const template of TEMPLATES) { test.describe(`${template} variants`, () => { test.beforeAll(async ({ request }) => { expect((await request.post(`${BACKEND}/test/demo?template=${template}`)).ok()).toBeTruthy(); }); for (const [i, type] of SECTION_TYPES.entries()) { for (const variant of SECTION_VARIANTS[type]) { test(`${type}: ${variant}`, async ({ page }) => { await design(page, { [type]: variant }); await page.setViewportSize(WIDTHS[2][1]); expect((await page.goto("/"))?.status()).toBe(200); await expect(page.locator("html")).toHaveAttribute("data-template", template); const sections = page.locator("main > section"); await expect(sections).toHaveCount(SECTION_TYPES.length); await loadImages(page); const axe = await new AxeBuilder({ page }).withTags(["wcag2a", "wcag2aa", "wcag21aa", "wcag22aa"]).analyze(); expect(axe.violations.map((v) => `${v.id} (${v.nodes.length}) ${v.nodes[0]?.target}`)).toEqual([]); const section = sections.nth(i); for (const [name, size] of WIDTHS) { await page.setViewportSize(size); await loadImages(page); await wholePixels(page); await expect(section).toHaveScreenshot(`${template}-${type}-${variant}-${name}.png`, { mask: [page.locator("main video"), page.locator("main [data-map]"), page.locator('main img[src*="/api/thumbnails/"]')], animations: "disabled", maxDiffPixelRatio: 0.01, }); } }); } } }); }