Playwright + JS · Lesson 32 of 35
Module 32: Visual Testing
Try it yourself
Implement isDiffAcceptable:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
toHaveScreenshot()import { test, expect } from "@playwright/test";
test("landing page visual comparison", async ({ page }) => {
await page.goto("https://targetqa.com");
await expect(page).toHaveScreenshot("landing.png", {
mask: [page.getByTestId("live-clock")],
maxDiffPixelRatio: 0.02,
});
});Update baselines when UI intentionally changes:
npx playwright test --update-snapshotsFunctional tests check if elements exist, but visual tests verify that your layout actually looks right. In this lesson, we will explore Playwright's native toHaveScreenshot visual assertions, pixel diffing, baseline snapshots, and how to mask dynamic elements like clocks and timestamps. Let's master visual regression testing!
Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Different operating systems render antialiasing and system fonts slightly differently. Visual tests should ideally run in standard Docker containers.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.