Skip to content

Instantly share code, notes, and snippets.

@ModulesUnraveled
Last active July 19, 2018 20:33
Show Gist options
  • Save ModulesUnraveled/cecdaa8051c56fe01ba0b09096aada7e to your computer and use it in GitHub Desktop.
Save ModulesUnraveled/cecdaa8051c56fe01ba0b09096aada7e to your computer and use it in GitHub Desktop.

Intro

In this post, I'm going to run through how I setup visual regression testing on sites. Visual regression testing is essentially the act of taking a screenshot of a web page (whether the whole thing, or just a specific element) and comparing that against an existing screenshot of the same thing to see if there are any differences.

There's nothing worse than adding a new component, tweaking styles, or pushing a config update, only to have the client tell you two months later that some random other part of the site is now broken, and you discover it's because of the change that you pushed... now it's been two months, and reverting that change has significant implications.

That's the worst. Literally the worst.

All kinds of testing can help improve the stability and integrity of a site. There's Functional, Unit, Integration, Stress, Performance, Usability, and Regression, just to name a few. What's most important to you will change depending on the project requirements, but in my experience, Functional and Regression are the most common, and in my opinion are a good baseline if you don't have the capacity to write all the tests.

If you're reading this, you probably fall into one of two categories:

  1. You're already familiar with Visual Regression testing, and just want to know how to do it
  2. You're just trying to get info on why Visual Regression testing is important, and how it can help your project.

In either case, it makes the most sense to dive right in, so let's do it.

Tools

I'm going to be using WebdriverIO to do the heavy lifting. According to the website:

WebdriverIO is an open source testing utility for nodejs. It makes it possible to write super easy selenium tests with Javascript in your favorite BDD or TDD test framework.

It basically sends requests to a Selenium server via the WebDriver Protocol and handles its response. These requests are wrapped in useful commands and can be used to test several aspects of your site in an automated way.

I'm also going to run my tests on Browserstack so that I can test IE/Edge without having to install a VM or anything like that on my mac.

Process

Let's get everything setup. I'm going to start with a Drupal 8 site that I have running locally. I've already installed that, and a custom theme with Pattern Lab integration based on Emulsify.

We're going to install the visual regression tools with npm.

If you already have a project running that uses npm, you can skip this step. But, since this is a brand new project, I don't have anything using npm, so I'll create an initial package.json file using npm init.

  • npm init -y
    • Update the name, description, etc. and remove anything you don't need.
    • My updated file looks like this:
{
  "name": "visreg",
  "version": "1.0.0",
  "description": "Website with visual regression testing",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  }
}

Now, we'll install the npm packages we'll use for visual regression testing.

  • npm install --save-dev webdriverio chai wdio-mocha-framework wdio-browserstack-service wdio-visual-regression-service node-notifier
    • This will install:
      • WebdriverIO: The main tool we'll use
      • Chai syntax support: "Chai is an assertion library, similar to Node's built-in assert. It makes testing much easier by giving you lots of assertions you can run against your code."
      • Mocha syntax support "Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun."
      • The Browserstack wdio package So that we can run our tests against Browserstack, instead of locally (where browser/OS differences across developers can cause false-negative failures)
      • Visual regression service This is what provides the screenshot capturing and comparison functionality
      • Node notifier This is totally optional, but supports native notifications for Mac, Linux, and Windows. We'll use these to be notified when a test fails.

Now that all of the tools are in place, we need to configure our visual regression preferences.

You can run the configuration wizard by typing ./node_modules/webdriverio/bin/wdio, but I've created a git repository with not only the webdriver config file, but an entire set of files that scaffold a complete project. You can get them here.

Follow the instructions in the README of that repo to install them in your project.

These files will get you setup with a fairly sophisticated, but completely manageable visual regression testing configuration. There are some tweaks you'll need to make to fit your project that are outlined in the README and the individual markdown files, but I'll run through what each of the files does at a high level to acquaint you with each.

  • .gitignore
    • The lines in this file should be added to your existing .gitignore file. It'll make sure your diffs and latest images are not committed to the repo, but allow your baselines to be committed so that everyone is comparing against the same baseline images.
  • VISREG-README.md
    • This is an example readme you can include to instruct other/future developers on how to run visual regression tests once you have it setup
  • package.json
    • This just has the example test scripts. One for running the full suite of tests, and one for running a quick test, handy for active development. Add these to your existing package.json
  • wdio.conf.js
    • This is the main configuration file for WebdriverIO and your visual regression tests.
    • You must update this file based on the documentation in wdio.conf.md
  • wdio.conf.quick.js
    • This is a file you can use to run a quick test (e.g. against a single browser instead of the full suite defined in the main config file). It's useful when you're doing something like refactoring an existing component, and/or want to make sure changes in one place don't affect other sections of the site.
  • tests/config/globalHides.js
    • This file defines elements that should be hidden in ALL screenshots by default. Individual tests can use this, or define their own set of elements to hide. Update these to fit your actual needs.
  • tests/config/viewports.js
    • This file defines what viewports your tests should run against by default. Individual tests can use these, or define their own set of viewports to test against. Update these to the screen sizes you want to check.

