Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/element-scroll-back-to-top.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@wdio/image-comparison-core": patch
---

fix: scroll back to position 0 after an element screenshot

With `autoElementScroll` enabled, `checkElement`, `saveElement` and `toMatchElementSnapshot` scroll the element into view and then scroll back to the old position. When the old position was 0 (the top of the page), the page was not scrolled back, because 0 was treated as "no position". A later `checkScreen` then captured a scrolled page. The position is now restored whenever it was read, also when it is 0.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ describe('takeElementScreenshot', () => {
expect(waitForSpy).toHaveBeenCalledWith(100)
})

it('should restore scroll to position 0 when the page was at the top', async () => {
const optionsWithScroll = { ...baseOptions, autoElementScroll: true }
executeMock.mockResolvedValueOnce(0) // previous scroll position at the top

await takeElementScreenshot(browserInstance, optionsWithScroll, true)

expect(executeMock).toHaveBeenCalledTimes(2)
expect(executeMock.mock.calls[1][1]).toBe(0)
})

it('should not restore scroll when autoElementScroll is enabled but no previous position', async () => {
const optionsWithScroll = { ...baseOptions, autoElementScroll: true }
executeMock.mockResolvedValueOnce(undefined) // no previous position
Expand Down Expand Up @@ -224,6 +234,16 @@ describe('takeElementScreenshot', () => {
)
})

it('should restore scroll to position 0 in viewport mode when the page was at the top', async () => {
const vpScrollOptions: ElementScreenshotDataOptions = { ...vpOptions, autoElementScroll: true }
executeMock.mockResolvedValueOnce(0) // previous scroll position at the top

await takeElementScreenshot(browserInstance, vpScrollOptions, true)

expect(executeMock).toHaveBeenCalledTimes(2)
expect(executeMock.mock.calls[1][1]).toBe(0)
})

it('should use origin: document for the default (no biDiOrigin set)', async () => {
const defaultOptions = { ...vpOptions, biDiOrigin: undefined }

Expand Down Expand Up @@ -304,6 +324,16 @@ describe('takeElementScreenshot', () => {
expect(waitForSpy).toHaveBeenCalledWith(100)
})

it('should scroll back to position 0 when the page was at the top', async () => {
const optionsWithScroll = { ...baseOptions, autoElementScroll: true }
executeMock.mockResolvedValueOnce(0) // previous scroll position at the top

await takeElementScreenshot(browserInstance, optionsWithScroll, false)

expect(executeMock).toHaveBeenCalledTimes(2)
expect(executeMock.mock.calls[1][1]).toBe(0)
})

it('should not scroll back when autoElementScroll is enabled but no current position', async () => {
const optionsWithScroll = { ...baseOptions, autoElementScroll: true }
executeMock.mockResolvedValueOnce(undefined) // no scroll position returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function takeBiDiElementScreenshot(
clip,
})

if (options.autoElementScroll && currentPosition) {
if (options.autoElementScroll && typeof currentPosition === 'number') {
await browserInstance.execute(scrollToPosition, currentPosition)
}

Expand Down Expand Up @@ -120,7 +120,7 @@ async function takeBiDiElementScreenshotFromViewport(
clip,
})

if (options.autoElementScroll && currentPosition) {
if (options.autoElementScroll && typeof currentPosition === 'number') {
await browserInstance.execute(scrollToPosition, currentPosition)
}

Expand Down Expand Up @@ -179,7 +179,7 @@ async function takeWebDriverElementScreenshot(
// When the screenshot has been taken and the element position has been determined,
// we can scroll back to the original position
// We don't need to wait for the scroll here because we don't take a screenshot after this
if (options.autoElementScroll && currentPosition) {
if (options.autoElementScroll && typeof currentPosition === 'number') {
await browserInstance.execute(scrollToPosition, currentPosition)
}

Expand Down