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

You can use Playwright to capture screenshots in a Vitest browser test, but a screenshot capture is not the same thing as a visual-regression assertion. Configure Vitest Browser Mode with its Playwright provider, then use the browser page API to capture an image or Vitest’s screenshot settings to save artifacts. Playwright’s toHaveScreenshot() matcher is documented for Playwright Test, not as a native Vitest matcher.

Choose the screenshot workflow that matches your goal

Start by deciding what the image is for. Vitest and Playwright can produce screenshot files for inspection, but producing a file does not automatically compare it with an approved reference image.

Goal Approach What you get
Capture an image during browser automation Use Playwright’s page.screenshot() or a locator’s screenshot method. Image bytes or a file; the capture itself does not assert that the image matches a baseline.
Save diagnostic images for Vitest browser tests Use Vitest Browser Mode screenshot settings, or capture through the browser page API. Artifacts useful for diagnosing a failure or reviewing a page.
Compare screenshots with visual baselines using toHaveScreenshot() Use Playwright Test for the documented page and locator screenshot assertions. A visual assertion that compares a capture with an expectation.

Vitest Browser Mode integrates browser tests into Vitest. Playwright Test is a separate test runner. Choose based on whether you need Vitest integration or Playwright’s documented screenshot assertion API.

Set up Vitest Browser Mode with the Playwright provider

Use the configuration for your installed Vitest major version. Vitest’s provider packages and imports have changed across releases, including changes recorded in the Vitest 4 migration guide. The following illustrates the current configuration shape described by Vitest; check the documentation matching your installed version before copying it verbatim.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Philips 24 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 241V8LB
  • CRISP CLARITY: This 23.8″ Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
  • WORK SEAMLESSLY: This sleek monitor is virtually bezel-free on three sides, so the screen looks even bigger for the viewer. This minimalistic design also allows for seamless multi-monitor setups that enhance your workflow and boost productivity
  • A BETTER READING EXPERIENCE: For busy office workers, EasyRead mode provides a more paper-like experience for when viewing lengthy documents
import { defineConfig } from 'vitest/config'
import { playwright } from '@vitest/browser-playwright'

export default defineConfig({
  test: {
    browser: {
      enabled: true,
      provider: playwright(),
      instances: [
        { browser: 'chromium' },
      ],
    },
  },
})

This configuration selects the Playwright provider and a Chromium browser instance. It does not, by itself, establish a visual baseline or tell Vitest to compare every test’s page with a saved image. If your installed Vitest version uses a different provider or configuration shape, follow its version-specific browser setup instead.

Capture a screenshot from a Vitest browser test

In the current Vitest Browser Mode guide, the browser page API is imported from vitest/browser. A basic capture can be written to a file with the page’s screenshot method. Use a path appropriate to your project and ensure the destination directory exists or is created by your test setup.

import { page } from 'vitest/browser'
import { it, expect } from 'vitest'

it('captures the rendered page', async () => {
  await page.goto('/your-route')
  await expect(page.getByRole('heading', { name: 'Your page heading' })).toBeVisible()

  await page.screenshot({ path: 'artifacts/page.png', fullPage: true })
})

The route and heading are examples; replace them with a route and stable UI element in your application. Waiting for a meaningful element before capture helps avoid taking an image before the relevant page state appears. Whether the page API and assertion helpers are imported this way depends on the Vitest version in use.

Choose the capture area

  • Viewport: omit fullPage to capture the currently visible page area.
  • Full page: set fullPage: true when the artifact should include content beyond the viewport. Lazy-loaded content may need to be triggered before capture.
  • One element: capture a locator when the whole page is unnecessary, for example a component under test. Playwright’s locator screenshot API is designed for element-level capture.
  • Clipped region: use screenshot clipping when a specific rectangle is more useful than the page or a single locator.

Playwright’s screenshot options also include controls such as scale and masking. Use the capture scope and options deliberately: a viewport image, a full-page image, and an element image answer different debugging questions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Philips 22 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 221V8LB
  • CRISP CLARITY: This 22 inch class (21.5″ viewable) Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • 100HZ FAST REFRESH RATE: 100Hz brings your favorite movies and video games to life. Stream, binge, and play effortlessly
  • SMOOTH ACTION WITH ADAPTIVE-SYNC: Adaptive-Sync technology ensures fluid action sequences and rapid response time. Every frame will be rendered smoothly with crystal clarity and without stutter
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors

