All files / webdriverio/src multiremote.js

100% Statements 33/33
100% Branches 4/4
100% Functions 9/9
100% Lines 32/32

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                          12x             24x 24x             11x 11x 11x   11x 968x           11x       11x 11x         11x 22x     11x                                     8x   8x       8x 16x     8x 8x 8x     8x             968x 968x 10x 20x           10x 5x 5x 1x 3x     4x                                                                                                        
import zip from 'lodash.zip'
import merge from 'lodash.merge'
import { webdriverMonad } from 'webdriver'
import { wrapCommand } from '@wdio/config'
 
import { multiremoteHandler } from './middlewares'
import { getPrototype } from './utils'
 
/**
 * Multiremote class
 */
export default class MultiRemote {
    constructor () {
        this.instances = {}
    }
 
    /**
     * add instance to multibrowser instance
     */
    async addInstance (browserName, client) {
        this.instances[browserName] = await client
        return this.instances[browserName]
    }
 
    /**
     * modifier for multibrowser instance
     */
    modifier (wrapperClient) {
        const propertiesObject = {}
        propertiesObject.commandList = { value: wrapperClient.commandList }
        propertiesObject.options = { value: wrapperClient.options }
 
        for (const commandName of wrapperClient.commandList) {
            propertiesObject[commandName] = {
                value: this.commandWrapper(commandName),
                configurable: true
            }
        }
 
        propertiesObject['__propertiesObject__'] = {
            value: propertiesObject
        }
 
        this.baseInstance = new MultiRemoteDriver(this.instances, propertiesObject)
        const client = Object.create(this.baseInstance, propertiesObject)
 
        /**
         * attach instances to wrapper client
         */
        for (const [identifier, instance] of Object.entries(this.instances)) {
            client[identifier] = instance
        }
 
        return client
    }
 
    /**
     * helper method to generate element objects from results, so that we can call, e.g.
     *
     * ```
     * const elem = $('#elem')
     * elem.getHTML()
     * ```
     *
     * or in case multiremote is used
     *
     * ```
     * const elems = $$('div')
     * elems[0].getHTML()
     * ```
     */
    static elementWrapper (instances, result, propertiesObject) {
        const prototype = merge({}, propertiesObject, getPrototype('element'), { scope: 'element' })
 
        const element = webdriverMonad({}, (client) => {
            /**
             * attach instances to wrapper client
             */
            for (const [i, identifier] of Object.entries(Object.keys(instances))) {
                client[identifier] = result[i]
            }
 
            client.instances = Object.keys(instances)
            delete client.sessionId
            return client
        }, prototype)
 
        return element(this.sessionId, multiremoteHandler(wrapCommand))
    }
 
    /**
     * handle commands for multiremote instances
     */
    commandWrapper (commandName) {
        const instances = this.instances
        return wrapCommand(commandName, async function (...args) {
            const result = await Promise.all(
                Object.entries(instances).map(([, instance]) => instance[commandName](...args))
            )
 
            /**
             * return element object to call commands directly
             */
            if (commandName === '$') {
                return MultiRemote.elementWrapper(instances, result, this.__propertiesObject__)
            } else if (commandName === '$$') {
                const zippedResult = zip(...result)
                return zippedResult.map((singleResult) => MultiRemote.elementWrapper(instances, singleResult, this.__propertiesObject__))
            }
 
            return result
        })
    }
}
 
/**
 * event listener class that propagates events to sub drivers
 */
/* istanbul ignore next */
class MultiRemoteDriver {
    constructor (instances, propertiesObject) {
        this.instances = Object.keys(instances)
        this.isMultiremote = true
        this.__propertiesObject__ = propertiesObject
    }
 
    on (...args) {
        this.instances.forEach((instanceName) => this[instanceName].on(...args))
    }
 
    once (...args) {
        this.instances.forEach((instanceName) => this[instanceName].once(...args))
    }
 
    emit (...args) {
        this.instances.forEach((instanceName) => this[instanceName].emit(...args))
    }
 
    eventNames (...args) {
        this.instances.forEach((instanceName) => this[instanceName].eventNames(...args))
    }
 
    getMaxListeners () {
        this.instances.forEach((instanceName) => this[instanceName].getMaxListeners())
    }
 
    listenerCount (...args) {
        this.instances.forEach((instanceName) => this[instanceName].listenerCount(...args))
    }
 
    listeners (...args) {
        this.instances.forEach((instanceName) => this[instanceName].listeners(...args))
    }
 
    removeListener (...args) {
        this.instances.forEach((instanceName) => this[instanceName].removeListener(...args))
    }
 
    removeAllListeners (...args) {
        this.instances.forEach((instanceName) => this[instanceName].removeAllListeners(...args))
    }
}