Visual Regression Testing with Playwright

Even when functionality is correct, visual regressions can break user experience and brand consistency. Playwright’s screenshot and comparison tools let you detect unintended UI changes automatically as part of your test suite.

Capturing Baseline and Comparison Screenshots

You can use expect(page).toHaveScreenshot() or locator.screenshot() to create and compare images. On the first run, Playwright stores baseline images; on later runs, it flags differences.

// visual-regression.spec.ts
import { test, expect } from '@playwright/test';

test('homepage visual regression', async ({ page }) => {
  await page.goto('https://demo.myshop.com');
  await expect(page).toHaveScreenshot('homepage.png');
});
Note: Visual comparisons work best when the environment is stable: same viewport, fonts, data and theme across runs.
Tip: Prefer smaller, component-level screenshots (such as a product card or header) over full-page captures to make diffs more precise and easier to review.
Warning: Dynamic content (such as rotating banners or timestamps) can cause noisy diffs; mask or stabilise those regions before relying on visual tests.

Visual tests are most valuable around critical pages such as checkout, dashboards and high-traffic landing pages.

Common Mistakes

Mistake 1 β€” Relying only on full-page screenshots

This creates large, noisy diffs.

❌ Wrong: Capturing the entire page where many unrelated elements change frequently.

βœ… Correct: Focus on stable, important components or regions.

Mistake 2 β€” Running visual tests in unstable environments

This leads to flaky results.

❌ Wrong: Changing fonts, themes or feature flags between runs.

βœ… Correct: Keep visual tests on a controlled environment with consistent assets and data.

🧠 Test Yourself

How can you make Playwright visual tests more reliable?