TechBuzzPro

Percy with Cypress: Visual Regression Testing Guide

· 4 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

Percy integrates with Cypress to add automated visual regression testing to your existing end-to-end tests. It captures screenshots of your app at key points, compares them against approved baselines, and flags any visual changes for review. This guide walks you through setting up Percy with Cypress, writing your first visual test, and integrating it into your workflow.

What You'll Need

  • A project with Cypress already installed and configured (Cypress 6.0 or later recommended).
  • A Percy account (free tier available) and a project created in the Percy dashboard.
  • Node.js and npm (or yarn) installed on your machine.
  • Basic familiarity with Cypress test writing.

Step-by-Step

  1. Install the Percy Cypress package. In your project root, run: npm install --save-dev @percy/cli @percy/cypress (or use yarn). This installs the Percy CLI and the Cypress-specific commands.
  2. Set up the Percy script. In your package.json, add a script to run Cypress with Percy: "percy:cypress": "percy exec -- cypress run". This wraps the Cypress run so that Percy captures and uploads snapshots.
  3. Import Percy in Cypress support file. In cypress/support/index.js (or e2e.js for Cypress 10+), add: import '@percy/cypress';. This registers the cy.percySnapshot() command.
  4. Add Percy snapshots to your tests. In any Cypress test, call cy.percySnapshot('Name of snapshot') at the point where you want to capture the screen. For example, after navigating to a page or after a user interaction. You can add multiple snapshots per test.
  5. Set your Percy token. In the Percy dashboard, find your project's write access token. Set it as an environment variable: export PERCY_TOKEN=your_token_here (or add it to your CI/CD environment). Do not commit this token to version control.
  6. Run the tests locally. Execute npm run percy:cypress. This will run your Cypress tests, capture screenshots at each snapshot, and upload them to Percy. The first run creates baselines.
  7. Review snapshots in Percy. After the run, go to your Percy project dashboard. You'll see the snapshots grouped by commit. Approve the initial set if they look correct—these become your baselines.
  8. Integrate with CI/CD. In your CI pipeline (e.g., GitHub Actions, CircleCI), install dependencies, set the PERCY_TOKEN environment variable, and run npm run percy:cypress. Percy will post a status check with a visual diff summary.
  9. Handle changes. When a snapshot differs from the baseline, Percy shows the diff. Review it, and either approve the change (if intentional) or fix the code and re-run.

Common Mistakes

  • Forgetting to import Percy in the support file. Without the import, cy.percySnapshot() is undefined and tests fail.
  • Not setting PERCY_TOKEN. Without the token, Percy CLI won't upload snapshots; you'll see an error in the terminal.
  • Using a non-deterministic test environment. If your app renders dynamic data (e.g., current time, random IDs), snapshots will diff every run. Mock or freeze such data.
  • Adding too many snapshots. Each snapshot increases execution time and storage. Be selective—capture key pages and states, not every scroll position.
  • Ignoring the baseline approval. If you don't approve the first run, Percy may treat all subsequent runs as 'new' and not compare properly.

Tips

  • Use cy.percySnapshot() after the page has fully loaded and animations have settled. You can use cy.wait() or wait for specific elements to be visible.
  • Set a fixed viewport size for consistent screenshots: cy.viewport(1280, 720) before the snapshot.
  • Name snapshots meaningfully, e.g., 'Home page desktop', 'Login form error state'. This makes diffs easier to identify.
  • Use Percy's 'ignore regions' feature in the dashboard to mask dynamic areas like dates or third-party widgets.
  • Run Percy locally before pushing to CI to catch obvious issues early.

Troubleshooting

  • Error: 'cy.percySnapshot is not a function'. Ensure you've imported @percy/cypress in the support file and that the package is installed.
  • No snapshots appear in Percy dashboard. Check that you're running via percy exec -- cypress run (not just cypress run). Also verify the token is set and the CLI is installed.
  • Snapshots are blank or missing content. This often happens when the page hasn't finished loading. Add a wait for a key element before the snapshot.
  • Diff shows changes that aren't real. Likely due to dynamic content or animations. Use Percy's 'ignore regions' or freeze dynamic data in tests.
  • Build fails in CI with 'percy: not found'. Make sure @percy/cli is installed as a dev dependency and the script is run from the project root.

Frequently Asked Questions

Do I need a Percy account to use it with Cypress?

Yes, you need a Percy account and a project to upload snapshots to. The free tier offers a limited number of screenshots per month.

Can I use Percy with Cypress on localhost?

Yes, Percy works with localhost. The Percy CLI starts a local server to capture snapshots, and it works with Cypress's default baseUrl.

How does Percy handle responsive design?

Percy can capture snapshots at multiple viewport sizes. You can use cy.viewport() before each snapshot to capture desktop, tablet, and mobile layouts.

Can I ignore specific elements in the snapshot?

Yes, in the Percy dashboard you can define ignore regions by selecting areas on the snapshot. This is useful for dynamic content like ads or live data.

What happens when I approve a snapshot?

Approving a snapshot sets it as the new baseline. Future runs will compare against this approved version.

Does Percy work with Cypress Component Testing?

Yes, Percy supports Cypress Component Testing. You need to add the same cy.percySnapshot() calls in your component tests and run with percy exec -- cypress run-ct.