Skip to main content
Visual assertions are the flakiest part of Playwright test suites. Stably augments the Playwright runner with AI-powered screenshot prompts that understand intent, tolerate dynamic content, and explain failures—no golden baseline required.
Why not vanilla Playwright? expect(page).toHaveScreenshot() excels for static pages with reliable baselines. For highly dynamic UIs, Stably’s prompt assertions cover intent-based validation that Playwright’s pixel diffing cannot reliably express.

Installation

Install the Stably Playwright test integration and import it in place of the Playwright test runner:
The API surface stays the same as Playwright, but you gain access to AI assertions.

aiAssert

Use prompt-based visual assertions to validate your UI without maintaining golden screenshots.

Basic Usage

How It Works

aiAssert performs intelligent visual validation in three steps:
  1. Stabilization: Waits for the page to stabilize and captures the screenshot.
  2. AI Analysis: Evaluates the screenshot against your prompt using Stably’s vision model.
  3. Assertion Outcome: If the prompt is satisfied, the assertion resolves. Otherwise it throws with a natural-language explanation describing what the AI found missing or inconsistent.
Each assertion performs one AI call that completes in a few seconds, and failures surface the AI reasoning in the thrown Playwright assertion error. aiAssert includes built-in caching—if the captured screenshot hasn’t changed since the last run, no AI call is made, saving time and cost.

Method Signature

Parameters:
  • prompt - Human-readable description of what should appear on screen
  • options - Configuration options
    • model - AI model to use (see Model Selection)
    • Plus all standard Playwright screenshot options (fullPage, animations, etc.)
Types:
The assertion resolves when the UI matches the intent described in the prompt. On failure it throws a Playwright assertion error with the AI-generated reasoning.

Common Use Cases

Validate Personalization
Check Conditional UI States
Verify Data Visualizations
These assertions stay resilient even as layout, colors, or supporting content evolve, as long as the core intent aligns with the prompt.

Model Selection

You can specify which AI model to use. If not specified, the backend default is used.

Advanced Options

All standard Playwright screenshot options are supported:
Common options:
  • model - AI model to use (see Model Selection)
  • fullPage - Capture the full scrollable page
  • animations - Set to 'disabled' to disable CSS animations
  • clip - Capture a specific rectangular area
  • timeout - Maximum time to wait for stabilization

Scoping with Locators

Combine with Playwright locators to scope assertions to specific components:

When to Use

  • Use aiAssert when:
    • The page can change materially between runs while maintaining semantic intent
    • You need to validate invariants (e.g., “shows revenue chart”) even when the UI layout or styling changes
    • You have dynamic content (personalized data, A/B tests, real-time updates)
    • Layout and styling evolve frequently and describing intent is easier than maintaining golden snapshots
  • Use expect(page).toHaveScreenshot() when:
    • You have a stable, non-dynamic screen
    • Minor rendering differences are acceptable (Stably’s auto-heal handles font rendering, anti-aliasing, and subtle visual variations)
    • Pixel-perfect matching is important and the page structure rarely changes
Stably offers auto-heal for toHaveScreenshot() that handles minor rendering differences like font anti-aliasing and subtle layout shifts. However, for pages where major structural changes are acceptable as long as semantic invariants remain valid, use aiAssert instead. Prompt-driven assertions tolerate variance, provide contextual reasoning, and continue to work even when UI components are re-arranged or re-styled.

toMatchAriaPrompt (Coming Soon)

Stably is expanding prompt assertions beyond screenshots to support semantic validation directly on accessibility trees.

Method Signature

This method will validate UI semantics and structure without requiring visual screenshots, enabling faster and more accessible test assertions.

Best Practices

  • Write intent-focused prompts: Mention critical UI elements (primary CTA, key metrics, legal copy) rather than pixel-perfect descriptions.
  • Stabilize before capture: Pair assertions with network and animation stabilization waits to reduce UI churn.
  • Scope assertions: Use Playwright locators to focus on components that matter.
  • Be specific: Provide enough detail in prompts for the AI to differentiate success states.

Troubleshooting

Prompt is too vague Refine the description with salient visual cues. The AI needs enough detail to differentiate success states.
Assertion feels slow Keep assertions scoped using locators, and watch for upcoming caching updates to accelerate reruns.
Unexpected failure reasoning Review the assertion error message—it often reveals missing UI elements or structural changes worth addressing.

References