We're live-coding on YouTube! Join us!

Getting started with visual testing in 5 minutes

Getting started with visual testing in 5 minutes

There are tons of tools out there that help you make sure your app is functioning perfectly, but how do you catch bugs that are purely visual?

In the example above, the button still works, and the text is actually still there, but the link somehow changed to be the same color as the button.

You could write regression tests to ensure that the text is always white, and the button is always green, but across hundreds of pages and states, that becomes unruly very fast. So in lieu of (or in addition to) writing line after line of regression tests, you might manually check all those pages before deploying. But we all know how time-consuming and imperfect manual QA can be.

That’s where visual testing comes in.

*Visual testing is the new way to have 100% confidence in your UI. *Instead of manually checking your UI or testing the code underneath, visual testing detects pixel-by-pixel changes on every commit automatically. Visual testing saves you time reviewing code and ensuring no visual bugs make their way to production.

In this 5-minute tutorial, we’ll walk through integrating Percy with an example app, reviewing visual changes, and running visual reviews as part of your day-to-day workflow.

Step 1: Integrate Percy

If you haven’t already, sign up for a Percy account, name your organization, and create your first project.

Note: Signing up for a Percy account will kick off a 14-day trial, but once that expires, you’ll be transferred to a free plan that includes 5,000 free snapshots each month.

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:

Our SDKs work by importing and calling Percy snapshot commands wherever you’d like visual coverage.

*For this tutorial, we’ll use PercyScript, which provides a quick and easy way to start doing visual testing in just a couple of lines of JavaScript. *PercyScript can fully interact with elements by clicking, typing, and waiting and can also be used to test live URLs. Read more in our PercyScript documentation.

For this tutorial, we’re going to use this TodoMVC example app. 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.

Keep the server running, open a new terminal, and 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] });
});

The next step is to start running this PercyScript and seeing visual changes.

Step 2: Run visual tests

To run your PercyScript locally, copy PERCY_TOKEN from the new project screen, then run:

$ export PERCY_TOKEN=aaabbbcccdddeee
$ npx percy exec -- node snapshots.js

Note: Be sure to replace the token with your project-specific 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

The PercyScript has run, sending snapshots up to Percy for rendering and processing:

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.

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.

Now that you’re integrated and have pushed your first build establishing your baseline, let’s make a change and review the outcome in Percy!

Step 3: Review visual changes

Let’s make a new feature branch and introduce a visual change.

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

Open up the Percy link, and we can now see the visual change highlighted!

Side-by-side comparison

The red areas in the right panel represent pixels that have changed—those changed pixels are called visual diffs.

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.

Pro tip: Click the arrow expansion box for a full-screen view. Cycle through old, new, and diff with your left and right keys, and navigate between snapshots with up and down down keys.

Responsive diffs

You’ll notice the widths at which your snapshots have been rendered show up here. In your example we rendered snapshots for mobile and desktop widths. Select the various widths to see what has changed across each width.

Cross-browser visual testing

By default, Percy renders all snapshots across both Chrome and Firefox. 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.

You’ve done your first visual review.

Step 4: CI and source code integrations

To get the most value out of automated visual testing, we recommend integrating visual reviews into your workflow.

Configure Percy with your CI service so that visual tests runs every time a CI build is kicked off. Simply add PERCY_TOKEN to your CI environment variables. 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:

You can also easily add Percy to your pull/merge requests to be notified when visual changes are detected, and when those changes are approved and ready to merge.

Head to your organization settings to give Percy access to GitHub or GitLab. Once you’ve given access, be sure to connect your project to your project's repository. Then the next time you commit, Percy will show up in your pull/merge request checks:

Clicking “Details” will take you right to the Percy UI 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:

This content is sponsored via Syndicate Ads.

Happy testing! 💜