TechBuzzPro

Visual Regression Testing on Netlify Deploy Previews

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Visual regression testing on Netlify deploy previews catches unintended UI changes before they reach production. By comparing screenshots of your site’s pages across deploys, you can spot broken layouts, shifted elements, or color changes automatically. This guide walks you through setting up a simple, reliable workflow using Playwright and GitHub Actions, with Netlify’s deploy preview URLs as the test target. You’ll need a Netlify site connected to a Git repository and basic familiarity with Node.js.

What You'll Need

  • A Netlify site linked to a GitHub, GitLab, or Bitbucket repository.
  • Node.js (version 18 or later) installed locally.
  • A package manager like npm or yarn.
  • Basic knowledge of the command line and Git.

Step-by-Step

  1. Install Playwright and its browser binaries. In your project root, run npm init -y if you don’t have a package.json, then npm install -D @playwright/test and npx playwright install chromium. This downloads the browser needed for screenshots.
  2. Create a Playwright config file. Make a playwright.config.js in the root. Set use: { baseURL: process.env.DEPLOY_URL || 'http://localhost:3000' } so tests run against the preview URL when available, and fall back to localhost for local runs.
  3. Write a visual test script. Create a tests/visual.spec.js file. Use test.describe to group checks. For each page you want to test, navigate to the path and call expect(page).toHaveScreenshot(). For example:
    const { test, expect } = require('@playwright/test');
    
    test('homepage matches baseline', async ({ page }) => {
      await page.goto('/');
      await expect(page).toHaveScreenshot('home.png');
    });
    Add more tests for other routes like /about or /pricing.
  4. Generate baseline screenshots. Run npx playwright test --update-snapshots locally. This creates a __screenshots__ folder with the reference images. Commit these to your repository.
  5. Set up a GitHub Actions workflow. Create .github/workflows/visual-tests.yml . Use a trigger that runs on pull requests. The workflow should check out the code, install dependencies, run npx playwright install --with-deps chromium , and then execute npx playwright test . Pass the deploy preview URL as an environment variable: .
  6. Link Netlify deploy previews to GitHub checks. In Netlify, go to Site settings → Build & deploy → Deploy notifications. Add a notification for “Deploy succeeded” that sends to GitHub Commit Statuses. This ensures the preview URL is available to the workflow.
  7. Run the workflow on pull requests. When you open a PR, Netlify builds a preview. Once the deploy succeeds, the GitHub Action triggers, runs the tests against that URL, and reports pass/fail as a check. If a screenshot differs, the action fails and you can download the diff from the workflow artifacts.
  8. Review and update baselines when changes are intentional. If a UI change is deliberate, run npx playwright test --update-snapshots locally, commit the new baselines, and push. The PR will then pass.

Common Mistakes

  • Forgetting to install browsers in CI. The workflow must run npx playwright install --with-deps chromium; otherwise, tests fail with “browser not found”.
  • Hardcoding the localhost URL. If you use http://localhost:3000 in the config, the CI will try to connect to itself. Always use the DEPLOY_URL environment variable.
  • Ignoring dynamic content. Timestamps, random ads, or animated elements cause false positives. Use page.clock or mask selectors with mask option in toHaveScreenshot.
  • Not committing baseline images. If you don’t commit the __screenshots__ folder, CI has no reference and fails.
  • Running tests on every push instead of PRs. This wastes resources and can fail on branch builds without preview URLs.

Tips

  • Start with a small set of critical pages—home, product, checkout—and add more as you trust the setup.
  • Use --max-failures=0 in CI to ensure all tests run and you see the full list of diffs.
  • Set a reasonable timeout (e.g., 30 seconds) in the config to avoid long hangs on slow previews.
  • Use Playwright’s mask option to ignore regions that change frequently, like carousels or live counters.
  • Store baselines in a separate branch if you have frequent intentional changes, and merge them periodically.
  • Consider using Netlify’s built-in “Deploy Preview” comments to see the URL directly in the PR conversation.

Troubleshooting

Tests pass locally but fail in CI. This usually means the CI is using a different URL. Check the workflow logs to see the DEPLOY_URL value. Ensure the Netlify notification is set to GitHub Commit Statuses, not just email or Slack.

“Cannot find module ‘@playwright/test’.” Run npm install in the workflow before running tests. If you’re using a lockfile, use npm ci for faster, reproducible installs.

Screenshots are all red or blank. The preview might still be building when the test runs. Add a delay or use Netlify’s “Deploy succeeded” notification, which fires only after the build is complete. Also verify the URL is publicly accessible; if the preview requires authentication, Playwright won’t see the content.

Diff is huge even for a small change. This often happens due to font loading or scrollbar differences. Set a fixed viewport in the config (viewport: { width: 1280, height: 720 }) and disable animations via CSS injection.

Workflow doesn’t trigger. Check that the workflow file is in the default branch and that the trigger is pull_request (not push). Also confirm that GitHub Actions is enabled for the repository.

Frequently Asked Questions

Q: Can I use Cypress instead of Playwright? A: Yes, Cypress has visual testing plugins like cypress-image-snapshot, but Playwright has built-in screenshot comparison and is often simpler for this purpose.

Q: How do I test pages that require login? A: In the test, use Playwright’s context.addCookies() or perform a login flow before navigating to the protected page. Store session state in a global setup file.

Q: What if my site uses a lot of dynamic content? A: Mask those elements with the mask option, or use page.clock to freeze time. Alternatively, test only static pages.

Q: Does this work with Netlify’s branch subdomains? A: Yes, the DEPLOY_URL environment variable contains the full URL of the deploy preview, whether it’s a branch subdomain or a random one.

Q: How often should I update baselines? A: Whenever a visual change is intentional and approved. Avoid updating baselines on every PR; instead, review the diff first.

Q: Can I run tests locally against a Netlify preview? A: Yes, set the DEPLOY_URL environment variable to the preview URL when running npx playwright test locally.