All files / wdio-runner/src utils.js

100% Statements 54/54
100% Branches 22/22
100% Functions 17/17
100% Lines 50/50

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 164 165 166 167 168 169          3x   3x 3x           16x   16x 11x 11x   1x                     5x       10x   7x 7x                           11x 6x 6x           5x 4x 4x 4x     1x 1x 1x 1x 1x     1x 1x 1x     1x                   5x   5x 2x   2x 1x   2x     2x     5x                 16x 4x                                 6x 1x 1x 1x 1x   1x       6x                     2x     4x             2x   4x       2x    
import merge from 'deepmerge'
import logger from '@wdio/logger'
import { remote, multiremote, attach } from 'webdriverio'
import { DEFAULT_CONFIGS } from '@wdio/config'
 
const log = logger('@wdio/local-runner:utils')
 
const MERGE_OPTIONS = { clone: false }
const mochaAllHooks = ['"before all" hook', '"after all" hook']
 
/**
 * run before/after session hook
 */
export function runHook (hookName, config, caps, specs) {
    const catchFn = (e) => log.error(`Error in ${hookName}: ${e.stack}`)
 
    return config && Array.isArray(config[hookName]) ? Promise.all(config[hookName].map((hook) => {
        try {
            return hook(config, caps, specs)
        } catch (e) {
            return catchFn(e)
        }
    })).catch(catchFn) : undefined
}
 
/**
 * sanitizes wdio config from capability properties
 * @param  {Object} caps  desired session capabilities
 * @return {Object}       sanitized caps
 */
export function sanitizeCaps (caps) {
    return Object.keys(caps).filter(key => (
        /**
         * filter out all wdio config keys
         */
        !Object.keys(DEFAULT_CONFIGS).includes(key)
    )).reduce((obj, key) => {
        obj[key] = caps[key]
        return obj
    }, {})
}
 
/**
 * initialise browser instance depending whether remote or multiremote is requested
 * @param  {Object}  config        configuration of sessions
 * @param  {Object}  capabilities  desired session capabilities
 * @return {Promise}               resolves with browser object
 */
export async function initialiseInstance (config, capabilities, isMultiremote) {
    /**
     * check if config has sessionId and attach it to a running session if so
     */
    if (config.sessionId) {
        log.debug(`attach to session with id ${config.sessionId}`)
        return attach({
            ...config,
            capabilities: capabilities
        })
    }
 
    if (!isMultiremote) {
        log.debug('init remote session')
        config.capabilities = sanitizeCaps(capabilities)
        return remote(config)
    }
 
    const options = {}
    log.debug('init multiremote session')
    delete config.capabilities
    for (let browserName of Object.keys(capabilities)) {
        options[browserName] = merge(config, capabilities[browserName], MERGE_OPTIONS)
    }
 
    const browser = await multiremote(options)
    for (let browserName of Object.keys(capabilities)) {
        global[browserName] = browser[browserName]
    }
 
    return browser
}
 
/**
 * Filter logTypes based on filter
 * @param  {string[]} excludeDriverLogs logTypes filter
 * @param  {string[]} driverLogTypes    available driver log types
 * @return {string[]}                   logTypes
 */
export function filterLogTypes(excludeDriverLogs, driverLogTypes) {
    let logTypes = [...driverLogTypes]
 
    if (Array.isArray(excludeDriverLogs)) {
        log.debug('filtering logTypes', logTypes)
 
        if (excludeDriverLogs.length === 1 && excludeDriverLogs[0] === '*') { // exclude all logTypes
            logTypes = []
        } else {
            logTypes = logTypes.filter(x => !excludeDriverLogs.includes(x)) // exclude specific logTypes
        }
 
        log.debug('filtered logTypes', logTypes)
    }
 
    return logTypes
}
 
/**
 * Send event to WDIOCLInterface if test or before/after all hook failed
 * @param {string} e        event
 * @param {object} payload  payload
 */
export function sendFailureMessage(e, payload) {
    if (e === 'test:fail' || (e === 'hook:end' && payload.error && mochaAllHooks.some(hook => payload.title.startsWith(hook)))) {
        process.send({
            origin: 'reporter',
            name: 'printFailureMessage',
            content: payload
        })
    }
}
 
/**
 * Gets { sessionId, isW3C, protocol, hostname, port, path, queryParams } of every Multiremote instance
 * @param {object} browser browser
 * @param {boolean} isMultiremote isMultiremote
 * @return {object}
 */
export function getInstancesData(browser, isMultiremote) {
    let instances
 
    if (isMultiremote) {
        instances = {}
        browser.instances.forEach(i => {
            const { protocol, hostname, port, path, queryParams } = browser[i].options
            const { isW3C, sessionId } = browser[i]
 
            instances[i] = { sessionId, isW3C, protocol, hostname, port, path, queryParams }
        })
    }
 
    return instances
}
 
/**
 * Attach to Multiremote
 * @param {object} instances mutliremote instances object
 * @param {object} caps multiremote capabilities
 * @return {object}
 */
export async function attachToMultiremote(instances, caps) {
    // emulate multiremote browser object
    const browser = {
        instances: Object.keys(instances),
        deleteSession () {
            return Promise.all(Object.keys(instances).map(name => browser[name].deleteSession()))
        }
    }
 
    /**
     * attach to every multiremote instance
     */
    await Promise.all(
        Object.keys(instances).map(async name => {
            browser[name] = await initialiseInstance(instances[name], caps[name].capabilities, false)
        })
    )
 
    return browser
}