Set up visual comparison testing with Playwright
✓Works with OpenClaudeYou are a QA automation engineer. The user wants to set up visual comparison testing with Playwright to detect unintended UI changes through screenshot comparisons.
What to check first
- Run
npm list playwrightto confirm Playwright is installed (v1.40+ recommended for stable visual comparisons) - Check if you have a
playwright.config.tsfile in your project root - Verify you have a baseline screenshots directory ready (or plan to create one with
npx playwright test --update-snapshots)
Steps
- Install Playwright with
npm install -D @playwright/testif not already present - Create a test file (e.g.,
tests/visual.spec.ts) that importstestandexpectfrom@playwright/test - Configure
webServerUrlorbaseURLinplaywright.config.tsto point to your app's test environment - Write a test that navigates to a page and calls
expect(page).toHaveScreenshot()with a descriptive snapshot name - Run
npx playwright test --update-snapshotsonce to generate baseline images intests/__screenshots__/directory - Add snapshot comparison assertions for different viewport sizes using
page.setViewportSize()before screenshots - Run
npx playwright testin CI/CD; Playwright will compare current screenshots against baselines and fail if pixel differences exceed the threshold - Review failed visual tests with
npx playwright show-reportto see side-by-side diffs of expected vs actual
Code
import { test, expect } from '@playwright/test';
test.describe('Visual Testing', () => {
test.beforeEach(async ({ page }) => {
await page.goto('https://example.com');
});
test('homepage full page snapshot', async ({ page }) => {
// Wait for critical elements to load
await page.waitForLoadState('networkidle');
// Take full page screenshot
await expect(page).toHaveScreenshot('homepage-full.png', {
fullPage: true,
maxDiffPixelRatio: 0.1, // Allow 10% pixel difference
});
});
test('hero section snapshot', async ({ page }) => {
// Isolate and compare specific components
const heroSection = page.locator('[data-testid="hero"]');
await expect(heroSection).toHaveScreenshot('hero-section.png', {
maxDiffPixels: 50, // Allow max 50 different pixels
});
});
test('responsive design - mobile', async ({ page }) => {
// Test at mobile viewport
await page.setViewportSize({ width: 375, height: 667 });
await page.waitForLoadState('networkidle');
await expect(page).toHaveScreenshot('homepage-mobile.png', {
fullPage: true,
});
});
test('responsive design - tablet
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Testing Skills
Other Claude Code skills in the same category — free to download.
Unit Test Generator
Generate unit tests for any function or class
Test Coverage Analyzer
Analyze test coverage gaps and suggest tests to write
Mock Generator
Generate mocks, stubs, and fakes for dependencies
Snapshot Test Creator
Create snapshot tests for UI components
E2E Test Writer
Write end-to-end tests using Playwright or Cypress
Test Data Factory
Create test data factories and fixtures
API Test Suite
Generate API test suites for REST endpoints
Mutation Testing Setup
Set up mutation testing to verify test quality
Want a Testing skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.