Save diagnostic screenshots with Vitest settings

Vitest documents Browser Mode settings for a screenshot directory and screenshots on failure. These are useful when you want test artifacts for investigation rather than an explicit expected-image comparison. Configure them using the syntax for your installed Vitest version.

import { defineConfig } from 'vitest/config'
import { playwright } from '@vitest/browser-playwright'

export default defineConfig({
  test: {
    browser: {
      enabled: true,
      provider: playwright(),
      instances: [{ browser: 'chromium' }],
    },
    browser: {
      screenshotDirectory: './artifacts/screenshots',
      screenshotFailures: true,
    },
  },
})

Do not paste this illustrative object unchanged if your version’s configuration schema differs. In particular, preserve a single browser configuration object in your actual config: the repeated key above separates the provider example from the screenshot-setting example for readability, and JavaScript object keys cannot safely be duplicated as a way to merge configuration. Combine the settings under the one test.browser object according to the installed version’s documented schema.

For instance, where the installed version supports these properties in the same browser configuration, the shape is conceptually:

test: {
  browser: {
    enabled: true,
    provider: playwright(),
    instances: [{ browser: 'chromium' }],
    screenshotDirectory: './artifacts/screenshots',
    screenshotFailures: true,
  },
}

A failure screenshot is evidence for diagnosis, not proof that a page matches a desired design. Treat those as separate jobs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Dell 24 Monitor - SE2426H - 23.8-inch FHD (1920x1080) 144Hz 1ms Display, in-Plane Switching (IPS) Technology, AMD FreeSync™, TÜV 3-Star 2X HDMI, Tilt
  • Clear visuals. Fluid motion: A 144Hz refresh rate and 1ms MPRT deliver smooth, tear‑free motion across work, gaming, and streaming for clearer, more fluid viewing.
  • Eye comfort: TÜV Rheinland 3‑star* certification reduces harmful blue light while preserving stunning color quality without compromise. *TÜV Rheinland 3-star eye comfort certification.
  • Wide viewing angle: Get consistent views across a wide 178° /178° viewing angle.
  • In-Plane Switching (IPS): See excellent color accuracy and consistency across wide viewing angles with In-plane Switching (IPS) technology.
  • Ultra-thin bezels: Maximize your viewing experience with thin bezels.

Understand the boundary between Vitest and Playwright Test

Playwright documents expect(page).toHaveScreenshot() and expect(locator).toHaveScreenshot() as Playwright Test-only screenshot assertions. Do not assume that importing Vitest’s expect makes those Playwright Test matchers available, or describe them as native Vitest assertions.

If your requirement is screenshot comparison with that exact API, put those tests in a Playwright Test suite. If the application’s main unit and integration tests use Vitest, you can keep them there and maintain a separate browser visual test suite. Another option is a visual comparison tool that explicitly supports Vitest, but treat its installation and assertion API as a separate dependency rather than implying it comes with Vitest.

Make captures more repeatable

Screenshot comparisons are sensitive to the state being captured. Fix the viewport and the route or component state across runs, and wait for the UI that matters to be ready. Mask regions that are genuinely dynamic when your visual-testing approach supports masking. Playwright exposes capture and assertion controls for matters such as scale, masks, animation handling, and style overrides; these can reduce irrelevant differences but cannot guarantee that every rendering variation disappears.

  • Use a stable browser instance and viewport for the comparisons you intend to make.
  • Wait for a meaningful selector or UI state, rather than relying only on an arbitrary delay.
  • Choose viewport, full-page, or locator capture to fit the intended check.
  • Mask only regions whose changing content is not part of what the test should verify.
  • Keep diagnostic artifacts distinct from checked-in or otherwise managed visual baselines.

Troubleshoot missing, blank, or surprising screenshots

The screenshot is blank or captures the wrong state

