Skip to content

feat: add hasScreenshot() visual comparison assertions for Page and Locator#1

Merged
anilreddy merged 1 commit into
mainfrom
feature/has-screenshot-assertions
Jul 22, 2026
Merged

feat: add hasScreenshot() visual comparison assertions for Page and Locator#1
anilreddy merged 1 commit into
mainfrom
feature/has-screenshot-assertions

Conversation

@anilreddy

Copy link
Copy Markdown
Owner

Summary

Implements the Java equivalent of upstream's toHaveScreenshot() (microsoft/playwright-java#1040), which was previously only available in the JS @playwright/test runner.

Adds PageAssertions.hasScreenshot() and LocatorAssertions.hasScreenshot() with full option parity to the JS API (animations, caret, clip, fullPage, mask, maskColor, omitBackground, scale, maxDiffPixels, maxDiffPixelRatio, threshold, style, timeout), following the Java bindings' existing naming convention (hasTitle/hasURLhasScreenshot).

How it works

The actual pixel-level image comparison (pixelmatch / SSIM-CIE94) is not reimplemented in Java — it already exists server-side in the bundled Node.js driver via the Page.expectScreenshot protocol method (used internally by the JS driver's Page._expectScreenshot()). This PR only adds the client-side wiring:

  • Protocol.java: new PageExpectScreenshotOptions / PageExpectScreenshotResult / PageExpectScreenshotErrorDetails DTOs mirroring channels.d.ts.
  • PageImpl.java: new expectScreenshot() sending the expectScreenshot message and decoding the actual/previous/diff buffers (base64) from either the success result or the ServerErrorWithDetails error payload.
  • ScreenshotAssertionsOptions.java (new): shared options DTO used with the existing Utils#convertType reflection helper.
  • ScreenshotAssertionsHelper.java (new): a Java-specific adaptation of @playwright/test's SnapshotHelper snapshot lifecycle (baseline creation, comparison, .not() handling, -actual/-diff debug artifacts on failure), since the Java bindings have no test-runner-managed snapshot directory:
    • Baselines are stored under src/test/resources/__screenshots__/<TestClassSimpleName>/<name> by default, overridable via the -Dplaywright.snapshotDir system property.
    • Pass -Dplaywright.updateSnapshots=true to (re-)generate baselines, analogous to --update-snapshots.
  • PageAssertions.java / LocatorAssertions.java: new HasScreenshotOptions class and hasScreenshot(...) overloads (by name, by name segments, or without a name).
  • PageAssertionsImpl.java / LocatorAssertionsImpl.java: implementations wiring into ScreenshotAssertionsHelper.

Testing

  • Added TestScreenshotAssertions covering: baseline creation on first run, matching on subsequent runs, failure on mismatch (with a clear AssertionFailedError message and debug artifacts), .not() behavior when no baseline exists, and locator-scoped screenshots.
  • Verified against a real Chromium instance through the actual Node.js driver: all 4 new tests pass.
  • Verified no regressions: TestPageAssertions (15), TestLocatorAssertions (112), and TestLocatorAssertions2 (20) — all 147 pre-existing assertion tests continue to pass unmodified.

Closes microsoft#1040 (if applicable upstream).

…ocator

Implements the Java equivalent of upstream's toHaveScreenshot() (issue microsoft#1040),
which is otherwise only available in the JS @playwright/test runner.

- Adds PageAssertions.hasScreenshot()/LocatorAssertions.hasScreenshot() with
  full option parity (animations, caret, clip, fullPage, mask, maskColor,
  omitBackground, scale, maxDiffPixels, maxDiffPixelRatio, threshold, style,
  timeout), matching the JS API naming convention used elsewhere in the Java
  bindings (hasTitle/hasURL -> hasScreenshot).
- Wires the existing driver-side Page.expectScreenshot protocol method
  (already bundled in the Node.js driver) via new Protocol.java DTOs and
  PageImpl.expectScreenshot(), so the actual pixel comparison (pixelmatch/
  SSIM) is done by the driver exactly as in JS - no image-diff algorithm is
  reimplemented in Java.
- Adds ScreenshotAssertionsHelper, a Java-specific adaptation of the
  @playwright/test SnapshotHelper snapshot lifecycle (baseline creation,
  comparison, .not() handling, actual/diff debug artifacts), since the Java
  bindings have no test-runner-managed snapshot directory:
    * Baselines stored under src/test/resources/__screenshots__/<TestClass>/<name>
      by default, overridable via -Dplaywright.snapshotDir
    * -Dplaywright.updateSnapshots=true regenerates baselines
- Adds TestScreenshotAssertions covering baseline creation, matching,
  mismatch failure, .not() with a missing baseline, and locator screenshots.

All existing assertion tests (TestPageAssertions, TestLocatorAssertions,
TestLocatorAssertions2 - 147 tests) continue to pass unmodified.
@anilreddy
anilreddy merged commit 553e179 into main Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Visual comparisons port to java library

1 participant