1---2name: playwright-visual-testing-23description: Add, repair, or review Playwright visual regression tests for browser-facing .NET apps, including screenshot baselines, Pixelmatch thresholds, deterministic rendering, and GitHub Actions artifacts. USE FOR: toHaveScreenshot, page.screenshot visual checks, Pixelmatch/pngjs comparison scripts, visual baseline updates, screenshot diff triage, or CI workflows for UI regression screenshots. DO NOT USE FOR: pure unit tests, accessibility audits, browser-debugging sessions, or frontend linting.4---56# Playwright Visual Testing78## Trigger On910- the user asks for pixel, screenshot, visual, or UI regression testing with Playwright11- a .NET repo needs visual baselines for ASP.NET Core, Blazor, WebAssembly, static pages, or generated frontend assets12- GitHub Actions should run Playwright screenshots and expose expected, actual, and diff artifacts13- tests fail with screenshot mismatches, noisy baselines, or unstable visual snapshots1415## Do Not Use For1617- pure .NET unit or integration tests without a browser surface18- accessibility, SEO, PWA, or security-header audits; route those to `webhint`19- browser debugging or live DOM inspection; route that to `chrome-devtools-mcp`20- JavaScript, TypeScript, CSS, or HTML linting; route those to `biome`, `eslint`, `stylelint`, or `htmlhint`2122## Load References2324- Read [CI and snapshot patterns](references/ci-and-snapshot-patterns.md) when adding a new visual test suite, wiring GitHub Actions, choosing between Playwright snapshots and a standalone Pixelmatch script, or stabilizing screenshot diffs.2526## Workflow2728```mermaid29flowchart TD30 A["Need visual regression coverage"] --> B{"Uses Playwright Test"}31 B -->|"Yes"| C["Prefer expect(page).toHaveScreenshot"]32 B -->|"No or custom compare needed"| D["Capture page.screenshot output"]33 D --> E["Compare with pixelmatch and pngjs"]34 C --> F["Stabilize viewport, data, animation, and volatile regions"]35 E --> F36 F --> G["Commit reviewed baselines"]37 G --> H["Run in CI and upload reports or image diffs"]38 H --> I["Triage expected, actual, and diff before changing thresholds"]39```40411. Inspect the current browser-test surface:42 - nearest `AGENTS.md`43 - `package.json`, lockfile, Playwright config, test folders, and CI workflows44 - how the app starts locally: `dotnet run`, Aspire AppHost, static preview, or frontend dev server452. Choose the comparison path deliberately:46 - default to Playwright Test `expect(page).toHaveScreenshot()` when the repo can use Playwright Test snapshots47 - use `page.screenshot()` plus a standalone Pixelmatch script only when the repo needs article-style central `screenshots/baseline`, `screenshots/actual`, and `screenshots/diff` folders, non-Playwright image inputs, or custom reporting outside Playwright Test483. Make screenshots deterministic before tuning thresholds:49 - fix viewport, browser project, locale/time zone, color scheme, and device scale factor50 - use stable test data and wait for the app-specific ready state51 - disable animations or use Playwright screenshot options for animations52 - mask or hide volatile regions such as ads, time, avatars, random IDs, spinners, and third-party iframes534. Keep baseline updates explicit:54 - generate missing baselines once, review them, and commit them55 - update intended Playwright snapshots with `npx playwright test --update-snapshots`56 - do not auto-create or auto-update baselines in pull-request CI575. Wire CI for repeatability:58 - use `npm ci`, then `npx playwright install --with-deps`, then the focused Playwright command59 - set CI workers conservatively when screenshots are resource-sensitive60 - upload the Playwright HTML report and `test-results/`, or upload `screenshots/baseline`, `screenshots/actual`, and `screenshots/diff` for a custom Pixelmatch flow616. Triage failures from artifacts:62 - inspect expected, actual, and diff images together63 - classify the mismatch as intentional design change, rendering nondeterminism, app bug, or baseline drift64 - fix nondeterminism before increasing `maxDiffPixels`, `maxDiffPixelRatio`, or Pixelmatch mismatch thresholds6566## Deliver6768- a Playwright visual-test path that matches the repo's existing package manager and test layout69- committed reviewed baseline images or a clear command to generate and review them70- deterministic screenshot controls for dynamic UI regions71- GitHub Actions report or diff artifacts that make failures reviewable72- a short note on whether the implementation uses built-in Playwright snapshots or a custom Pixelmatch comparison script7374## Validate7576- `npm ci`77- `npx playwright install --with-deps`78- `npx playwright test` or the repo's focused visual-test script79- `npx playwright test --update-snapshots` only when accepting intentional baseline changes80- in CI changes, confirm artifact upload uses maintained GitHub Actions versions and runs on pull requests without requiring secrets8182## Common Pitfalls8384- capturing screenshots before the UI is stable85- generating baselines on one OS and comparing them on another86- sharing one mutable browser context across tests87- masking too much of the page and removing the regression signal88- raising thresholds to hide animation, font, clock, or data nondeterminism89- using a custom Pixelmatch script when Playwright's built-in screenshot assertion would give better trace, report, and snapshot integration