All files / webdriverio/src index.js

100% Statements 44/44
80% Branches 8/10
100% Functions 10/10
100% Lines 44/44

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 194x 194x 194x 1x     194x 194x     194x 1x     194x 1x     194x 194x           194x 194x 24x     194x 194x 9x     194x     69x 1x 1x     69x 12x 12x         12x   24x 24x 24x 24x   24x 24x 24x             12x 12x         12x           12x 12x 3x     12x 12x 3x     12x    
import path from 'path'
import WebDriver from 'webdriver'
import { validateConfig, wrapCommand, runFnInFiberContext, detectBackend } from '@wdio/config'
 
import MultiRemote from './multiremote'
import { WDIO_DEFAULTS } from './constants'
import { getPrototype } from './utils'
 
/**
 * A method to create a new session with WebdriverIO
 *
 * @param  {Object} [params={}]       Options to create the session with
 * @param  {function} remoteModifier  Modifier function to change the monad object
 * @return {object}                   browser object with sessionId
 */
export const remote = async function (params = {}, remoteModifier) {
    const config = validateConfig(WDIO_DEFAULTS, params)
    const modifier = (client, options) => {
        if (typeof remoteModifier === 'function') {
            client = remoteModifier(client, Object.assign(options, config))
        }
 
        Object.assign(options, config)
        return client
    }
 
    if (params.user && params.key) {
        params = Object.assign({}, detectBackend(params), params)
    }
 
    if(params.outputDir){
        process.env.WDIO_LOG_PATH = path.join(params.outputDir, 'wdio.log')
    }
 
    const prototype = getPrototype('browser')
    const instance = await WebDriver.newSession(params, modifier, prototype, wrapCommand)
 
    /**
     * we need to overwrite the original addCommand and overwriteCommand
     * in order to wrap the function within Fibers
     */
    const origAddCommand = ::instance.addCommand
    instance.addCommand = (name, fn, attachToElement) => (
        origAddCommand(name, runFnInFiberContext(fn), attachToElement)
    )
 
    const origOverwriteCommand = ::instance.overwriteCommand
    instance.overwriteCommand = (name, fn, attachToElement) => (
        origOverwriteCommand(name, runFnInFiberContext(fn), attachToElement)
    )
 
    return instance
}
 
export const attach = function (params) {
    const prototype = getPrototype('browser')
    return WebDriver.attachToSession(params, null, prototype, wrapCommand)
}
 
export const multiremote = async function (params = {}) {
    const multibrowser = new MultiRemote()
    const browserNames = Object.keys(params)
 
    /**
     * create all instance sessions
     */
    await Promise.all(
        browserNames.map((browserName) => {
            const config = validateConfig(WDIO_DEFAULTS, params[browserName])
            const modifier = (client, options) => {
                Object.assign(options, config)
                return client
            }
            const prototype = getPrototype('browser')
            const instance = WebDriver.newSession(params[browserName], modifier, prototype, wrapCommand)
            return multibrowser.addInstance(browserName, instance)
        })
    )
 
    /**
     * use attachToSession capability to wrap instances around blank pod
     */
    const prototype = getPrototype('browser')
    const sessionParams = {
        sessionId: '',
        isW3C: multibrowser.instances[browserNames[0]].isW3C,
        logLevel: multibrowser.instances[browserNames[0]].options.logLevel
    }
    const driver = WebDriver.attachToSession(sessionParams, ::multibrowser.modifier, prototype, wrapCommand)
 
    /**
     * in order to get custom command overwritten or added to multiremote instance
     * we need to pass in the prototype of the multibrowser
     */
    const origAddCommand = ::driver.addCommand
    driver.addCommand = (name, fn, attachToElement) => {
        origAddCommand(name, runFnInFiberContext(fn), attachToElement, Object.getPrototypeOf(multibrowser.baseInstance), multibrowser.instances)
    }
 
    const origOverwriteCommand = ::driver.overwriteCommand
    driver.overwriteCommand = (name, fn, attachToElement) => {
        origOverwriteCommand(name, runFnInFiberContext(fn), attachToElement, Object.getPrototypeOf(multibrowser.baseInstance), multibrowser.instances)
    }
 
    return driver
}