Running the Test Suite

I'll copy the example homepage test from the example-tests.md file into a new file /web/themes/custom/visual_regression_testing/components/_patterns/05-pages/home/home.test.js. (I'm putting it here because my wdio.conf.js file is looking for test files in the _patterns directory, and I like to keep test files next to the file they're testing.)

The only thing you'll need to update in this file is the relative path to the globalHides.js file. It should be relative from the current file. So, mine will be:

const visreg = require('../../../../../../../../tests/config/globalHides.js');

With that done, I can simply run npm test and the tests will run on BrowserStack against the three OS/Browser configurations I've specified. While they're running, we can head over to https://automate.browserstack.com/ we can see the tests being run against Chrome, Firefox, and IE 11.

Once tests are complete, we can view the screenshots in the /tests/screenshots directory. Right now, the baseline shots and the latest shots will be identical because we've only run the test once, and the first time you run a test, it creates the baseline from whatever it sees. Future tests will compare the most recent "latest" shot to the existing baseline, and will only update/create images in the latest directory.

At this point, I'll commit the baselines to the git repo so that they can be shared around the team, and used as baselines by everyone running visual regression tests.

If I run npm test again, the tests will all pass because I haven't changed anything. I'll make a small change to the button background color which might not be picked up by a human eye, but will cause a regression that our tests will pick up with no problem.

in the _buttons.scss file, I'm going to change the default button background color from $black (#000) to $gray-darker (#333). I'll run the style script to update the compiled css and then clear the site cache to make sure the change is implemented. (When actively developing, I suggest disabling cache and keeping the watch task running. It just makes things easier and more efficient.)

This time all the tests fail, and if we look at the images in the diff folder, we can clearly see that the "search" button is different as indicated by the bright pink/purple coloring.

If I open up one of the "baseline" images, and the associated "latest" image, I can view them side-by-side, or toggle back and forth. The change is so subtle that a human eye might not have noticed the difference, but the computer easily identifies a regression. This shows how useful visual regression testing can be!

Let's pretend this is actually a desired change. The original component was created before the color was finalized, black was used as a temporary color, and now we want to capture the update as the official baseline. Simply Move the "latest" image into the "baselines" folder, replacing the old baseline, and commit that to your repo. Easy peasy.

Running an Individual Test

If you're creating a new component and just want to run a single test instead of the entire suite, or you run a test and find a regression in one image, it is useful to be able to just run a single test instead of the entire suite. This is especially true once you have a large suite of test files that cover dozens of aspects of your site. Let's take a look at how this is done.

I'll create a new test in the organisms folder of my theme at /search/search.test.js. There's an example of an element test in the example-tests.md file, but I'm going to do a much more basic test, so I'll actually start out by copying the homepage test, and then modify that.

The first thing I'll change is the describe section. This is used to group and name the screenshots, so I'll update it to make sense for this test. I'll just replace "Home Page" with "Search Block".

Then, the only other thing I'm going to change is what is to be captured. I don't want the entire page, in this case. I just want the search block. So, I'll update checkDocument (used for full-page screenshots) to checkElement (used for single element shots). Then, I need to tell it what element to capture. This can be any css selector, like an id or a class. I'll just inspect the element I want to capture, and I know that this is the only element with the search-block-form class, so I'll just use that.

I'll also remove the timeout, since we're just taking a screenshot of a single element, we don't need to worry about the page taking longer to load than the default of 60 seconds. This really wasn't necessary on the page either, but whatever.

My final test file looks like this:

const visreg = require('../../../../../../../../tests/config/globalHides.js');

describe('Search Block', function () {
  it('should look good', function () {
    browser
      .url('./')
      .checkElement('.search-block-form', {hide: visreg.hide, remove: visreg.remove})
      .forEach((item) => {
        expect(item.isWithinMisMatchTolerance).to.be.true;
      });
  });
});

