Skip to content

Commit

Permalink
have fallback for scrollIntoView if actions command fails (#9435)
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Dec 21, 2022
1 parent 61e8e67 commit 33a1473
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
11 changes: 11 additions & 0 deletions __mocks__/got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let manualMockResponse: any

const path = '/session'

const customResponses = new Set<{ pattern, response }>()
const defaultSessionId = 'foobar-123'
let sessionId = defaultSessionId
const genericElementId = 'some-elem-123'
Expand All @@ -28,6 +29,13 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
uri = new URL(uri)
}

for (const { pattern, response } of customResponses) {
if (!(uri as URL).pathname.match(pattern)) {
continue
}
return response
}

if (
params.json &&
params.json.capabilities &&
Expand Down Expand Up @@ -426,6 +434,9 @@ requestMock.retryCnt = 0
requestMock.setMockResponse = (value: any) => {
manualMockResponse = value
}
requestMock.customResponseFor = (pattern: RegExp, response: any) => {
customResponses.add({ pattern, response })
}

requestMock.getSessionId = () => sessionId
requestMock.setSessionId = (newSessionId: any) => {
Expand Down
46 changes: 34 additions & 12 deletions packages/webdriverio/src/commands/element/scrollIntoView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import logger from '@wdio/logger'

import { ELEMENT_KEY } from '../../constants.js'
import { getBrowserObject } from '../../utils/index.js'

const log = logger('webdriverio')

function scrollIntoViewWeb (
this: WebdriverIO.Element,
options: ScrollIntoViewOptions | boolean = { block: 'start', inline: 'nearest' }
) {
const browser = getBrowserObject(this)
return browser.execute(
(elem: HTMLElement, options: ScrollIntoViewOptions | boolean) => elem.scrollIntoView(options),
{
[ELEMENT_KEY]: this.elementId, // w3c compatible
ELEMENT: this.elementId, // jsonwp compatible
} as any as HTMLElement,
options,
)
}

/**
*
* Scroll element into viewport ([MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView)).
Expand Down Expand Up @@ -28,16 +47,11 @@ export default async function scrollIntoView (
) {
const browser = getBrowserObject(this)

// Appium does not support the "wheel" action
/**
* Appium does not support the "wheel" action
*/
if (browser.isMobile) {
return browser.execute(
(elem: HTMLElement, options: ScrollIntoViewOptions | boolean) => elem.scrollIntoView(options),
{
[ELEMENT_KEY]: this.elementId, // w3c compatible
ELEMENT: this.elementId, // jsonwp compatible
} as any as HTMLElement,
options,
)
return scrollIntoViewWeb.call(this, options)
}

let deltaX = 0
Expand All @@ -63,7 +77,15 @@ export default async function scrollIntoView (
}
}

return browser.action('wheel')
.scroll({ origin: this, duration: 200, deltaY, deltaX })
.perform()
try {
return await browser.action('wheel')
.scroll({ origin: this, duration: 200, deltaY, deltaX })
.perform()
} catch (err: any) {
log.warn(
`Failed to execute "scrollIntoView" using WebDriver Actions API: ${err.message}!\n` +
'Re-attempting using `Element.scrollIntoView` via Web API.'
)
return scrollIntoViewWeb.call(this, options)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ describe('scrollIntoView test', () => {
const optionsCenter = vi.mocked(got).mock.calls.slice(-2, -1)[0][1] as any
expect(optionsCenter.json).toMatchSnapshot()
})

it('falls back using Web API if scroll action fails', async () => {
// @ts-expect-error mock feature
got.customResponseFor(/\/actions/, { error: 'invalid parameter' })
// @ts-expect-error mock feature
elem.elementId = { scrollIntoView: vi.fn() }
await elem.scrollIntoView({})
console.log(vi.mocked(got).mock.calls.pop()![0]!.href.endsWith('/execute/sync'))
})
})

describe('mobile', () => {
Expand Down

0 comments on commit 33a1473

Please sign in to comment.