Manuals / Playwright with Python / Chapter 27

Part 4 · Advanced Techniques · advanced · ~45 min · Chapter 27 of 61

19. Visual & Accessibility Testing

Screenshot comparison expect(page).to_have_screenshot("homepage.png") expect(page).to_have_screenshot(name, max_diff_pixels=..., mask=...) What it does: Compares a screenshot of the current page (or element) against a saved baseline image; fails if they differ beyond a tolerance threshold. Types/params: ● name (string, optional) — filename for the baseline image; auto-generated from the test name

Path progress
43%

Step 1 of 5

expect(page).to_have_screenshot("homepage.png")

mask=...) What it does: Compares a screenshot of the current page (or element) against a saved baseline image; fails if they differ beyond a tolerance threshold. Types/params: (see dynamic content handling below) Pointers: The first run creates the baseline image (nothing to compare against yet) — subsequent runs compare against it. Baselines need to be committed to version control and regenerated deliberately when a UI change is intentional, not just whenever a test fails.

  • name (string, optional) — filename for the baseline image; auto-generated from the test name if omitted
  • max_diff_pixels (integer, optional) — allowed pixel difference before failing, useful for minor anti-aliasing differences across environments
  • mask (list of Locators, optional) — regions to exclude from comparison entirely
  • full_page (boolean, optional) — capture the entire scrollable page vs just the viewport
expect(page).to_have_screenshot("homepage.png")Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →

Example

expect(page).to_have_screenshot(name, max_diff_pixels=...,
Was this step clear?
Chapter learning outcomes
  • Handling dynamic content in visual diffs
  • Integrating axe-core for accessibility checks
  • Axe().run(page)

Clear these before you leave

Side quest

19. Visual & Accessibility Testing deliverable

Apply one idea from “expect(page).to_have_screenshot("homepage.png")” in a small script or note.