Visual testing (sometimes called visual regression testing or UI testing) is the process of reviewing software for purely visual integrity. Unlike the tools you use to make sure your app is behaving as intended, visual testing is all about what your users actually see and interact with.
It works by comparing snapshots of your UI against baselines to see if pixels have changed. By looking at the pixels rather than the code underneath, visual testing makes it easy to see exactly what your UI looks like before deploying and to catch visual regressions.
Visual testing is more than snapshot testing—having a visual review workflow to get continuous visual coverage is crucial. With Percy, it’s easy to integrate visual testing into your stack to in tandem with your CI/CD pipeline on every pull request.
Now that you have an overview of how visual testing works, we’re going to walk through a 5-minute tutorial.
This is what we’ll cover:
Let’s get started!
If you haven’t already, sign up for a free Percy account, name your organization, and create your first project.
Signing up for a Percy account is free and includes 5,000 snapshots each month with upgrades starting at $29 available.
Percy projects correspond with the apps, sites, or component libraries you want to test. With our SDKs you can add visual testing to virtually anything that renders in a browser.
These are some of our popular SDKs:
For this tutorial, we’ll use PercyScript—the easiest and fastest way to get started with visual testing for any web app. We’re going to use this TodoMVC example app, although you can easily adapt the PercyScript below to work for your own application.
First, let’s setup the example app:
$ git clone https://github.com/percy/example-todomvc.git
$ cd example-todomvc/
$ npm install
$ npm run start
You can now visit http://localhost:8000 and play around with the todos app yourself.
Next, we’re going to install PercyScript and write our first visual tests for this application.
It’s important to keep the server running and open a new terminal to run:
$ npm install -D @percy/script
This will add @percy/script
to your package.json
file.
Next, create a file named snapshots.js
and add your first PercyScript:
// snapshots.js
const PercyScript = require('@percy/script');
// A script to navigate our app and take snapshots with Percy.
PercyScript.run(async (page, percySnapshot) => {
await page.goto('http://localhost:8000');
await percySnapshot('TodoMVC home page');
// Enter a new to-do.
await page.type('.new-todo', 'A really important todo');
await page.keyboard.press('Enter');
await percySnapshot('TodoMVC with a new todo', { widths: [768, 992, 1200] });
});
That’s it! The next step is to start running this PercyScript.
To run your PercyScript locally, copy the PERCY_TOKEN
environment variable from the new project screen or your project settings, then run:
$ export PERCY_TOKEN=aaabbbcccdddeee
$ npx percy exec -- node snapshots.js
Replace the token with your project’s `PERCY_TOKEN`.
You should see output like:
$ npx percy exec -- node snapshots.js
[percy] created build #1: https://percy.io/test/example-todomvc/builds/1738842
[percy] percy has started.
[percy] snapshot taken: 'TodoMVC home page'
[percy] snapshot taken: 'TodoMVC with a new todo'
[percy] stopping percy...
[percy] waiting for 2 snapshots to complete...
[percy] done.
[percy] finalized build #1: https://percy.io/test/example-todomvc/builds/1738842
What’s happening behind the scenes? Percy works by capturing the DOM snapshot everywhere the Percy snapshot command is called. We then recreate the page or component in our custom rendering environment. New snapshots are compared against baseline snapshots to determine which pixels have changed.
If you’re following along, you’ll see that since this is the first build, there isn’t anything to compare it to. It has also been “auto-approved” because the commit was on master and we assume that master builds are production-ready.
Now that you’re integrated and have pushed your first build establishing your baseline, let’s make a new feature branch and introduce a visual change to review in Percy.
Use your text editor to edit index.html
and make the h1 text on line 11 purple:
<h1 style=”color:#9e66bf;”>
Now run the snapshots again:
$ npx percy exec -- node snapshots.js
Head back to Percy or click the Percy build link to see the visual changes! On the right, you’ll see the new snapshot, overlaid with red pixels highlighting the areas that have changed.
Clicking that area (or pressing “d”) will toggle between the underlying snapshot and the overlaid diff so you can easily see what exactly has changed.
You can also use Percy to get coverage across all the most important viewport sizes. By default, we render snapshots for mobile and desktop widths. Toggle between widths to see what has changed across each.
Cross-browser snapshots help you detect subtle differences caused by browser rendering.
If you’re happy with your changes, hit “Approve All” — knowing with 100% confidence what visual changes you’ll be deploying. By default, Percy renders all snapshots across both Chrome and Firefox.
Since you wanted to make these changes, hit “Approve All.”
You’ve done your first visual review!
To get the most value out of automated visual testing, we recommend integrating with your existing dev tools and processes.
Percy is designed to run alongside your CI builds as part of your code review process. For more in-depth instructions and to see all of our supported CI services, check out our CI setup documentation. Here are a few of our most popular supported services:
With a source code integration enabled, you will be notified right in your pull or merge request when visual changes are detected, and when those changes are approved and ready to merge.
Clicking “Details” will take you right to the Percy build where you can review visual changes.
After snapshots are approved, your commit status will change to green and the PR can be merged.
That’s it! You’re ready to merge with confidence that every part of your app looks exactly like it should.
We hope this tutorial has helped you get acquainted with Percy’s visual review platform and workflow. To continue learning about how Percy works, feel free to check out these additional resources:
And if you haven’t already, sign up for a free Percy account. You get 5,000 free monthly snapshots and access to our visual testing platform and integrations.
✨ Happy testing!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!