<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gideon Pyzer on Medium]]></title>
        <description><![CDATA[Stories by Gideon Pyzer on Medium]]></description>
        <link>https://medium.com/@gidztech?source=rss-7113ec24ccf2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*r7NX-D6X4En-5U-jz9ObVg.jpeg</url>
            <title>Stories by Gideon Pyzer on Medium</title>
            <link>https://medium.com/@gidztech?source=rss-7113ec24ccf2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:03:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gidztech/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Visual Regression Testing]]></title>
            <link>https://medium.com/huddle-engineering/visual-regression-testing-ff7a1d31a112?source=rss-7113ec24ccf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff7a1d31a112</guid>
            <category><![CDATA[visual-regression]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[huddles]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gideon Pyzer]]></dc:creator>
            <pubDate>Mon, 25 Jun 2018 15:37:51 GMT</pubDate>
            <atom:updated>2019-01-12T15:13:16.589Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M3m8zNw6beeOHPg8kAqYlg.png" /></figure><p><strong>TL;DR: </strong>Visual Regression Testing captures regressions in CSS in an automated way, using screenshot image comparisons. You should use Docker to avoid environmental differences, and <a href="https://github.com/gidztech/jest-puppeteer-docker">jest-puppeteer-docker</a> with <a href="https://github.com/americanexpress/jest-image-snapshot">jest-image-snapshot</a> are the perfect tools for achieving this.</p><h3>🎬 Introduction</h3><p>Automation tests are usually concerned with the functional requirements of an application. This post will show you why this might not be enough to have peace of mind when deploying your web app UI projects to production.</p><h3>Does the UI look right? 🤔</h3><ul><li>You can test that particular DOM elements exist on the page when certain criteria are met, such as clicking a button and performing an action.</li><li>You also might test that the router is correctly initializing the right page or component when a particular end-point is hit.</li></ul><p><strong>Question: How do you test if the application actually looks right?</strong></p><p><strong>Answer:</strong> One way is to manually look. We’re usually quite good at spotting large regressions, such as colour changes or content overflowing on the page.</p><p><strong>👎 Problems:</strong></p><ul><li>The application is large. There may be many different screens, and some will show under very specific, rare conditions (e.g. permission based)</li><li>What if the application is responsive, and one needs to test multiple breakpoints for different devices and screen sizes?</li><li>What if the tester simply misses something? They could miss both small and larger regressions, due to <a href="https://www.verywellmind.com/what-is-change-blindness-2795010">Change Blindness</a>.</li><li>Time is a factor too. With all these different scenarios, it might simply be too time consuming for developers and QA to do a thorough job.</li></ul><p>An automated mechanism for testing how the UI looks is needed. What if you could take a photo of the UI component before and after a breakage and compare the difference in a computery kind of way? <em>(Puts nerd hat on)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/120/0*za4zJEJJR_dw8Qop.png" /></figure><h3>🎨 Pixel Comparisons</h3><p>Every image is made up of pixels, which are assigned a red, green and blue value. In a simple model, you could take screenshots of the UI and compare every pixel’s colour in screenshot one with the pixel colour in the corresponding location of screenshot two. If any pixels are different, then you know the images are not identical.</p><p>Maybe a single pixel difference is quite a high threshold, so let’s say that there’s probably a problem if 1% or more of pixels are different.</p><p>It’s not quite as simple as that. There are other factors that might give false negatives. Aliasing occurs when an image’s resolution is too low for the processor to accurately render smooth lines. You end up with jagged edges, often in text. Operating systems will apply <strong>anti-aliasing</strong> techniques to give a smoother appearance, but they all do it slightly differently.</p><p><strong>Problem:</strong> You can get different comparison results per environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*43WJQ6Gc_gv3a54ngWuSnw.png" /><figcaption><em>Source: </em><a href="https://helpx.adobe.com/photoshop/key-concepts/aliasing-anti-aliasing.html"><em>https://helpx.adobe.com/photoshop/key-concepts/aliasing-anti-aliasing.html</em></a></figcaption></figure><p>Image comparison libraries can deal with anti-aliasing problems by ignoring some of the pixels where anti-aliasing is likely to have occurred. Examples of libraries include <a href="https://github.com/HuddleEng/Resemble.js">ResembleJS</a> and <a href="https://github.com/mapbox/pixelmatch">Pixelmatch</a>.</p><h3>Visual Regression Testing Process</h3><h4>❶ Take screenshot of the UI in good state</h4><p>The visual regression library or framework of your choice will provide a function to do a visual comparison. When it’s invoked on a particular UI element, a screenshot is taken of it. If one doesn’t already exist, the test will automatically pass and output the screenshot to disk.</p><h4>❷ Store the screenshot in source control</h4><p>After running the tests, you version the test suite and associated screenshot in source control, e.g. Git. This will become the baseline image.</p><h4>❸ Run the tests again after a code change</h4><p>After making a code change, the test suite can be run again. Code changes, once committed, can also trigger tests configured in a CI environment. The visual regression library or framework will see that a baseline exists and will take a second screenshot of the UI element.</p><h4>❹ Compares screenshots</h4><p>The new screenshot is automatically compared with the baseline image to see if there are any visual differences.</p><h4>❺ Pass or fail test based on visual threshold</h4><p>The test will be sent a pass or fail signal depending on how different the screenshot is. This is based on the threshold configured.</p><h4>❻ (Optional) Rebase visual if valid change</h4><p>If the visual test has failed due to an intended change, you can rebase the visual so that subsequent test runs will pass. This is essentially done by removing the old image and running the test again to generate a new image.</p><h3>👍 Benefits</h3><p>Say you have a Panel component which displays a title and a body. You use it in a few places and release it to production.</p><p>You later decide to add an icon to the panel in one mode, so you give it a property to enable the icon. You conditionally put the icon next to the title if it exists. You’re happy with how that looks and you integrate it into your page.</p><p>You later find out from a customer that the panel is misaligned.</p><p><strong>What happened?</strong></p><p>Well, when you added the icon next to the title, you added a margin. But what you didn’t realise is that you applied the margin to the title element, which is not scoped to the new property. Now there’s a margin in both modes!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*UrOnBybjg71i5PF6sNbv6A.png" /><figcaption>Original</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*GV6CQA30lZ4iyk44.png" /><figcaption>Broken</figcaption></figure><h3>⌨️ Large refactors</h3><p>You may be doing a large scale architectural refactor. For example, you have a CSS file that you need to split up.</p><p>In CSS, the order of imports is important for specificity. So if you get the wrong order, your application could look different. Sometimes this will be quite obvious, but perhaps not. You might subtly break your hover states.</p><h3>🖍 Theming</h3><p>You might provide theming capabilities to your application, where you set theme styles for each customer. It’s not ideal to test the whole application again for every single theme because functionally it will work the same. Instead, you can just use visual comparisons to ensure styles are correct.</p><h3>✋ Limitations and solutions</h3><p>If you decide to take screenshots of dynamic content, you’re in for some trouble. Taking multiple screenshots of the BBC News website even within a 5 minute window could yield different results. It’s important to remember to stub dynamic content within the system under test.</p><h3>🚀 Animations</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/150/1*CcCaCxUqo5qwWM4pSztIdw.gif" /><figcaption>This is just an image</figcaption></figure><p>Animations are a lovely enhancement to your application, but they can play havoc with visual regression testing. Imagine a spinner component. If you take a screenshot of it twice, there’s a fair chance that the visual will be different. Animation speeds can vary by milliseconds, so you might get a screenshot of the spinner at its starting position in one case and the middle position in the second case.</p><p>The solution here is to disable animations in your tests.</p><h3>🖱Browsers</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*-CY4OPWo5auUVqXRt-96qg.png" /></figure><p>People use different browsers, so you should be testing that the UI looks right in a handful of them. Visual regression testing isn’t really about checking cross browser rendering. It’s designed to look at regressions, and you usually find most dev regressions in one particular browser. It’s an expensive job to run all your tests against every browser, so it’s usually sufficient enough to manually smoke test or use something like <a href="https://www.browserstack.com/">BrowserStack</a>.</p><h3>💻 Operating systems</h3><p>Operating systems will render content differently. If you look at your app in Windows and compare it to MacOS, it will likely look different. This is a problem for visual regression testing where you have teams on different environments. It’s important to make sure that screenshots are taken under the same environmental conditions every time, otherwise it defeats to purpose of automation testing.</p><h3>📦 Docker</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*Z0QLRsFBmG4JvAXzMYDLuQ.png" /></figure><p>Docker allows you to host a different environment within the current OS. It’s like a virtual machine, but it works a little differently. The important part here is that you declaratively provide a recipe for how the environment should be configured, and it’s followed every single time a container is started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*-XAabYXfEA_YZceEKx8XcQ.png" /></figure><h3>🛠 Libraries and frameworks</h3><p>Let’s now take a look at some of the visual regression solutions available.</p><h3>PhantomCSS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*gR9EeGqTsHHKQ0yZpQhdxg.png" /></figure><p><a href="https://github.com/HuddleEng/PhantomCSS">PhantomCSS</a> was one of the first open source front-end visual regression testing libraries around. It was released by <a href="https://twitter.com/jamescryer">James Cryer</a> at <a href="https://www.huddle.com/">Huddle</a> back in 2013 and has become quite a popular tool.</p><p>It uses <a href="http://phantomjs.org/">PhantomJS</a>, a headless browser (no GUI), to render web applications. On top of that, <a href="http://casperjs.org/">CasperJS</a> provides a comprehensive API for interacting with the browser and the DOM. For example, you can click on buttons, navigate to pages, get and set properties on page elements, etc. James also created the image comparison library, <a href="https://github.com/HuddleEng/Resemble.js">ResembleJS</a>, to compare the screenshots.</p><p>The main problem is that PhantomJS is no longer being maintained as a project. It’s based on an old fork of Webkit, which lacks support for modern browser features, such as <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout">CSS Grid</a>. It’s possible to use <a href="https://slimerjs.org/">SlimerJS</a> instead, which is based on Gecko, the rendering engine behind FireFox. However, the technology for communicating with the browser is not standardized.</p><h3>First Solution: Muppeteer</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*8d0KQgs5pnstLlzI.png" /><figcaption>Logo by: <a href="https://twitter.com/hsincyeh">Hsin-chieh Yeh</a></figcaption></figure><p>Due to the problems with PhantomJS, a newer solution is needed. Originally, I was writing a lower level library to work with the <a href="https://chromedevtools.github.io/devtools-protocol/">Chrome DevTools Protocol</a>, but Google released <a href="https://github.com/GoogleChrome/puppeteer">Puppeteer</a>, which did exactly what I was trying to do.</p><p>Puppeteer allows you to interact with the browser and pages in same way CasperJS does with PhantomJS. The difference is you get a lot more control, and a standardized protocol is used.</p><p>I decided to write a framework called <a href="https://github.com/HuddleEng/Muppeteer">Muppeteer</a>, for running Mocha tests with Chrome via Puppeteer, and allow for visual testing using Docker. The image comparison library used is <a href="https://github.com/mapbox/pixelmatch">Pixelmatch</a>, a lighter version of ResembleJS.</p><p>One of the benefits of the framework is the abstractions for test setup. In Muppeteer, many of the abstractions occur using a wrapper describeComponent block. You can configure the URL to navigate to here, or you can do that in a more central place, like in the example below.</p><p>The important part is the assert.visual() function. This is what performs the visual comparison. All you provide is a DOM selector for the component you want to take a screenshot of and the framework will do the rest.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00d825c079bad7776ec0c3d8d69c3e08/href">https://medium.com/media/00d825c079bad7776ec0c3d8d69c3e08/href</a></iframe><p>To configure your test suites, you just have to invoke a configuration function in a Node script as your entry point for running the tests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c59578c11cb60f4971f6f85c38dc8b07/href">https://medium.com/media/c59578c11cb60f4971f6f85c38dc8b07/href</a></iframe><p>You can configure a function that returns a URL to navigate the page for all tests, based on arguments passed to describeComponent in your test. You can alternatively manually navigate within the test itself.</p><p>The other interesting configuration is setting up Docker. Normally, you’d have to create a Dockerfile for creating all the dependencies for the image. With Muppeteer, the only dependency you have is having it installed on the computer. By setting the dockerChromeVersion property, when you run the tests, Muppeteer will automatically pull a Docker image with that version of Chrome installed. The test runner will then start a container and run the tests against the Chrome instance running inside.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1zZomPL1GDy45dhP_9f-3w.png" /></figure><p>When running your test suites, you will see a percentage mismatch in the terminal when a test fails. You can then go to the screenshot path to open it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*UrOnBybjg71i5PF6sNbv6A.png" /><figcaption>Original</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*bBK5cRw8O3jN2UK1-VBjrA.png" /><figcaption>Broken</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*3lFYgrbyXgF3CU2z91_hIg.png" /><figcaption>Diff</figcaption></figure><p>You can take a look at <a href="https://github.com/gidztech/vrt-examples/tree/master/examples/muppeteer-example">muppeteer-example</a> for an example repository including both a unit test and an end-to-end test suite.</p><h3>🎉 Jest Puppeteer Docker with Jest Image Snapshot</h3><p>While I was creating Muppeteer, American Express were also working on a Jest plugin called <a href="https://github.com/americanexpress/jest-image-snapshot">jest-image-snapshot</a>. The main difference is that it&#39;s not a full framework, it&#39;s a just a plugin library. The extensibility of Jest is great.</p><p>Originally, it was quite cumbersome to configure and setup Puppeteer with Jest via their interface hooks. However, with improved configuration options in new versions, the developers at <a href="https://www.smooth-code.com/">Smooth Code</a> created <a href="https://github.com/smooth-code/jest-puppeteer">jest-puppeteer</a>.</p><p>Jest Puppeteer is a brilliant library that launches Chromium and runs your Jest tests against it, rather than the standard Node environment. You can then bring in jest-image-snapshot and configure your visual tests really easily.</p><p>The problem here for Visual Regression Testing is that the Chromium instance doesn’t run in a Docker container, which may cause some of the environmental problems I outlined earlier in this post.</p><p>I solved this by publishing <a href="https://github.com/gidztech/jest-puppeteer-docker">jest-puppeteer-docker</a>, a plugin that wraps Jest Puppeteer, but instead, launches Chromium in a Docker container. It works by looking up the version of Chromium linked to the Puppeteer dependency in your project, and then pulls a Docker image containing that version of Chromium. Tests will run against that version.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c74710897483b3893200168d6c08991e/href">https://medium.com/media/c74710897483b3893200168d6c08991e/href</a></iframe><p>Much like Muppeteer, Jest provides hooks for you to put functions and other state on the global object before the test runs. In the case above, I was able to hide the configuration for jest-image-snapshot and the URL for the page.</p><p>You can <a href="https://github.com/gidztech/jest-puppeteer-docker/tree/master/example">take a look at an example config and test</a>, which includes reporting.</p><p>This is my recommended solution now, as it’s more flexible than the lockin associated with frameworks like Muppeteer. Jest is also very powerful.</p><h3>More</h3><p>There are several other libraries and frameworks available that implement visual regression techniques. Some use more legacy technology, such as PhantomJS, but others use more modern techniques.</p><p>Check out <a href="https://github.com/mojoaxel/awesome-regression-testing">awesome-regression-testing</a> for an extensive list.</p><p><em>Originally published at </em><a href="https://www.gideonpyzer.com/blog/visual-regression-testing/"><em>www.gideonpyzer.com</em></a><em> on June 25, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff7a1d31a112" width="1" height="1" alt=""><hr><p><a href="https://medium.com/huddle-engineering/visual-regression-testing-ff7a1d31a112">Visual Regression Testing</a> was originally published in <a href="https://medium.com/huddle-engineering">huddle-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>