All files / webdriverio/src/utils refetchElement.js

100% Statements 10/10
100% Branches 4/4
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                    8x     8x 15x 15x   8x   8x     8x 15x 15x         15x      
import implicitWait from './implicitWait'
 
/**
 * helper utility to refetch an element and all its parent elements when running
 * into stale element exception errors
 * @param  {Object}  currentElement  element to refetch
 * @param  {string}  commandName  name of the command that called this
 * @return {Promise} resolves with element after all its parent were refetched
 */
export default async function refetchElement (currentElement, commandName) {
    let selectors = []
 
    //Crawl back to the browser object, and cache all selectors
    while(currentElement.elementId && currentElement.parent) {
        selectors.push(currentElement.selector)
        currentElement = currentElement.parent
    }
    selectors.reverse()
 
    const length = selectors.length
 
    // Beginning with the browser object, rechain
    return selectors.reduce(async (elementPromise, selector, index) => {
        const resolvedElement = await elementPromise
        let nextElement = await resolvedElement.$(selector)
        /**
         *  For error purposes, changing command name to '$' if we aren't
         *  on the last element of the array
         */
        return await implicitWait(nextElement, index + 1 < length ? '$' : commandName)
    }, Promise.resolve(currentElement))
}