diff --git a/src/util/refetchElements.ts b/src/util/refetchElements.ts index 2ee14a228..403c7539c 100644 --- a/src/util/refetchElements.ts +++ b/src/util/refetchElements.ts @@ -12,8 +12,9 @@ export const refetchElements = async ( full = false ): Promise => { if (elements && wait > 0 && (elements.length === 0 || full) && isElementArray(elements) && elements.parent && elements.foundWith && elements.foundWith in elements.parent) { - const fetchFunction = elements.parent[elements.foundWith as keyof typeof elements.parent] as Function - elements = await fetchFunction(elements.selector, ...elements.props) + const browser = elements.parent + const $$ = browser[elements.foundWith as keyof typeof browser] as Function + elements = await $$.call(browser, elements.selector, ...elements.props) } return elements } diff --git a/test/util/refetchElements.test.ts b/test/util/refetchElements.test.ts index 0f50e682b..39543a0e8 100644 --- a/test/util/refetchElements.test.ts +++ b/test/util/refetchElements.test.ts @@ -51,6 +51,20 @@ describe('refetchElements', () => { const actual = await refetchElements(elements, 0, true) expect(actual).toEqual(elements) }) + + test('should call $$ with all props', async () => { + elements.props = ['prop1', 'prop2'] + await refetchElements(elements, 5, true) + expect(elements.parent.$$).toHaveBeenCalledWith('parent', 'prop1', 'prop2') + }) + + test('should call $$ with the proper parent this context', async () => { + const parentFoundWith = vi.mocked(elements.parent.$$) + + await refetchElements(elements, 5, true) + + expect(parentFoundWith.mock.contexts[0]).toBe(elements.parent) + }) }) describe('given WebdriverIO.Element[] type', () => {