Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Test Hover States with Playwright Screenshots

Hover a resilient Playwright locator, then compare the page or element screenshot with a reviewed visual baseline.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright’s locator-based hover() action to put a control into its hover state, then assert its rendering with toHaveScreenshot(). Choose a page screenshot when surrounding layout matters, or a locator screenshot when the element alone is the visual contract.

Test a hover state with Playwright Test

This TypeScript example hovers a navigation link and compares the page against a screenshot baseline:

As an Amazon Associate I earn from qualifying purchases.

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

test('navigation link has the expected hover appearance', async ({ page }) => {
  await page.goto('/');

  const link = page.getByRole('link', { name: 'Products' });
  await link.hover();

  await expect(page).toHaveScreenshot('products-link-hover.png');
});

Replace the URL, role, and accessible name with those for your page. Prefer a role and accessible name when they identify the control clearly; if your project defines a stable test ID as its testing contract, use that instead. Playwright recommends user-facing attributes and explicit contracts over brittle CSS or XPath chains where practical. Playwright locator guidance

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

Choose page or locator screenshot assertions

Assertion Best when Trade-off
await expect(page).toHaveScreenshot() The hover may change surrounding layout, overlays, or other parts of the viewport. Captures a wider visual contract, so unrelated page rendering can affect the comparison.
await expect(locator).toHaveScreenshot() The target element alone is the intended visual contract. Focused on that element, so it will not catch visual changes elsewhere on the page.

For example, after hovering the link, a focused assertion is:

const link = page.getByRole('link', { name: 'Products' });
await link.hover();
await expect(link).toHaveScreenshot();

Playwright documents screenshot assertions for both pages and locators. Page screenshot assertions are part of the Playwright Test runner. Playwright visual comparisons

Build and maintain a stable hover baseline

  1. Select the intended control. Use a role and accessible name or a project-owned test ID when available; avoid selectors tied to incidental DOM nesting.
  2. Trigger the interaction. Call locator.hover() and await it before taking the screenshot. It performs actionability checks unless force is enabled. The older page.hover() API is discouraged in favor of locator-based hover. Locator hover API
  3. Assert the rendering. Use a page or locator toHaveScreenshot() assertion, depending on the scope of the visual contract. For page screenshots, Playwright waits for two consecutive screenshots to match before comparing with the expectation. Screenshot assertion behavior
  4. Generate and review the reference. The first visual-comparison run creates the expected screenshot; subsequent runs compare against it. Inspect the generated baseline to confirm it represents the intended hover state before committing it. Baseline workflow
  5. Keep the environment consistent. Rendering can differ with operating system, browser version, settings, hardware, power source, and headless mode. Generate and check baselines in a consistent setup, ideally the same environment. Screenshot comparison guidance
  6. Choose animation handling intentionally. Screenshot assertions default to animations: 'disabled'. Playwright stops CSS animations, transitions, and Web Animations for capture: finite animations are fast-forwarded to completion, while infinite animations are canceled to their initial state and played again after capture. Set animations: 'allow' when the animation itself is what you need to test. Screenshot assertion options

Troubleshoot hover screenshot failures

  • The screenshot shows the normal state: verify that the locator targets the control whose appearance should change, and await hover() before the assertion. Hover performs actionability checks by default.
  • The locator stops matching after markup changes: replace brittle nesting-based CSS or XPath with a suitable role/name locator or the project’s explicit test ID contract.
  • The screenshot differs across machines: align the browser, operating system, headless mode, and other environment settings with those used to create the baseline.
  • The image captures an unintended transition frame: decide whether the test should suppress animation with the default animations: 'disabled' or capture it using animations: 'allow'.
  • The test uses page.hover(): migrate to locator.hover(), which is the documented preferred approach.

Or skip the browser setup

If you need a screenshot of a URL rather than an automated hover interaction, ScreenshotNeo provides a website screenshot API and MCP server. A single GET request can return a PNG, JPEG, WebP, or PDF. For example, this cURL request saves a WebP screenshot; see the ScreenshotNeo API documentation for options and setup:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
  • Cookie and consent banners, newsletter popups, and chat widgets are removed before the shot; each cleanup step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing; responses indicate the page verdict and billing status in headers.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.
  • 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.

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

Frequently Asked Questions

Does a screenshot assertion trigger hover by itself?

No. Hover the locator first with await locator.hover(), then call the screenshot assertion.

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

Can Playwright test the hover animation itself?

Yes. Screenshot assertions disable animations by default; set animations: 'allow' when the animated transition is part of the expected result.

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. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

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.