Visual Regression Testing and Webdriverio Guide
Visual Regression Testing and Webdriverio Guide
and WebdriverIO
Kevin Lamping and Micah Godbolt
This book is for sale at http://leanpub.com/visual-regression-testing-and-webdriverio-guide
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
ChromeDriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Let’s make a test! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Some Terminology
Let’s kick it off by expanding our vocabulary. We’ll be using these terms throughout the course:
Regression Testing
Change hurts. Especially when it’s unwanted.
“Regressions” are changes, bad ones, to the functionality of your site. To say you’ve found a
“regression” means you found code that used to work but no longer does.
“Regression Testing” is a type of testing that checks for these bugs in existing functionality after an
update.
While you definitely want to put new code through the wringer, it’s also important to look at existing
features to ensure they weren’t adversely affected by the updates. That’s what regression testing
does.
Automated Testing
Automation comes in many forms, whether through robots or driverless cars. For us, it’s writing
code that “automates” actions on a website, similar to how AI is written to “automate” driving a car.
While the goal of website automation is to test without a human clicking links and entering text, in
no way can it take the place of hard-working people.
First of all, someone has to write the automation and know how to keep it up to date. That can easily
be a full-time job on a large enough application.
But there is also a lot of nuance to websites. Checking to see if an animation worked as designed is
very difficult with automated tests. Automation simply aims to handle the boring, repetitive tasks,
leaving you time to test the hard stuff.
What to know about Visual Regression Testing 3
Functional Testing
There are many types of software testing out there; Wikipedia has an extensive list of the various
definitions2 .
We’re focusing on “functional” testing, which checks the UI functionality of a website and ensures
that it works properly. This is not the same as testing JavaScript functions (that would be unit testing).
Functional testing also goes by “system testing”, “end-to-end tests” and many other monikers, but
for our course we’ll use the term “functional testing”.
Visual Testing
There’s one last term to cover, and that’s the concept of “visual testing.” In this course, we’ll talk
about both standard testing (e.g. make sure certain text is on the page) and visual testing (e.g. make
sure the page looks the same as before).
Visual Regression Testing has a unique set of benefits. Just because an element is on a page does not
mean it’s in the right location (or the right size or the right color).
With visual testing, we take screenshots of various parts of the page to serve as the “baseline” of
how the site should work. Then in subsequent test runs, we take new screenshots and compare to
those baselines. Any differences are flagged for review.
Some Disclaimers
We really believe in the benefits of automated testing, so we should probably throw out some
disclaimers before we get you too excited:
Basically, it’s not magic (although it feels like it at times). There are certain things you just won’t be
able to automate testing for, so it’s best not to fight that fact.
The Technology
We’ll go in to installation of the tools tomorrow, but here’s what we’ll be using:
• A working NodeJS environment7 (Learn about Node and NPM if you are new to it8 )
• A light understanding of the command line9
• An understanding of JavaScript fundamentals10
What’s Next?
Phew, you’ve made it through the entire first day without any code being shown. Don’t worry, we’ve
saved all that for tomorrow, where we’ll take a look at writing your very first automated test (we’re
so excited for you!)
4
https://en.wikipedia.org/wiki/Selenium_(software)
5
http://webdriver.io/
6
https://github.com/visualregressiontesting/webdrivercss
7
https://github.com/creationix/nvm#node-version-manager-
8
https://docs.npmjs.com/getting-started/what-is-npm
9
https://www.codecademy.com/learn/learn-the-command-line
10
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Basics
Today you’re going to write your first
regression test
Day 2 is here; we hope you’re ready to dig in. Let’s start with some installs!
First, create a folder to store all of your code. You can name it whatever you want.
Then, open a command line and navigate to that folder. From there, you’ll create a new package.json
file by running the command:
npm init -y
If you’re new to NodeJS or unfamiliar with NPM, have a look at their about page for a quick intro
video11 .
This file just stores information about your project and the software you need. It’s useful to have on
hand if you need to set the project up again later.
Now to run the actual installation:
WebdriverIO
WebdriverIO is a JavaScript functional test library. It lets you write test instructions in JavaScript,
then passes them along to Selenium, which tells the browser what actions to take.
For us, writing tests in JavaScript is awesome. We don’t have to learn Java for test automation!
WebdriverCSS
WebdriverCSS is a Visual Regression Testing plugin for WebdriverIO. It’s a pretty basic tool that
boils down to two basic functions:
You can do a fair amount of configuration with the tool, but for now, those two concepts are all you
need to know.
11
https://docs.npmjs.com/getting-started/what-is-npm
ChromeDriver
We’re testing in real browsers folks, which is really quite awesome. But it does require some set up.
While getting Selenium running is easier than ever, it’s too complex for a single email.
As a worthy alternative, we’re going to use ChromeDriver. ChromeDriver is a Selenium-like tool
that allows us to run tests on a real Chrome browser.
To get it going, just open a new command line window (making sure you’re in your project folder),
and run:
This will be a constantly-running service. You can stop the it by pressing Ctrl-C.
• If you’re interested in why we reference ./node_modules/bin/ and a way around that, read
up on npm scripts12 .
Disclaimer For the rest of this course we’re going to leave out repetitive bits of the code
in our examples. To see the full code, check out the code samples for each day13 .
12
http://firstdoit.com/npm-scripts/
13
http://learn.visualregressiontesting.com/code-samples.zip
ChromeDriver 7
What just happened here? Well, we told WebdriverIO to start a new Chrome browser. If you want
to read more about it, check out the documentation14 .
browser.url("http://learn.visualregressiontesting.com");
browser.url("http://learn.visualregressiontesting.com")
.getTitle().then(function(title) {
console.log("Title is: " + title);
});
Here we asked our browser to get the title of the page (the aptly named getTitle command), and
then logged that value out using the generic then command.
Note: If you’re not familiar with JavaScript Promises, this syntax may look a little strange. For the
sake of brevity, we’re not going to get into the details of it in this email. Fortunately there are a lot
of resources on JavaScript Promises15 already out there for you to find.
browser.url("http://learn.visualregressiontesting.com")
... tests are here ...
.end();
Just to recap, here’s what your tests.js file should look like:
browser.url("http://learn.visualregressiontesting.com")
.getTitle().then(function(title) {
console.log("Title is: " + title);
})
.end();
Now it’s time to run the test! You can do so by asking Node (via the command line) to ever so kindly
execute your code:
node tests.js
If all went well, you should have seen the following message in your command line:
Tomorrow’s outlook
So, how does it feel to have programmatically taken control of a browser? A little awesome, right?
Okay, if you’re underwhelmed, that’s because we didn’t really test much. We only peeked at the
page title then closed everything down before we got ourselves in to real trouble.
We promise that over the next couple of days we’ll get our hands extra dirty with mouse clicks,
keyboard taps, HTML checks and very real visual tests.
If you can’t wait until then, take a look at the WebdriverIO API page16 to see a full list of commands
available for use.
16
http://webdriver.io/api.html
Real Visual Regression Testing with
WebdriverCSS
It’s Day 3! We’ve spent the past two days getting things in place. Today is the day we finally use
WebdriverCSS!
GraphicsMagick
So we need to install one more tool.
GraphicsMagick is software that performs image manipulation and comparison. It allows us to
compare our images, and let us know when there are differences between the two.
To install it, follow the instructions in the WebdriverCSS Readme17 . Be forewarned that you may
need to uninstall ‘ImageMagick’ if you have it installed. Yes, they’re different software, and they
don’t always get along.
To test your installation, run gm from the command line. You should see a response that looks like:
Setting Up WebdriverCSS
Now that we have that installed, it’s time to make some changes to our tests.js file. First, we load
and initialize WebdriverCSS.
On a new line after your ‘.init(…)’ code, add:
require('webdrivercss').init(browser);
browser.url("https://learn.visualregressiontesting.com")
.webdrivercss("homepage", [
{
name: "header",
elem: ".header"
},
{
name: "benefits",
elem: ".benefits",
screenWidth: [320,640,1024]
}
])
.end();
In our code, we’re capturing two different elements on our homepage: the header and the benefits
section.
The header doesn’t change over different breakpoints, but the benefits section does. So to make sure
we test each of those breakpoints we use the screenWidth parameter in the second set of options.
This will produce a total of three images for the benefits element, one for each viewport size.
If you’d like to learn more about the options available to you, check out the documentation18 .
With ChromeDriver started, and WebdriverCSS ready to go, let’s run our tests!
node tests.js
To see the results of our tests, open up the newly created webdrivercss folder. In this folder you’ll
see the following files:
18
https://github.com/visualregressiontesting/webdrivercss#usage
19
http://learn.visualregressiontesting.com/code-samples.zip
Real Visual Regression Testing with WebdriverCSS 11
homepage.320px.png
homepage.640px.png
homepage.1024px.png
homepage.png
homepage.benefits.320px.baseline.png
homepage.benefits.640px.baseline.png
homepage.benefits.1024px.baseline.png
homepage.header.baseline.png
The first four images listed (without baseline in their name) are the full page screenshots
WebdriverCSS captures from Chrome. Once those images are captured, WebdriverCSS crops the
image down to your specified areas giving you the four baseline images.
browser.url("https://learn.visualregressiontesting.com/broke.html")
The regressions are the ‘next state’ version of your image. It’s useful to see the actual image that
was captured.
The ‘diff’ images are a comparison of the baseline and regression images, highlighting in hot pink
anything dissimilar between the two.
Up next
Snapping pictures of the initial page load is good, but what about dynamic content? In our next
lesson, we’ll learn some advanced features of WebdriverIO and see how to test interactive elements
like mobile navigation and login forms.
And… Action! Take Control with
WebdriverIO Commands
Yesterday we learned to use WebdriverCSS for capturing screenshots of our page. Today, we’re going
to take things one step further. Scratch that. We’re going to things several steps further by adding
“steps” to our tests.
Websites aren’t static. When you visit one, you’re taking all sorts of actions. You’re clicking buttons,
entering text, dragging boxes around. There’s a lot to do!
So far though, our tests haven’t done much. We’ve simply loaded our page, checked a title, and taken
a few screenshots. What about dynamic elements like dropdown menus and form fields? We should
totally test those too!
For example, if we wanted to test the main navigation of the outdatedbrowser.com website, our steps
would look something like this:
Starting Off
We’re testing a new page, so we need a new file. Let’s fill it in with the basics:
require('webdrivercss').init(browser);
var menuIcon = {
And… Action! Take Control with WebdriverIO Commands 14
browser.url("http://outdatedbrowser.com")
.webdrivercss("Hamburger Icon", [menuIcon])
.end();
So far there’s nothing new. We’ve moved our WebdriverCSS selector to its own variable, and the
reason why is coming up next!
Clicking around
The navigation on outdatedbrowser.com is hidden by default behind a friendly hamburger icon.
After having taken a picture of this icon using WebdriverCSS, we should click it to open the menu.
To do this, we’re going to use the ‘click’ command. And just like with the WebdriverCSS command,
we’ll pass in a CSS selector to specify the element we want to click:
browser.url("http://outdatedbrowser.com/en")
.webdrivercss("Main Menu Icon", menuIcon)
.click(menuIcon.elem)
See why we needed that menuIcon variable? Since we’re using the same selector for both Web-
driverCSS and our click command, it’s nice to keep it all in one variable.
FYI, click works almost exactly like an actual left click with your mouse. That’s the beauty of
Selenium; it’s as if you took the action yourself!
browser
... previous actions ...
.pause(1000)
.webdrivercss("Main Menu - Open", menu)
The pause command takes a number of milliseconds to wait. Since we want to wait a second, we’ll
pass in the number 1000, or 1000 milliseconds.
Now look at that! We have a Visual Regression Test for a dynamic element that animates in. That’s
complicated stuff!
Click a link
It would be good to know how the page looks after clicking a menu item and loading a new page.
We’ll use the ‘click’ command again to trigger a page load of the ‘Project’ menu item:
browser
... previous actions ...
.click("=THE PROJECT")
Notice anything different about this click command? In order to let WebdriverIO know which link
we want, we passed in an equal sign plus the text of the link.
Not only does WebdriverIO have strong support for CSS selectors, but it also supports text-based
selectors as well. Check out the selectors page20 for more information.
Finishing up
Last thing; Now that we’re on the Projects page, we should open the menu and capture the
‘active’ state of the project menu item. Even though we’ve loaded an entirely different webpage,
WebdriverIO is smart enough to understand what’s going on. We can write our test as one cohesive
script:
20
http://webdriver.io/guide/usage/selectors.html
And… Action! Take Control with WebdriverIO Commands 16
browser
... previous actions ...
.click("=THE PROJECT")
// WebdriverIO automatically waits here while the new page loads
.webdrivercss("Main Menu Icon - Projects", menuIcon)
.getUrl().then(function(url) {
console.log("Page url is: " + url);
})
end();
We threw the getUrl command in just to validate we’re on the right page, although our menu
screenshot should be validation enough.
In Review
Wow! We just pieced several parts together to make one very useful test. Hopefully you feel
more empowered in your testing ability. Check out the full WebdriverIO commands API22 to see
everything you now have the power to do.
Tomorrow, we’re looking at advanced WebdriverCSS usage. Until then, try writing some in-depth
tests on your own!
21
http://learn.visualregressiontesting.com/code-samples.zip
22
http://webdriver.io/api.html
Advanced WebdriverCSS Usage
As you advance in your test writing capabilities, you’ll find yourself in situations where a plain ol’
screenshot just won’t work. Maybe you need to test all images in three different resolutions, or you
need to hide some text because it’s always changing.
Good news, WebdriverCSS has you covered.
require('webdrivercss').init(browser);
There’s a second argument we can pass in to that ‘init’ call; an configuration object which defines
these aspects of our screenshots:
• screenshotRoot
– By default, screenshots are all saved to the ‘./webdrivercss’ folder. By passing in a custom
path, we can change where all the baseline screenshots are saved.
• failedComparisonsRoot
– Similar to screenshotRoot, this option defines where to store the ‘diff’ images. It defaults
to ./webdrivercss/diff.
• misMatchTolerance
– A number between 0 and 100 (default is 0.05) that defines the degree of mismatch to
consider two images as identical. Increase the number to be less strict in comparisons;
decrease to define smaller difference limits.
• screenWidth Numbers[] ( default: [] )
– When defined, will cause every capture to be taken in the different screen widths
requested
• updateBaseline
– Set this to true to automatically update the baseline images with the latest capture. Useful
for when you make an update to your design and want to redefine the images.
require('webdrivercss').init(browser, {
// example options
screenshotRoot: 'my-shots',
failedComparisonsRoot: 'diffs',
misMatchTolerance: 0.05,
screenWidth: [320,480,640,1024],
updateBaseline: true
});
Customize a screenshot
In our previous usage of the WebdriverCSS command, we showed example code but didn’t go on to
detail on the function itself. Let’s do that now!
The webdrivercss function takes 3 different parameters:
1. An ID: Each WebdriverCSS test needs to have a unique ID. This value will be the prefix for
all of the screenshot file names, and should therefore follow traditional naming conventions
like no spaces, dashes, or special characters.
2. Options: an array of option objects, each one representing a different part of the page that
you would like to test. Properties in the objects include:
3. name: Name of the captured element
4. elem: Selector of the element you want to capture
5. width: You can specify a fixed width for your screenshot
6. height: You can specify a fixed height for your screenshot
7. x: You can specify a fixed x coordinate for your screenshot (requires width/height)
8. y: You can specify a fixed y coordinate for your screenshot (requires width/height)
9. screenWidth: Pass through an array of screen widths to test this element at
10. Various other properties to hide, remove or exclude23 parts of the page, which can be useful
in order to ignore dynamic components such as advertisements. We’ll talk about these in a
moment.
11. The Callback: This function answers the “Ok I’ve got all of these images, what do I do now”?
We’ll leave it alone, but you can customize it gain information on the status of your screenshot
comparisons24 .
23
https://github.com/webdriverio/webdrivercss/tree/beta-rc1#usage
24
https://github.com/webdriverio/webdrivercss#let-your-test-fail-when-screenshots-differ
Advanced WebdriverCSS Usage 19
webdrivercss('startpage',[
{
name: 'header',
elem: '#header'
}, {
name: 'hero',
elem: '#hero'
}, {
name: 'headerbar',
x: 110,
y: 15,
width: 980,
height: 34,
screenWidth: [1200]
}
])
You can peruse through all the options via the documentation25 .
• Excluded
– This will cause the area or element defined to be covered in black
• Hidden
– This will cause the element defined to have ‘visibility: hidden’ applied to it. It will still
take up space on the page, only being invisible.
• Removed
– Uses ‘display: none’ to remove the defined element from the page, as if the element
didn’t exist on the page at all.
25
https://github.com/webdriverio/webdrivercss#usage
Advanced WebdriverCSS Usage 20
.webdrivercss('header', {
name: 'header',
elem: '#header',
exclude: ['#leaderboard-ad', '#mini-ad'],
hide: '.username',
remove: '#date'
});
1. Know that you are not limited to a single browser when testing. Pass in an array of browsers27
in your desiredCapabilities configuration and WebdriverIO will run your tests in each
browser.
2. WebdriverIO has built in support28 for Selenium cloud tools like Sauce Labs29 and Browser-
stack30 . These services are great for testing in a multitude of browsers, and provide consistency
when snapping screenshots through WebdriverCSS.
3. Automation and Continuous Integration go hand in hand. Getting hooked in to a tool like
Jenkins31 or TravisCI is a great next step in improving the value of your tests.
4. Sometimes you just need to see how someone else did it. The WebdriverIO code repo contains
several examples to browse through32 .
5. Get your tests organized by using a test framework like Mocha33 or Jasmine34 . These
frameworks make working with large test suites a breeze, and offer a deep set of features.
6. Once you start building out several test files, you’ll end up with that same WebdriverIO
configuration scattered across all of them. Switch over to using the WebdriverIO Test Runner35
to simplify your test setup.
7. Get your team on board with Visual Regression Testing by bribing them with cookies and
donuts.
8. Need more help? The WebdriverIO36 and WebdriverCSS37 chat rooms are open at all hours
for questions and help with the journey. Reach out to @micahgodbolt and @klamping to let
us know you’ve joined the fun.
27
https://github.com/webdriverio/webdriverio/blob/master/examples/wdio.conf.js#L71
28
http://webdriver.io/guide/testrunner/cloudservices.html
29
https://saucelabs.com/
30
https://www.browserstack.com/
31
http://webdriver.io/guide/testrunner/jenkins.html
32
https://github.com/webdriverio/webdriverio/tree/master/examples
33
http://mochajs.org/
34
https://github.com/jasmine/jasmine
35
http://webdriver.io/guide/testrunner/gettingstarted.html
36
https://gitter.im/webdriverio/webdriverio
37
https://gitter.im/webdriverio/webdrivercss
What’s Next for You? 22
9. For more information on Visual Regression Testing, check out the main website at visualre-
gressiontesting.com38 .
10. Want to learn more WebdriverIO? Check out the full course on it for in-depth video lessons39 .
That’s It
Again, thanks for reading through these emails and kudos on finishing the course.
38
http://visualregressiontesting.com
39
http://learn.webdriver.io/