All files / webdriverio/src/utils getElementObject.js

97.78% Statements 44/45
87.5% Branches 7/8
100% Functions 7/7
97.78% Lines 44/45

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                            69x 219x 219x   219x 219x   219x       198x         198x 154x   44x     21x     219x 219x 219x 219x     219x   219x 219x 4x 4x   219x                 69x 19x 19x   19x 57x 57x   57x       57x         57x 39x   18x           57x 57x 57x 57x 57x     57x   57x 57x 2x 2x   57x     19x    
import { webdriverMonad } from 'webdriver'
import { wrapCommand, runFnInFiberContext } from '@wdio/config'
import merge from 'lodash.merge'
 
import { getBrowserObject, getPrototype as getWDIOPrototype, getElementFromResponse } from '../utils'
import { elementErrorHandler } from '../middlewares'
import { ELEMENT_KEY } from '../constants'
 
/**
 * transforms and findElement response into a WDIO element
 * @param  {String} selector  selector that was used to query the element
 * @param  {Object} res       findElement response
 * @return {Object}           WDIO element object
 */
export const getElement = function findElement (selector, res) {
    const browser = getBrowserObject(this)
    const prototype = merge({}, browser.__propertiesObject__, getWDIOPrototype('element'), { scope: 'element' })
 
    const element = webdriverMonad(this.options, (client) => {
        const elementId = getElementFromResponse(res)
 
        if (elementId) {
            /**
             * set elementId for easy access
             */
            client.elementId = elementId
 
            /**
             * set element id with proper key so element can be passed into execute commands
             */
            if (this.isW3C) {
                client[ELEMENT_KEY] = elementId
            } else {
                client.ELEMENT = elementId
            }
        } else {
            client.error = res
        }
 
        client.selector = selector
        client.parent = this
        client.emit = ::this.emit
        return client
    }, prototype)
 
    const elementInstance = element(this.sessionId, elementErrorHandler(wrapCommand))
 
    const origAddCommand = ::elementInstance.addCommand
    elementInstance.addCommand = (name, fn) => {
        browser.__propertiesObject__[name] = { value: fn }
        origAddCommand(name, runFnInFiberContext(fn))
    }
    return elementInstance
}
 
/**
 * transforms and findElement response into a WDIO element
 * @param  {String} selector  selector that was used to query the element
 * @param  {Object} res       findElement response
 * @return {Object}           WDIO element object
 */
export const getElements = function getElements (selector, res) {
    const browser = getBrowserObject(this)
    const prototype = merge({}, browser.__propertiesObject__, getWDIOPrototype('element'), { scope: 'element' })
 
    const elements = res.map((res, i) => {
        const element = webdriverMonad(this.options, (client) => {
            const elementId = getElementFromResponse(res)
 
            Eif (elementId) {
                /**
                 * set elementId for easy access
                 */
                client.elementId = elementId
 
                /**
                 * set element id with proper key so element can be passed into execute commands
                 */
                if (this.isW3C) {
                    client[ELEMENT_KEY] = elementId
                } else {
                    client.ELEMENT = elementId
                }
            } else {
                client.error = res
            }
 
            client.selector = selector
            client.parent = this
            client.index = i
            client.emit = ::this.emit
            return client
        }, prototype)
 
        const elementInstance = element(this.sessionId, elementErrorHandler(wrapCommand))
 
        const origAddCommand = ::elementInstance.addCommand
        elementInstance.addCommand = (name, fn) => {
            browser.__propertiesObject__[name] = { value: fn }
            origAddCommand(name, runFnInFiberContext(fn))
        }
        return elementInstance
    })
 
    return elements
}