feat: add hasScreenshot() visual comparison assertions for Page and Locator - #1950
feat: add hasScreenshot() visual comparison assertions for Page and Locator#1950anilreddy 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
left a comment
There was a problem hiding this comment.
can anyone please review this and approve the PR
|
@microsoft-github-policy-service agree |
✅ 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 |
|
@yury-s is there any changes need to done in this PR can you please check and let me know if it can be merged? |
|
@yury-s I understand the submission policy but let me know where is the low quality code will try to resolve it or help me add this PR your way. It was not resolved from many years so I tried to create a PR if it is not good please assign it to someone so that they can guide |
|
@yury-s also one more thing please let me know if you want to assign it to someone or me and guide me the standards so I will try to create a new PR which is better and rectify it |
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).