With that in place, this test will run when I use npm test because it's globbing, and running every file that ends in .test.js anywhere in the _patterns directory. The problem is this also runs the homepage test. If I just want to update the baselines of a single test, or I'm actively developing a component and don't want to run the entire suite every time I make a locally scoped change, I want to be able to just run the relevant test so that I don't waste time waiting for all of the irrelevant tests to pass.

We can do that by passing the --spec flag.

I'll commit the new test file and baselines before I continue.

Now I'll re-run just the search test, without the homepage test.

npm test -- --spec web/themes/custom/visual_regression_testing/components/_patterns/03-organisms/search/search.test.js

We have to add the first set of -- because we're using custom npm scripts to make this work. Basically, it passes anything that follows directly to the custom script (in our case test is a custom script that calls ./node_modules/webdriverio/bin/wdio). More info on the run-script documentation page.

If I scroll up a bit, you'll see that when I ran npm test there were six passing tests. That is one test for each browser for each test. We have two test, and we're checking against three browsers, so that's a total of six tests that were run.

This time, we have three passing tests because we're only running one test against three browsers. That cut our test run time by more than half (from 106 seconds to 46 seconds). If you're actively developing or refactoring something that already has test coverage, even that can seem like an eternity if you're running it every few minutes. So let's take this one step further and run a single test against a single browser. That's where the wdio.conf.quick.js file comes into play.

Running Test Against a Subset of Browsers

The wdio.conf.quick.js file will, by default, run test(s) against only Chrome. You can of course change this to whatever you want (for example if you're only having an issue in a specific version of IE, you could set that here), but I'm just going to leave it alone and show you how to use it.

You can use this to run the entire suite of tests, or just a single test. First, I'll show you how to run the entire suite against only the browser defined here, then I'll show you how to run a single test against this browser.

In the package.json file, you'll see the test:quick script. You could pass the config file directly to the first script by typing npm test -- wdio.conf.quick.js, but that's a lot more typing than npm run test:quick and you (as well as the rest of your team) have to remember the file name. Capturing the file name in a second custom script simplifies things.

When I run npm run test:quick You'll see that two tests were run. We have two tests, and they're run against one browser, so that simplifies things quite a bit. And you can see it ran in only 31 seconds. That's definitely better than the 100 seconds the full test suite takes.

Let's go ahead and combine this with the technique for running a single test to cut that time down even further.

npm run test:quick -- --spec web/themes/custom/visual_regression_testing/components/_patterns/03-organisms/search/search.test.js

This time you'll see that it only ran one test against one browser and took 28 seconds. There's actually not a huge difference between this and the last run because we can run three tests in parallel. And since we only have two tests, we're not hitting the queue which would add significantly to the entire test suite run time. If we had two dozen tests, and each ran against three browsers, that's a lot of queue time, whereas even running the entire suite against one browser would be a significant savings. And obviously one test against one browser will be faster than the full suite of tests and browsers.

So this is super useful for active development of a specific component or element that has issues in one browser as well as when you're refactoring code to make it more performant, and want to make sure your changes don't break anything significant (or if they do, alert you sooner than later). Once you're done with your work, I'd still recommend running the full suite to make sure your changes didn't inadvertently affect another random part of the site.

So, those are the basics of how to setup and run visual regression tests. In the next post I'll dive into our philosophy of what we test, when we test, and how it fits into our everyday development workflow.

@evanmwillhite
Copy link

This is fantastic! I only saw a couple of things. The first is in that in this section the second line suggests there is something we need to do in that file. Do we or should it say something like "this is where any changes can be made to blah blah.."

wdio.conf.js
- This is the main configuration file for WebdriverIO and your visual regression tests.
- You must update this file based on the documentation in wdio.conf.md

And the last is that it felt like a transition heading ("Concluding Thoughts" or something akin) might help with the transition before So this is super useful for active development of a specific component...

That's it! Screenshots/gifs/videos would definitely be useful in this situation. Excellent work on this.

@ModulesUnraveled
Copy link
Author

You do have to make changes. They're outlined in the repo that I linked to: https://github.com/ModulesUnraveled/visreg-config-files/blob/master/wdio.conf.md

There was just a tiny link to that supplemental repo. I should probably make sure that's a big hairy "DOWNLOAD THIS STUFF HERE" link that's more obvious

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment