What is visual regression testing? It’s a useful testing method that checks whether any visual regression has occurred in our application. It typically involves comparing two versions of an application (for instance, the staging environment and the test environment) by capturing full-page or element-specific screenshots based on HTML selectors. Afterward, testing libraries compare these images pixel by pixel to detect possible differences.

The graphic below illustrates this concept

visual regression testing playwright

When is this type of test useful?

For key landing pages for different locations and languages, manual testing becomes difficult, time-consuming, and almost impossible at scale. Moreover, it’s worth introducing this type of test for the most important parts of our web application even if it doesn’t directly influence our sales process, it still affects our branding.

It’s worth using automated visual testing because:

  • It quickly verifies whether, after introducing new changes (e.g., those made by the front-end developer) everything works as before.
  • This method is especially useful with UI rebranding or bigger UI changes – it helps confirm that everything works correctly and looks as we expected after implementing a new UI.

What potential challenges can we expect with this type of test?

  • Fonts may render differently depending on the operating system and web browser.
  • Diverse screen resolutions can cause issues in displaying different elements on the site.
  • There’s always a chance that approving new baselines may lead to overlooking real visual regression testing issues.
  • With frequent changes in the UI, it’s essential to continuously update baselines, which increases the cost of maintaining tests and time spent on them. 
  • Managing test results on a large scale can also be time-consuming and lead to software bloat.
  • Playwright stands out because visual regression testing functionality is available out of the box.

Additionally, there are a few tools that allow extending the basic capabilities for visual regression in Playwright. Similar solutions are also available for other test automation tools like Selenium, Cypress, and others. It’s worth mentioning about BackStopJS – a tool I used, which was also helpful.

https://github.com/garris/BackstopJS

visual regression testing playwright

Interestingly, BackStopJS uses Playwright underneath (not only Playwright, but also Puppeteer).  As a result, BackStopJS offers support for modern browsers and a robust solution.

BackStopJS can be another utterly useful tool. In BackStopJS we use specially prepared JSON file format then we can add our selectors, URLs, and areas that we want to take for creating visual regression tests. For simple tests – without logging, it can be easier to set than visual regression in Playwright but doing visual regression tests for logged areas is also useful and necessary as well.

What options are available in Playwright for using visual regression in test automation?

  • Solution out-of-the-box – Playwright allows users to add visual regression checks to existing tests without a very complicated setup.
  • Visual Regression Tracker – A more advanced open-source tool that runs using several Docker containers. It enables integration with many automated testing tools, not just Playwright. Since it’s an open-source tool, we can quite easily deploy it on our servers.
  • Aria snapshot – It’s quite a new approach for creating assertions for accessibility tree and it can check the structure of elements on the page – it looks promising, but it has some potential traps like working with more dynamic sites or when our site supports more than one language and our accessibility tree is different – of course these type of challenges we can expect for visual regression as well but probably in future I will write a post about that and I will show how to try it for advanced examples.
  • Commercial solutions –There are many commercial tools available on the market such as Applitools, and Percy, and others however, the cost of using them at an appropriate scale can be too high.

What test application do we use for this article?

We will use The internet application as our test application

visual regression testing playwright

To develop our skills as an automated tester, I recommend the website https://the-internet.herokuapp.com/, which offers a lot of modern UI controls and examples of small internet applications. It is perfect for practicing test automation skills.  

These examples are ideal for practicing with tools like Playwright, Selenium, Cypress, and other test automation tools.

visual regression testing playwright

For this article, I will use a shifting content example – a case where, after each refresh of the page, the page’s picture changes its location. Moreover, we can define how many pixels the image will be moved and add an element of randomness to where the picture will appear on the site.

The code of our test looks like this

visual regression testing playwright

It’s a simple test that enters the site and then checks a potential visual regression at the end by using an assertion. I ran this test twice to see differences – the picture on the site appears in a different location each time. For this purpose, I use the option pixel_shift=200,

which moves the picture by a different number of pixels on the site after each run.

visual regression testing playwright

It’s important to remember that these tests fail on the first run because Playwright doesn’t have a baseline – that’s a default screenshot for the application to compare test results.

To create a baseline like this, we need to run a test to generate the baseline screenshots or use a specific method to generate them. This method also helps to create new reference images when, e.g., the layout is updated and it’s good to use in our pipeline.