Check that navigation completed, the expected UI state was reached, and the selected capture scope includes the target. A screenshot taken before a route or component is ready can faithfully capture an empty or intermediate state. Wait for a relevant selector or state before capture.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Samsung 27" Essential S3 (S36GD) Series FHD 1800R Curved Computer Monitor
  • CURVED FOR ENHANCED ENGAGEMENT: An immersive viewing experience with a curved monitor that wraps more closely around your field of vision; It creates a wider view, enhancing depth perception and minimizing peripheral distraction
  • SMOOTH PERFORMANCE FOR SEAMLESS CONTENT: Stay in the action when playing games, watching videos, or working on creative projects; The 100Hz refresh rate reduces lag and motion blur so you don't miss a thing in fast-paced moments¹
  • MORE GAMING POWER: Gain the edge with optimizable game settings; Color and image contrast can be adjusted to see scenes more vividly and spot enemies hiding in the dark; Game Mode adjusts any game to fill the screen so you can view every detail²
  • KEEP IT EASY ON THE EYES: Care for your eyes and stay comfortable, even during long sessions; Advanced eye comfort technology certified by TÜV reduces eye strain by minimizing blue light and reducing irritating screen flicker²
  • INCREASED VERSATILITY: Connect to more; Plug devices straight into your monitor for increased flexibility, making your computing environment even more convenient

The test cannot find the browser page API or provider

Check the installed Vitest major version and its matching Browser Mode guide. Provider package names and imports are version-sensitive; the current guide’s vitest/browser import should not be assumed to match every earlier release.

toHaveScreenshot() is unavailable

That matcher is documented for Playwright Test. A Vitest test using Vitest’s assertion library should not rely on it as a native matcher. Use Playwright Test for that API or select a separately documented Vitest-compatible visual comparison tool.

The image differs between runs

Confirm the browser/provider, viewport, capture scope, and UI readiness are consistent. Inspect whether the difference comes from dynamic content or an unfinished state. Masking or style controls can help with known dynamic regions, but they do not eliminate every cause of variation.

The artifact is missing after a failed test

Verify that screenshot-on-failure is configured for the Vitest version in use and that the configured screenshot directory is where you expect. For a manual capture, verify the output path and directory. A screenshot setting for failure artifacts is not the same as an assertion that compares against a reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Sale
Sceptre New 22-Inch Gaming Monitor, FHD 1080p, Up to 144Hz, HDMI, DisplayPort, Built-in Speakers, Machine Black (E225W-FW144 Series, 2026)
  • 【INTEGRATED SPEAKERS】Whether you're at work or in the midst of an intense gaming session, our built-in speakers provide rich and seamless audio, all while keeping your desk clutter-free.
  • 【EASY ON THE EYES】 Protect your eyes and enhance your comfort with Blue-Light Shift technology. This feature reduces harmful blue light emissions from your screen, helping to alleviate eye strain during long hours of use and promoting healthier viewing habits.
  • 【WIDEN YOUR PERSPECTIVE】Our sleek minimal bezel design ensures undivided attention. The nearly bezel-free display seamlessly connects in a dual monitor arrangement, delivering an unobstructed view that lets you focus on more at once, completely distraction-free.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need an image or PDF of a public page rather than a browser test artifact, ScreenshotNeo provides a website screenshot API and MCP server. One GET request returns a PNG, JPEG, WebP, or PDF. For the exact options and response details, see the ScreenshotNeo API documentation.

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

Use your own API key and target URL. This service is not a replacement for assertions about your application’s state inside Vitest; it is an alternative for obtaining page captures without setting up browser automation. Cookie banners, popups, and chat widgets are removed before capture; bot checks, blank pages, and failed loads are never billed. An MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000.

Sign up free for ScreenshotNeo to get 1,000 screenshots a month with no card.

Cost and reliability considerations

For tests running in your own browser setup, the relevant cost and reliability work is operational: keep the browser configuration aligned with the Vitest version, save useful artifacts, and limit visual comparisons to stable states that matter. A capture API is a different workflow from a test runner: ScreenshotNeo bills only clean shots, with response headers indicating the page verdict and billing status, and cache hits cost nothing. The appropriate choice depends on whether you need an assertion against your application or a screenshot of a URL.

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

Frequently Asked Questions

Can I call Playwright’s page.screenshot() from Vitest?

Yes, in a Vitest Browser Mode setup using the Playwright provider, use the browser page API for capture; check the API/import form against your installed Vitest version.

Does a Vitest screenshot automatically become a visual regression test?

No. Capturing or saving an artifact is different from comparing it with an expected image.

Can I use toHaveScreenshot() in a Vitest test?

Playwright documents that matcher for Playwright Test, not as a native Vitest assertion.

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.

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