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
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
Example
expect(page).to_have_screenshot(name, max_diff_pixels=...,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.