1
---
2
name: visual-regression-testing
3
description: >
4
Detect unintended visual changes in UI by comparing screenshots across
5
versions. Use for visual regression, screenshot diff, Percy, Chromatic, UI
6
testing, and visual validation.
7
---
8
9
# Visual Regression Testing
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
Visual regression testing captures screenshots of UI components and pages, then compares them across versions to detect unintended visual changes. This automated approach catches CSS bugs, layout issues, and design regressions that traditional functional tests miss.
22
23
## When to Use
24
25
- Detecting CSS regression bugs
26
- Validating responsive design across viewports
27
- Testing across different browsers
28
- Verifying component visual consistency
29
- Catching layout shifts and overlaps
30
- Testing theme changes
31
- Validating design system components
32
- Reviewing visual changes in PRs
33
34
## Quick Start
35
36
Minimal working example:
37
38
```typescript
39
// tests/visual/homepage.spec.ts
40
import { test, expect } from "@playwright/test";
41
42
test.describe("Homepage Visual Tests", () => {
43
test("homepage matches baseline", async ({ page }) => {
44
await page.goto("/");
45
46
// Wait for images to load
47
await page.waitForLoadState("networkidle");
48
49
// Full page screenshot
50
await expect(page).toHaveScreenshot("homepage-full.png", {
51
fullPage: true,
52
maxDiffPixels: 100, // Allow small differences
53
});
54
});
55
56
test("responsive design - mobile", async ({ page }) => {
57
await page.setViewportSize({ width: 375, height: 667 }); // iPhone SE
58
await page.goto("/");
59
60
await expect(page).toHaveScreenshot("homepage-mobile.png");
61
});
62
63
test("responsive design - tablet", async ({ page }) => {
64
// ... (see reference guides for full implementation)
65
```
66
67
## Reference Guides
68
69
Detailed implementations in the `references/` directory:
70
71
| Guide | Contents |
72
|---|---|
73
| [Playwright Visual Testing](references/playwright-visual-testing.md) | Playwright Visual Testing |
74
| [Percy Visual Testing](references/percy-visual-testing.md) | Percy Visual Testing |
75
| [Chromatic for Storybook](references/chromatic-for-storybook.md) | Chromatic for Storybook |
76
| [Cypress Visual Testing](references/cypress-visual-testing.md) | Cypress Visual Testing |
77
| [BackstopJS Configuration](references/backstopjs-configuration.md) | BackstopJS Configuration |
78
| [Handling Dynamic Content](references/handling-dynamic-content.md) | Handling Dynamic Content |
79
| [Testing Responsive Components](references/testing-responsive-components.md) | Testing Responsive Components |
80
81
## Best Practices
82
83
### ✅ DO
84
85
- Hide or mock dynamic content (timestamps, ads)
86
- Test across multiple viewports
87
- Wait for animations and images to load
88
- Use consistent viewport sizes
89
- Disable animations during capture
90
- Test interactive states (hover, focus)
91
- Review diffs carefully before approving
92
- Store baselines in version control
93
94
### ❌ DON'T
95
96
- Test pages with constantly changing content
97
- Ignore small legitimate differences
98
- Skip responsive testing
99
- Forget to update baselines after design changes
100
- Test pages with random data
101
- Use overly strict thresholds (0% diff)
102
- Skip browser/device variations
103
- Commit unapproved diffs
104