Skip to content

fix: scroll back to position 0 after an element screenshot - #1271

Open
theluckystrike wants to merge 1 commit into
webdriverio:mainfrom
theluckystrike:fix/visual-testing-1229
Open

theluckystrike wants to merge 1 commit into
webdriverio:mainfrom
theluckystrike:fix/visual-testing-1229

Conversation

@theluckystrike

Copy link
Copy Markdown

Fixes #1229

checkElement(), saveElement() and toMatchElementSnapshot() with autoElementScroll (the default) scroll the element into view and then restore the old position. The restore in packages/image-comparison-core/src/methods/takeElementScreenshots.ts checked options.autoElementScroll && currentPosition, so a page at the top (scrollY 0) was never scrolled back. A later checkScreen() then captured a scrolled page.

The three restore guards (BiDi document origin, BiDi viewport origin, WebDriver classic) now check typeof currentPosition === 'number'. An undefined result still skips the restore. screenshots.ts restores scrollY without a truthiness check, so it stays unchanged. A patch changeset covers @wdio/image-comparison-core.

Tests I ran on macOS, Node 25.9.0, pnpm 11.28.2

$ npx vitest --run packages/image-comparison-core/src/methods/takeElementScreenshots.test.ts
main 187e0767e   Tests  3 failed | 25 passed (28)
this branch      Tests  28 passed (28)

$ pnpm run build && pnpm run test:lint && pnpm run test:types   all exit 0
$ pnpm run test:unit   1071 passed, 1 timeout in processDiffPixels.test.ts (6M pixel case, 5000 ms under load), passes when rerun alone

End to end with the reporter's long.html and spec, headless Chrome 155, @wdio/visual-service built from this workspace

main 187e0767e   E2E start 0: after checkElement 1206
                 E2E start 100: after checkElement 100
this branch      E2E start 0: after checkElement 0
                 E2E start 100: after checkElement 100

The cloud provider suites and test.local.chrome.v10 were not run.

With autoElementScroll enabled, takeElementScreenshot scrolls the element
into view and then restores the old scroll position. The restore in
packages/image-comparison-core/src/methods/takeElementScreenshots.ts was
guarded by `options.autoElementScroll && currentPosition`, so a start
position of 0 counted as missing and the page stayed scrolled. A later
checkScreen then captured a scrolled page (webdriverio#1229).

The guard is now `typeof currentPosition === 'number'` in all three paths
(BiDi document origin, BiDi viewport origin, WebDriver classic). An
undefined result still skips the restore. The scrollToPosition calls in
screenshots.ts have no truthiness guard, so they stay as they are.

One new unit test per path starts at position 0 and expects
scrollToPosition with 0. A patch changeset covers
@wdio/image-comparison-core.

Tests I ran on macOS with Node 25.9.0 and pnpm 11.28.2
```
$ npx vitest --run packages/image-comparison-core/src/methods/takeElementScreenshots.test.ts
base source 187e076   Tests  3 failed | 25 passed (28)
this commit             Tests  28 passed (28)

$ pnpm run build        exit 0
$ pnpm run test:lint    exit 0
$ pnpm run test:types   exit 0
$ pnpm run test:unit    Tests  1 failed | 1071 passed (1072)
  the failure is processDiffPixels.test.ts "should handle maximum diff
  pixels threshold" (5000 ms timeout under load), unrelated; rerun alone
  Tests  10 passed (10)
```

End to end with the reporter's long.html and spec, headless Chrome 155
and @wdio/visual-service from this workspace (webdriverio 10.0.0)
```
base 187e076   E2E start 0: after checkElement 1206
                 E2E start 100: after checkElement 100
this commit      E2E start 0: after checkElement 0
                 E2E start 100: after checkElement 100
```

Not run are the cloud provider suites and test.local.chrome.v10.
@changeset-bot

changeset-bot Bot commented Oct 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 45f1d88

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@wdio/image-comparison-core Patch
@wdio/visual-service Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@greptile-apps

greptile-apps Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Fixes scroll restoration logic in element screenshot capture.

This PR appears safe to merge.

What we checked:

  • Scroll restoration stays optional: Each path reads the position only when autoElementScroll is enabled. The new guards require both that option and a numeric position.

Summary

Restores the page’s previous scroll position after an element screenshot, including position zero.

  • Updates the guards for BiDi document, BiDi viewport, and WebDriver Classic screenshots.
  • Adds a zero-position regression test for each path.
  • Adds a patch changeset for @wdio/image-comparison-core.

Reviews (1) · Last reviewed commit: "fix: scroll back to position 0 after an ..." · Reviewed by Greptile

This branch has not been deployed

No deployments
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.

bug: checkElement() does not scroll back when the page was at the top (scroll position 0)

1 participant