feat: add hasScreenshot() visual comparison assertions for Page and Locator#1950
Open
anilreddy wants to merge 2 commits into
Open
feat: add hasScreenshot() visual comparison assertions for Page and Locator#1950anilreddy wants to merge 2 commits into
anilreddy wants to merge 2 commits into
Conversation
…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.
feat: add hasScreenshot() visual comparison assertions for Page and Locator
anilreddy
commented
Jul 22, 2026
anilreddy
left a comment
Author
There was a problem hiding this comment.
can anyone please review this and approve the PR
Author
|
@microsoft-github-policy-service agree |
Author
✅ Done — All 5 driver platforms set up, full test suite greenWhat was set upDownloaded and assembled Node.js v24.16.0 binaries for all 5 platforms
All placed under Test run resultsTests run: 1687, Failures: 0, Errors: 0, Skipped: 26 BUILD SUCCESS — 1m 13s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the Java equivalent of upstream's
toHaveScreenshot()(microsoft/playwright-java#1040), which was previously only available in the JS@playwright/testrunner.Adds
PageAssertions.hasScreenshot()andLocatorAssertions.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/hasURL→hasScreenshot).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.expectScreenshotprotocol method (used internally by the JS driver'sPage._expectScreenshot()). This PR only adds the client-side wiring:Protocol.java: newPageExpectScreenshotOptions/PageExpectScreenshotResult/PageExpectScreenshotErrorDetailsDTOs mirroringchannels.d.ts.PageImpl.java: newexpectScreenshot()sending theexpectScreenshotmessage and decoding the actual/previous/diff buffers (base64) from either the success result or theServerErrorWithDetailserror payload.ScreenshotAssertionsOptions.java(new): shared options DTO used with the existingUtils#convertTypereflection helper.ScreenshotAssertionsHelper.java(new): a Java-specific adaptation of@playwright/test'sSnapshotHelpersnapshot lifecycle (baseline creation, comparison,.not()handling,-actual/-diffdebug artifacts on failure), since the Java bindings have no test-runner-managed snapshot directory:src/test/resources/__screenshots__/<TestClassSimpleName>/<name>by default, overridable via the-Dplaywright.snapshotDirsystem property.-Dplaywright.updateSnapshots=trueto (re-)generate baselines, analogous to--update-snapshots.PageAssertions.java/LocatorAssertions.java: newHasScreenshotOptionsclass andhasScreenshot(...)overloads (by name, by name segments, or without a name).PageAssertionsImpl.java/LocatorAssertionsImpl.java: implementations wiring intoScreenshotAssertionsHelper.Testing
TestScreenshotAssertionscovering: baseline creation on first run, matching on subsequent runs, failure on mismatch (with a clearAssertionFailedErrormessage and debug artifacts),.not()behavior when no baseline exists, and locator-scoped screenshots.TestPageAssertions(15),TestLocatorAssertions(112), andTestLocatorAssertions2(20) — all 147 pre-existing assertion tests continue to pass unmodified.Closes #1040 (if applicable upstream).