toHaveScreenshot()expect:import { test, expect } from "@playwright/test";test("landing page visual comparison", async ({ page }) => {
await page.goto("https://targetqa.com");
// Compares current page screenshot with saved golden snapshot:
await expect(page).toHaveScreenshot("landing-page.png");
});
---
test("dashboard with masked dynamic elements", async ({ page }) => {
await page.goto("/dashboard");await expect(page).toHaveScreenshot("dashboard.png", {
// Replaces dynamic elements with pink bounding boxes during comparison
mask: [
page.getByTestId("current-timestamp"),
page.getByTestId("live-stock-ticker"),
],
maxDiffPixelRatio: 0.02, // Allow 2% pixel tolerance
});
});
---
npx playwright test --update-snapshots