npx skills add https://github.com/adaptationio/skrillz --skill visual-testingSKILL.md
Visual Testing
Overview
Visual regression testing catches unintended UI changes by comparing screenshots. This skill covers three approaches:
- Chromatic - Cloud-based, unlimited parallelization (recommended)
- Lost Pixel - Self-hosted, open source alternative
- Playwright Built-in - Simple snapshot testing
Quick Start: Chromatic (5 Minutes)
1. Install Chromatic
npm install --save-dev chromatic
2. Get Project Token
- Go to chromatic.com
- Sign up with GitHub (free tier: 5000 snapshots/month)
- Create project → Copy project token
3. Run First Build
# Set token
export CHROMATIC_PROJECT_TOKEN=your_token_here
# Run visual tests with Playwright
npx chromatic --playwright
4. Review Changes
- Chromatic UI shows visual diffs
- Approve or reject changes
- Approved changes become new baselines
Chromatic with Playwright
Configuration
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './tests/visual',
use: {
screenshot: 'on',
},
reporter: [
['html'],
['chromatic', { projectToken: process.env.CHROMATIC_PROJECT_TOKEN }],
],
});
Visual Test Example
// tests/visual/homepage.spec.ts
import { test, expect } from '@playwright/test';
test('homepage visual', async ({ page }) => {
await page.goto('/');
// Full page screenshot
await expect(page).toHaveScreenshot('homepage.png');
});
test('dashboard visual', async ({ page }) => {
await page.goto('/dashboard');
// Wait for data to load
await page.waitForSelector('.dashboard-loaded');
// Component screenshot
const chart = page.locator('.revenue-chart');
await expect(chart).toHaveScreenshot('revenue-chart.png');
});
CI Integration (GitHub Actions)
# .github/workflows/visual.yml
name: Visual Tests
on: [push, pull_request]
jobs:
visual:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for Chromatic
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps
- run: npx chromatic --playwright
env:
CHROMATIC_PROJECT_TOKEN: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
Chromatic Features (Free Tier)
| Feature | Free Tier |
|---|---|
| Snapshots | 5000/month |
| Parallelization | Unlimited |
| Browsers | Chrome, Firefox, Safari |
| Review UI | Yes |
| GitHub Integration | Yes |
Lost Pixel (Self-Hosted)
For confidential projects that can't use cloud services.
Installation
npm install --save-dev lost-pixel
Configuration
// lostpixel.config.ts
import { CustomProjectConfig } from 'lost-pixel';
export const config: CustomProjectConfig = {
pageShots: {
pages: [
{ path: '/', name: 'home' },
{ path: '/login', name: 'login' },
{ path: '/dashboard', name: 'dashboard' },
],
baseUrl: 'http://localhost:3000',
},
// Store baselines locally
imagePathBaseline: '.lostpixel/baseline',
imagePathCurrent: '.lostpixel/current',
imagePathDifference: '.lostpixel/difference',
// Fail on difference
generateOnly: false,
failOnDifference: true,
// Comparison settings
threshold: 0.1, // 0.1% pixel difference allowed
};
Run Lost Pixel
# Generate baselines (first run)
npx lost-pixel update
# Run comparison
npx lost-pixel
# View differences in .lostpixel/difference/
Docker Setup (for CI)
# Dockerfile.lostpixel
FROM mcr.microsoft.com/playwright:v1.40.0-focal
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["npx", "lost-pixel"]
# .github/workflows/visual-selfhosted.yml
jobs:
visual:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker build -f Dockerfile.lostpixel -t visual-tests .
- run: docker run visual-tests
Playwright Built-in Snapshots
Simplest approach, no external services.
Basic Usage
// tests/visual/basic.spec.ts
import { test, expect } from '@playwright/test';
test('visual regression', async ({ page }) => {
await page.goto('/');
// Full page
await expect(page).toHaveScreenshot('homepage.png');
// With options
await expect(page).toHaveScreenshot('homepage-full.png', {
fullPage: true,
maxDiffPixels: 100, // Allow 100 pixel difference
});
// Specific element
const header = page.locator('header');
await expect(header).toHaveScreenshot('header.png');
});
Update Baselines
# Update all baselines
npx playwright test --update-snapshots
# Update specific test
npx playwright test tests/visual/homepage.spec.ts --update-snapshots
Configuration
// playwright.config.ts
export default defineConfig({
expect: {
toHaveScr
...
Repository
adaptationio/skrillzParent repository
Repository Stats
Stars1
Forks0