CSS often breaks as all styles live in the global namespace across the page. See how to perform CSS regression testing with Selenium to prevent this issue.
We all know that CSS often breaks as all our styles live in the global namespace across the whole page. There are tools to limit this risk such as CSS modules or Styles Components (for React JS) but these ones force you to follow their standards even if you don’t like them.
You may also be interested in tools such as “Pixel Perfect” but these ones force you to manually compare each section, which is definitely not going to help you in the long term.
Fortunately, we may automate this process! The Internet lacks resources on this but they are there and this article is going to be a summary of the most important things concerning the configuration of Hermione.js.
Regression testing means testing existing software applications to make sure that a change or addition hasn’t broken any existing functionality. Regression testing is the process of testing changes to computer programs to make sure that the older programming still works with the new changes. Regression testing is a normal part of the program development process.
Selenium is a portable framework for testing web applications. It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages. The tests can then run against most modern web browsers. Selenium deploys on Windows, Linux, and macOS platforms.
It is open-source software, released under the Apache 2.0 license: web developers can download and use it without charge. Testing in Selenium may be performed in parallel, meaning less waiting time.
Hermione.js is a “browser test runner based on mocha and wdio”. It opens the desired browser and performs actions defined in the test suites. Tests run in parallel so the waiting process is short.
Hermione has a very feature-rich API to cover all your needs. You may, among others, type your desired resolution to capture the screen at.
But what’s most important for us – we may take a reference screenshot of a page and then compare this screenshot each time we make a change in UI to make sure that we touched only what was intended.
Testing with Hermione.js is a plain process with minimal configuration.
Let’s say we have the following dummy page:
Let’s pretend we want to make each option 10% taller. We write some simply transform and check what it looks like:
The change is very subtle. Each option is a pixel or so taller so at first everything looks cool. Let’s run Hermione just to make sure:
Oops! It looks like we accidentally also upscaled the description on the right!
See what the difference between the original and changed image looks like?
Testing with Selenium and Hermione.js, you can spot unwanted visual changes immediately before they hit your users, making both devs and businesses happy.
When you run a diff and the only differences are the intended ones, then you may save the image as new references using hermione –update-refs.
You will need “selenium-standalone”.
After you install Selenium Standalone, type selenium-standalone start in your command line.
Install Hermione globally -> npm install -g hermione.
Install “HTML reporter” – a Hermione plugin for reporting differences between two images -> npm install html-reporter.
Create a Hermione configuration file called .hermione.conf.js in, for example, the following manner:
module.exports = {
sets: {
desktop: {
files: 'tests/common'
}
},
baseUrl: 'http://localhost:3000',
gridUrl: 'http://localhost:4444/wd/hub',
compositeImage: true,
browsers: {
chromeXL: {
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
args: ['--headless'],
},
},
windowSize: '1300x900',
},
chromeMobile: {
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
args: ['--headless'],
},
},
windowSize: {
width: 414,
height: 800
},
},
safariXL: {
desiredCapabilities: {
browserName: 'safari',
},
windowSize: '1300x900'
},
safariMobile: {
desiredCapabilities: {
browserName: 'safari',
},
windowSize: {
width: 414,
height: 700
}
}
},
plugins: {
'html-reporter/hermione': {
enabled: true,
path: 'my/hermione-reports',
defaultView: 'all',
baseHost: 'http://localhost:3000'
}
}
};
Note: We are running headless Chrome here. It’s much faster and better for the developer.
You may read more about headless Chrome here: https://developers.google.com/web/updates/2017/04/headless-chrome
Create a simple test in tests/common such as skills.js and fill it with your test suite, e.g.:
it('creates a screenshot of the skills page', function() {
return this.browser
.url('/skills')
.assertView('Whole page', '#root')
});
Note: assertView is the most important method here. It’s used to compare images.
If it is the first time you take a screenshot, type hermione –update-refs in your terminal window.
Each time you make any visual change type hermione.
If any of your tests do not pass, it will be reported with an original, changed and diff images in:
file:///Users/<USER>/path/to/project/my/hermione-reports/index.html
You will see the above path in the terminal if any of your tests fail.
As you can see, setting up regression testing tools isn’t difficult. Using Selenium + Hermione.js can boost your productivity and eliminate (or at least heavily reduce) visual regression bugs. If you find your team struggles with constant CSS issues, Hermione.js is definitely a good idea.
You may also skip certain tests for selected browsers. As I mentioned, Hermione has a very rich API.
Read next
Top reads
Become a better tech leader.
Join 200+ CTOs, founders and engineering managers and get weekly bite-sized leadership lessons that take <60 seconds to read.