DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk6 min

How to Set Up Screenshot-Based Visual Regression Testing in Playwright

Use Playwright Test’s built-in screenshot assertion to create reviewed visual baselines and catch unintended UI changes in later runs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright Test’s built-in expect(page).toHaveScreenshot() assertion. The first run creates a reference image; later runs capture the page again and compare it with that reviewed baseline. Commit the reference images to version control, keep baseline generation and CI comparisons in a consistent environment, and inspect any proposed baseline update before accepting it.

1. Add a screenshot assertion

Screenshot assertions are part of the Playwright Test runner. A minimal test navigates to a page and asserts that its screenshot matches the stored reference:

import { test, expect } from '@playwright/test';

test('landing page visual layout', async ({ page }) => {
  await page.goto('https://example.com');
  await expect(page).toHaveScreenshot('landing.png');
});

Use a named PNG for an explicit snapshot filename. Playwright also supports lossless WebP snapshots with a .webp extension. If you omit the name, Playwright derives one from the test and snapshot sequence. The official visual comparisons guide and page assertion API document these behaviors.

Generate and review the first baseline

  1. Run the test with your project’s normal Playwright Test command, such as npx playwright test.
  2. On the first run, Playwright reports that the reference snapshot is missing and writes an actual screenshot. It captures until two consecutive screenshots match, then saves the last image as the reference.
  3. Open the generated image and verify that it represents the intended design and state. Do not treat a newly generated file as correct without review.
  4. Commit the snapshots directory with the test so later runs have a baseline to compare against.

By default, snapshot names include browser and platform information; with multiple configured projects, the project name is included. Use the default per-test organization unless you have a concrete reason to configure a shared or custom snapshot location.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

2. Make local and CI captures comparable

Screenshot diffs can come from the environment rather than a product change. Playwright’s official guide says, “For consistent screenshots, run tests in the same environment where the baseline screenshots were generated.” Host operating system, browser version, settings, hardware, power source, and headless mode can all affect rendering. In practice, generate and compare baselines with the same operating system, browser build, project settings, and installed fonts where practical.

Also make the capture’s viewport and target browser explicit in your Playwright project configuration. If different browsers are in scope, keep their references separate and compare each browser against its own baseline rather than comparing unlike renderers.

3. Choose the right screenshot scope

Assertion Use it when Key option
toHaveScreenshot() on the page The viewport or overall page layout is the behavior under test. fullPage: true captures the full scrollable page instead of only the visible viewport.
toHaveScreenshot() on a locator A particular component or region is the target and the rest of the page is irrelevant to this assertion. Use the locator’s screenshot assertion so the comparison is scoped to that element.

Full-page capture can make a test sensitive to content far below the fold. Prefer a locator assertion when it more accurately expresses what the test promises to protect. The relevant options are described in the page assertion API.

4. Control dynamic content without hiding meaningful regressions

Playwright already reduces some transient differences: screenshot assertions wait for two consecutive page screenshots to match; animations are disabled by default, with finite animations fast-forwarded and infinite animations canceled during capture and resumed afterward. The caret is hidden by default. These measures do not make changing page data deterministic.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Mask only irrelevant changing regions

If a timestamp, rotating avatar, or other volatile region is not part of the visual behavior under test, mask it for the assertion. Alternatively, use stylePath to hide or adjust changing content during capture. A stylesheet supplied this way can affect Shadow DOM and inner frames. Keep the mask narrow: anything masked or hidden is no longer checked for visual regressions.

Keep the UI state itself reproducible

  • Use stable test data and a predictable page state before capturing.
  • Wait for the relevant content to appear rather than relying on a fixed delay when a selector can express readiness.
  • Choose page or component scope deliberately so unrelated dynamic areas do not create noise.
  • When reviewing a diff, distinguish a real layout or styling change from rendering variation before changing tolerances.

5. Tune pixel comparison deliberately

Playwright uses pixelmatch for visual comparison. The settings control different parts of the decision: threshold decides how different an individual pixel may be before it counts as changed, while maxDiffPixels and maxDiffPixelRatio cap the total changed pixels or their fraction of the image. The documented default threshold is 0.2; the maximum-pixel and maximum-ratio caps are unset by default.

Option What it controls Documented default
threshold Per-pixel perceived color difference; 0 is strict and 1 is lax. 0.2
maxDiffPixels Maximum number of pixels allowed to differ. Unset
maxDiffPixelRatio Maximum fraction of image pixels allowed to differ. Unset

Begin with the defaults and inspect actual diffs. Relax a setting only when you can explain which harmless variation it absorbs and why meaningful changes will still fail. There is no universal tolerance suitable for every page and rendering environment.

Set shared defaults when a policy applies across tests

Playwright supports shared screenshot assertion settings in the configuration’s expect.toHaveScreenshot section, globally or per project. For example, the official guide shows this illustrative pixel cap; it is not a universal recommendation:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
import { defineConfig } from '@playwright/test';

export default defineConfig({
  expect: {
    toHaveScreenshot: {
      maxDiffPixels: 100,
      // Add a threshold only when the observed rendering needs it.
    },
  },
});

See the Playwright configuration reference for configuration details. Avoid setting a generous global allowance merely to silence noisy tests; a per-test or per-project exception is easier to justify when only one capture needs it.

6. Review and update references safely

When an intentional design change alters screenshots, update the baseline with:

npx playwright test --update-snapshots

Review the changed images, then commit the updated references alongside the product change. Do not run this command automatically as the response to every failing test: a snapshot defines expected behavior, and accepting a failure without inspecting it can bless an accidental regression.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

7. Troubleshoot common failures

The test says a snapshot is missing

This is expected for a new assertion or new project snapshot. Review the generated image and add it to version control as the initial reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

The test fails repeatedly on a page that looks unchanged

Check that the reference and comparison use the same OS, browser build, fonts, viewport, project, and headless mode. Then inspect the diff for volatile data or an unstable UI state; mask only irrelevant regions or make the test data deterministic.

Only a small area fails because content changes every run

Identify whether that content is part of the visual contract. If it is not, use a narrowly scoped mask or stylePath; if it is, stabilize the underlying test data rather than hiding the region.

A full-page assertion is noisy but the component is stable

Switch to a locator screenshot assertion when the component, not the entire scrollable document, is what the test is intended to protect.

A tolerance change makes failures disappear

Check which control you changed. threshold changes the pixel-level sensitivity; maxDiffPixels and maxDiffPixelRatio permit a total amount of difference. Restore stricter settings if the allowance hides a real shift, and justify any remaining tolerance from reviewed diffs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Or skip the browser setup

If you need a screenshot endpoint rather than a committed Playwright visual baseline, ScreenshotNeo is a website screenshot API and MCP server. Its single-request API can return an image or PDF, but it does not replace Playwright’s baseline comparison workflow.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for setup and options. Before capture, it can accept cookie or consent banners and remove 60+ known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses include X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Sources and version note

Playwright’s assertion APIs and defaults can change between versions. Check the live visual comparisons guide, page assertion API, and configuration reference for the version used by your project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.