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 desk5 min

How to Hide Dynamic Content in Playwright Screenshot Tests

Learn when to mask volatile elements, hide or normalize them with capture-only CSS, disable animations, and keep Playwright screenshot baselines consistent.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright’s mask option to cover a changing element, or use stylePath to hide or normalize it with capture-only CSS. Disable animations when motion causes inconsistent frames, and run visual tests in the same rendering environment as the baseline.

Choose the right way to handle changing content

First decide what the screenshot is meant to verify. If a timestamp’s text is irrelevant but you want the rest of the page checked, mask that element. If it should not appear in the image at all—or you need to normalize its appearance—apply CSS during capture. If the instability comes from motion, disable animations as well.

As an Amazon Associate I earn from qualifying purchases.

Method What appears in the screenshot Use it when
mask A solid overlay covers the matched element’s bounding box. You want to ignore a known region and an overlay is acceptable.
stylePath Capture-only CSS hides or alters the matched content. The content should disappear or be visually normalized.
animations: 'disabled' Playwright captures without ongoing animation effects. Motion or transitions create varying frames.

These options address different sources of variation. A stable screenshot may require more than one of them.

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

Mask a volatile element with a locator

For a changing timestamp, avatar, or other region that should not affect the visual comparison, pass a specific locator in the screenshot assertion’s mask array:

await expect(page).toHaveScreenshot('dashboard.png', {
  mask: [page.getByTestId('live-timestamp')],
});

Playwright covers the matched element’s bounding box. The default mask color is pink (#FF00FF); set maskColor if a different CSS color is more suitable. Masking also applies to matched elements that are invisible.

Use a stable locator, and keep the mask as narrow as possible. A broad mask can conceal a genuine layout or rendering regression in the region it covers.

Hide or normalize content with capture-only CSS

Use stylePath with toHaveScreenshot when an overlay is not wanted. It accepts a stylesheet path or an array of paths. For example, this stylesheet hides the timestamp while retaining its layout box:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option
[data-testid="live-timestamp"] {
  visibility: hidden !important;
}

Then apply it to the screenshot assertion:

await expect(page).toHaveScreenshot('dashboard.png', {
  stylePath: './screenshot.css',
});

You can instead use CSS to normalize a volatile appearance or value. Review the captured result: hiding or changing an element can obscure behavior that the test is supposed to verify, and the effect on layout depends on the page’s CSS.

Choose the option for the API you are using. Screenshot assertions use stylePath. Locator screenshot capture documents the corresponding style option, which accepts stylesheet text; do not substitute one option name for the other.

Disable animations when motion is the source

Screenshot assertions default animations to "disabled". You can make the intent explicit:

await expect(page).toHaveScreenshot('dashboard.png', {
  animations: 'disabled',
});

With this setting, finite animations are fast-forwarded to completion and fire transitionend. Infinite animations are canceled at their initial state for the capture and played over afterward.

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.

Standalone locator screenshot capture documents a different default: "allow". If you use that API and motion matters, set the option explicitly. Disabling animations does not stabilize unrelated live text or imagery; mask or style those regions separately.

Keep the baseline and test environment consistent

Masking and CSS do not eliminate differences caused by the rendering environment. Playwright’s visual comparison guidance notes that output may vary with the host operating system, browser version, browser settings, hardware, power source, and headless mode. Generate baselines and compare them in the same environment whenever possible, including the browser and relevant settings.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Screenshot assertions capture repeatedly until two consecutive screenshots match, then compare or save the result. That helps with instability during capture, but it cannot remove differences between machines or determine which changing content is safe to ignore. Keep the assertion focused on meaningful, stable regions and inspect diffs after adding masks or CSS.

Implementation sequence

  1. Identify the changing region. Decide whether the test should ignore it entirely or preserve its layout while hiding its contents.
  2. Choose a treatment. Use a locator mask if a colored box is acceptable; use stylePath in a screenshot assertion to hide or normalize content.
  3. Address motion separately. Disable animations if they cause variation, especially when using locator capture, whose documented default allows animations.
  4. Match the baseline environment. Keep the browser, host environment, headless mode, and relevant settings consistent between baseline generation and comparisons.
  5. Review the diff. Confirm that the mask or CSS has not hidden a meaningful regression or behavior under test.

Troubleshooting

The screenshot still changes after masking

Check whether the locator matches the entire volatile region and whether another changing element remains outside the mask. Use a specific locator, then inspect the diff to identify the uncovered area rather than expanding the mask indiscriminately.

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

The hidden element leaves an unexpected gap

visibility: hidden hides the content but preserves its layout box. If the intended screenshot should not reserve that space, adjust the capture-only CSS to alter layout, then verify that this does not conceal a layout change the test needs to catch.

An animation appears in a locator screenshot

Locator screenshot capture defaults to animations: 'allow'. Set animations: 'disabled' when you need the capture to avoid motion effects.

Screenshots differ across machines or CI runs

Compare the rendering environments: operating system, browser version, settings, hardware, power source, and headless mode can affect output. Run baseline creation and comparisons in the same environment where possible.

The screenshot assertion keeps waiting for a stable image

Assertions recapture until two consecutive screenshots match. If the page continues changing, identify whether the cause is live content, animation, or another unstable region, then apply the narrowest suitable mask or capture stylesheet and disable motion where needed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 a screenshot returned from an API rather than a Playwright visual assertion, ScreenshotNeo takes a screenshot from one GET request. This is a different workflow: it does not replace Playwright’s baseline comparison or its mask and CSS assertion options.

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 request options. Before capture it accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers report the page verdict and billing status. An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.

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.

Leave a Reply

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

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
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.