TestQuality Blog

Visual Regression Testing with Playwright

Unit Tests

What is Playwright?

Playwright is a Microsoft-created NodeJS package with capabilities that are quite similar to Puppeteer. Both libraries enable you to automate browser-based tasks.

With Playwright, you can launch or connect to a Chrome, Edge, Safari, or Firefox browser and exchange instructions. The DevTools protocol (for Chrome browsers) and a bespoke protocol (for Firefox and WebKit browsers) are used for these messages.

The primary benefit of adopting Playwright over Puppeteer is that Playwright supports many browser manufacturers, whereas Puppeteer exclusively supports Chrome-based browsers.

How can I automate testing using Playwright?

Using a browser automation framework with a testing framework is one of the use-cases. Popular testing frameworks like Jest and Mocha already allow Playwright testing.
These test frameworks enable you to launch or connect to a browser and carry out the tasks specified in your test cases.

It's vital to remember that Playwright will operate in headless mode by default. This indicates that the user will not be able to see the browser's UI. You will not see the browser do these activities when running your automated tests. Headless mode is used to accelerate Playwright execution: it decreases CPU utilization because no UI updates are necessary, and it increases overall execution speed.

What exactly is Visual Regression Testing (VRT)?

Visual Regression Testing is used to prevent unintended changes to the visual state of an application. Assume you have a website and know exactly how the page should appear. You tell your test framework to take a snapshot of your pixel-perfect website and tell it that the page should always appear exactly like the screenshot you just captured.

In an ideal world, your webpage would always appear flawless to the end user, exactly like the snapshot you took. However, a change to your website by a developer or another member of your team may occasionally result in a (visual) problem. This might be a missing DOM element on your page, a CSS modification that causes rendering troubles, or any other error that results in a poor user experience.

This is where Visual Regression Testing shines: it will notify you if an unanticipated change happens on your website, resulting in a visual bug.



Libraries for Visual Testing

There are several open-source and licensed visual comparison libraries accessible online that may assist you in setting up and doing regression testing.

Popular Testing frameworks such as BackstopJS for Puppeteer and Loki provide strong ways for developers and QA to rapidly and simply build up visual regression testing.

In the case of Playwright testing, Playwright Test has an integrated Visual Comparison tool that allows you to take and visually compare screenshots with Playwright.

Setup Visual Comparison Playwright Evaluation

Playwright includes the Playwright Test package, which is designed as a tool for doing automated tests using Playwright.

When you run a test for the first time, Playwright Test will capture a snapshot of the page, known as a baseline screenshot (or golden picture), and put it in a folder. 

Following tests will continue to capture screenshots and compare them, pixel by pixel, to the baseline screenshot.
Assuming you saved your silver picture as silver.png, a simple assertion would be as follows:

expect(await page.screenshot()).toMatchSnapshot('silver.png');

Where page is a Playwright Test object that attempts to replicate the output of page.screenshot() with the contents of silver.png.

When a test fails because the page's contents have changed, you will receive a test error stating that the snapshot no longer matches the golden picture.

Internally, Playwright Test compares screenshots for visual changes using the pixelmatch library. To fine-tune the picture comparison, numerous arguments may be passed to this library. The threshold option is the most crucial, as it specifies the amount of difference permitted between two photos.

What are some of the most popular visual testing libraries?

Visual regression testing is becoming increasingly common among QA and development teams.
There are a plethora of visual testing libraries accessible online, such as BackstopJS for Puppeteer or Loki. Through Playwright Test, Playwright provides its own built-in visual regression testing solution.
You may use Playwright's Visual comparison testing to compare reference screenshots to fresh screenshots.

TestQuality will be offering Playwright integration very soon as it does with other testing frameworks such as Junit, Selenium, Jest or Mocha. 

Sign Up for a Free Trial and add TestQuality to your workflow today!

TestQuality's import feature adds test cases from different platforms, manual testing results from spreadsheet files, test automation XML, and custom CLI integration with your automated testing.



TestQuality can simplify test case creation and organization, it offers a very competitive price but it is free when used with GitHub free repositories providing Rich and flexible reporting that can help you to visualize and understand where you and your dev or QA Team are at in your project's quality lifecycle. But also look for analytics that can help identify the quality and effectiveness of your test cases and testing efforts to ensure you're building and executing the most effective tests for your efforts.

TestQuality | Test Case Management Tool