The Visual Testing Engineer catches UI regressions that functional tests miss - layout shifts, colour changes, overlapping elements, missing icons. Visual tests compare screenshots pixel-by-pixel and flag differences for human review. They are complementary to functional tests, not a replacement.
Playwright Visual Tests
import { test, expect } from '@playwright/test';
// Screenshot comparison - fails if visual diff > threshold
test('product card matches snapshot', async ({ page }) => {
await page.goto('/products/123');
await page.waitForLoadState('networkidle'); // Wait for images to load
await expect(page.getByTestId('product-card')).toHaveScreenshot('product-card.png', {
maxDiffPixelRatio: 0.02, // 2% pixel difference allowed (for anti-aliasing)
});
});
// Full page screenshot
test('homepage matches snapshot', async ({ page }) => {
await page.goto('/');
await expect(page).toHaveScreenshot('homepage.png', {
fullPage: true,
mask: [page.locator('[data-testid="live-price"]')], // Mask dynamic content
});
});
Storybook + Chromatic
// Write Storybook stories for every design system component
// stories/Button.stories.tsx
import type { Meta, StoryObj } from '@storybook/react';
import { Button } from '../src/components/Button';
const meta: Meta<typeof Button> = {
component: Button,
argTypes: { variant: { control: 'select', options: ['primary', 'secondary', 'danger'] } },
};
export default meta;
export const Primary: StoryObj<typeof Button> = {
args: { children: 'Click me', variant: 'primary' },
};
export const Disabled: StoryObj<typeof Button> = {
args: { children: 'Disabled', disabled: true },
};
// .github/workflows/chromatic.yml
- name: Run Chromatic
uses: chromaui/action@v11
with:
projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
exitZeroOnChanges: true # Don't fail CI - require manual review
onlyChanged: true # Only test stories in changed files
Anti-Flake Strategies
- Always wait for
networkidleor element visibility before screenshotting - partial loads cause false positives - Mask dynamic content (timestamps, prices, user avatars) with solid blocks before comparison
- Use
maxDiffPixelRatio: 0.02threshold to allow minor font rendering differences across OS - Run on consistent OS - screenshot pixels differ between macOS and Linux. Use Docker containers for CI consistency.
- Store baseline screenshots in version control; review changes in PR before merging