All files / webdriver/src index.js

100% Statements 32/32
91.67% Branches 22/24
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                                  4x   4x 3x                 4x                   4x                 4x           4x         4x   4x 4x 4x 4x 4x 4x             8x 1x       7x 1x     7x 7x   7x 7x 7x 7x 7x       1x     1x             1x     1x     1x     1x     1x                      
import logger from '@wdio/logger'
import merge from 'lodash.merge'
import { validateConfig } from '@wdio/config'
 
import webdriverMonad from './monad'
import WebDriverRequest from './request'
import { DEFAULTS } from './constants'
import { getPrototype, environmentDetector, getEnvironmentVars } from './utils'
 
import WebDriverProtocol from '../protocol/webdriver.json'
import JsonWProtocol from '../protocol/jsonwp.json'
import MJsonWProtocol from '../protocol/mjsonwp.json'
import AppiumProtocol from '../protocol/appium.json'
import ChromiumProtocol from '../protocol/chromium.json'
 
export default class WebDriver {
    static async newSession (options = {}, modifier, userPrototype = {}, commandWrapper) {
        const params = validateConfig(DEFAULTS, options)
 
        if (!options.logLevels || !options.logLevels['webdriver']) {
            logger.setLevel('webdriver', params.logLevel)
        }
 
        /**
         * the user could have passed in either w3c style or jsonwp style caps
         * and we want to pass both styles to the server, which means we need
         * to check what style the user sent in so we know how to construct the
         * object for the other style
         */
        const [w3cCaps, jsonwpCaps] = params.capabilities && params.capabilities.alwaysMatch
            /**
             * in case W3C compliant capabilities are provided
             */
            ? [params.capabilities, params.capabilities.alwaysMatch]
            /**
             * otherwise assume they passed in jsonwp-style caps (flat object)
             */
            : [{ alwaysMatch: params.capabilities, firstMatch: [{}] }, params.capabilities]
 
        const sessionRequest = new WebDriverRequest(
            'POST',
            '/session',
            {
                capabilities: w3cCaps, // W3C compliant
                desiredCapabilities: jsonwpCaps // JSONWP compliant
            }
        )
 
        const response = await sessionRequest.makeRequest(params)
 
        /**
         * save original set of capabilities to allow to request the same session again
         * (e.g. for reloadSession command in WebdriverIO)
         */
        params.requestedCapabilities = { w3cCaps, jsonwpCaps }
 
        /**
         * save actual receveived session details
         */
        params.capabilities = response.value.capabilities || response.value
 
        const environment = environmentDetector(params)
        const environmentPrototype = getEnvironmentVars(environment)
        const protocolCommands = getPrototype(environment)
        const prototype = merge(protocolCommands, environmentPrototype, userPrototype)
        const monad = webdriverMonad(params, modifier, prototype)
        return monad(response.value.sessionId || response.sessionId, commandWrapper)
    }
 
    /**
     * allows user to attach to existing sessions
     */
    static attachToSession (options = {}, modifier, userPrototype = {}, commandWrapper) {
        if (typeof options.sessionId !== 'string') {
            throw new Error('sessionId is required to attach to existing session')
        }
 
        // logLevel can be undefined in watch mode when SIGINT is called
        if (options.logLevel !== undefined) {
            logger.setLevel('webdriver', options.logLevel)
        }
 
        options.capabilities = options.capabilities || {}
        options.isW3C = options.isW3C === false ? false : true
 
        const environmentPrototype = getEnvironmentVars(options)
        const protocolCommands = getPrototype(options)
        const prototype = merge(protocolCommands, environmentPrototype, userPrototype)
        const monad = webdriverMonad(options, modifier, prototype)
        return monad(options.sessionId, commandWrapper)
    }
 
    static get WebDriver () {
        return WebDriver
    }
    static get DEFAULTS () {
        return DEFAULTS
    }
 
    /**
     * Protocols
     */
    static get WebDriverProtocol () {
        return WebDriverProtocol
    }
    static get JsonWProtocol () {
        return JsonWProtocol
    }
    static get MJsonWProtocol () {
        return MJsonWProtocol
    }
    static get AppiumProtocol () {
        return AppiumProtocol
    }
    static get ChromiumProtocol () {
        return ChromiumProtocol
    }
}
 
/**
 * Helper methods consumed by webdriverio package
 */
export {
    webdriverMonad,
    getPrototype
}