npx playwright test --update-snapshots

This command creates a baseline for tests.

Playwright has created three images based on this test.

Another method to compare is passing the name of a file containing our screenshot to compare it. It can be useful in cases when we need to display certain resources e.g., a picture downloaded from another service, and check whether it is available at a defined place on our site.

Apart from the useful method of capturing a full-page screenshot by setting fullPage: true, there is access to various settings that can be used in tests

How we can increase or decrease the level of these checks?

MaxDiffPixels parameter defines the acceptable difference in pixel count for automated tests. If the value is larger, a great number of pixel differences will be allowed.

How can we add a mask for a specific element or multiple elements?

visual regression testing playwright

When we use the mask parameter in our automated test, a mask will be applied. It means that instead of a certain HTML element (in this case, the element with the H3 selector) a defined color will appear in its place. The default mask color in Playwright is pink. 

By using the maskColor parameter we can independently define a color for the mask. In my case, I set the color pink as well to demonstrate how you can manually set any color.

Additionally, the mask parameter takes an array of elements, allowing us to define multiple elements to mask – it doesn’t need to be one element.

visual regression testing playwright

Is it possible to turn off animations on the site in visual regression testing?

According to Playwright documentation, it is possible to use the parameter animation: ‘disabled’, which causes those animations to be ignored when taking a screenshot. This is useful because modern web applications often include many animations and dynamic elements.

In cases where a web application has many of these types of elements, it’s worth considering taking a screenshot only of selected elements on the site instead of taking a screenshot of the entire page. As a result, our tests will be more resilient to potential failures caused by dynamic changes.

Visual regression testing for different resolutions

Different resolutions:

visual regression testing playwright

It should be noted that using different resolutions and operating systems helps increase coverage and the chance of finding potential issues. Testing on various resolutions and emulating mobile resolutions can be realized by editing the settings in the playwright.config.ts file.

Other useful settings

Apart from using other resolutions which we can set in the playwright.config.ts we can define other settings here, like the threshold and maxDiffPixelRatio globally for all checks in our solution.

As a reminder, in the configuration file, we can define not only resolutions but also the name of the browser and its version. In this way, we can check whether there are any visual regressions between various versions of browsers.

What should we do when we want to increase coverage for various operating systems and more specific configurations?

If we want to increase the number of covered browsers and improve browser diversity, there are a few options. We can use commercial services from companies that offer cloud-based web browser services. In this way, we can easily cover a wide variety of configurations.

An alternative solution is using Moon – a commercial but quite approachable solution for running Playwright tests remotely, even on a big scale, and setting it up in our infrastructure.

Summary

n this first part of the series on visual regression testing in Playwright, we’ve explored how to get started. In the next article, I will introduce Visual Regression Tracker—an open-source tool that offers more features than Playwright’s default visual regression mechanism. Stay tuned!

Author

Michał Śleżak

Michal Slezak brings over 9 years of expertise in test automation, having led projects for prominent banks and contributed to aviation and other major industry initiatives. His experience spans from large enterprises to innovative startups, including ArtistGrowth and WhatClinic.com. Currently, as a Test Architect at Sii, Michal plays a key role in diverse projects and initiatives. He also shares his knowledge through his blog, testingplus.me, focused on test automation in Polish, and actively engaged in the testing community by delivering training sessions and speaking at renowned conferences like QA Global Summit, 4Developers, TestWarez, TestFest, ConSelenium, and TestCamp.

Related posts:

StarEast 2025 – Join Me for My Talk!

Hey everyone! I’ve got some exciting news that I can’t keep to myself any longer. This year, I’m heading to StarEast 2025 – and not just as an attendee, but as a speaker! 🎉 StarEast is one of the biggest testing conferences in the world, and I’ll be there representing Sii Poland. The event takes ...

Read more

Visual Regression Tracker – it’s a useful an open-source tool which extend a default functionalities in Playwright.

Today, I’d like to introduce a more advanced tool for visual regression testing: Visual Regression Tracker. If you’ve worked with visual regression testing before, you know that sometimes it’s necessary to involve multiple people in the approval and analysis process to determine which changes are expected and what are not. While tools like SauceLabs, BrowserStack, ...

Read more

Leave a Comment