| name | visual-regression |
| description | Screenshot comparison testing with Playwright, Percy, and Chromatic for catching unintended UI changes. |
| metadata | {"thinkfleetbot":{"emoji":"👁️","requires":{"anyBins":["npx","playwright"]}}} |
Visual Regression Testing
Catch unintended UI changes by comparing screenshots across builds.
Playwright Visual Comparisons
Generate baseline screenshots
npx playwright test --update-snapshots
npx playwright test visual.spec.ts --update-snapshots
Run comparisons
npx playwright test visual.spec.ts
Example test
import { test, expect } from '@playwright/test';
test('homepage visual', async ({ page }) => {
await page.goto('http://localhost:3000');
await expect(page).toHaveScreenshot('homepage.png', {
fullPage: true,
maxDiffPixelRatio: 0.01,
});
});
test('component visual', async ({ page }) => {
await page.goto('http://localhost:3000/components');
const card = page.locator('.card').first();
await expect(card).toHaveScreenshot('card-component.png');
});
test('responsive visual', async ({ page }) => {
await page.setViewportSize({ width: 375, height: 812 });
await page.goto('http://localhost:3000');
await expect(page).toHaveScreenshot('homepage-mobile.png', { fullPage: true });
});
Percy (Cloud Visual Testing)
npm install --save-dev @percy/cli @percy/playwright
npx percy exec -- npx playwright test
export PERCY_TOKEN=your_token_here
Percy in test
import { percySnapshot } from '@percy/playwright';
test('homepage', async ({ page }) => {
await page.goto('http://localhost:3000');
await percySnapshot(page, 'Homepage');
});
Chromatic (Storybook Visual Testing)
npm install --save-dev chromatic
npx chromatic --project-token=$CHROMATIC_TOKEN
npx chromatic --project-token=$CHROMATIC_TOKEN --auto-accept-changes
Manual Screenshot Comparison
npx playwright screenshot http://localhost:3000 before.png
npx playwright screenshot http://localhost:3000 after.png
compare before.png after.png diff.png
identify -verbose diff.png | grep -i "mean"
Notes
- Snapshots are OS and browser-specific. Generate baselines in CI, not locally.
- Use
maxDiffPixelRatio not exact match — antialiasing causes 1-pixel diffs across environments.
- Percy/Chromatic handle cross-browser and responsive comparisons automatically but require cloud accounts.
- Visual tests are slow. Run them as a separate CI job, not with unit tests.
- Review visual diffs carefully — auto-approving defeats the purpose.