All files / wdio-cli/src interface.js

100% Statements 98/98
100% Branches 60/60
100% Functions 18/18
100% Lines 98/98

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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239            2x       67x             67x 67x 67x 67x 67x 67x 67x   67x 67x   67x 67x       68x 68x           68x           68x                 71x 71x 2x   71x 2x   71x       6x       4x       3x       3x       14x 14x 10x   14x 1x     14x       3x 3x 2x 2x   3x       13x 2x   11x             5x 5x             7x   7x 7x 7x 7x 4x     7x 2x 2x 5x 3x 3x 3x   2x 2x                 135x 135x             8x 1x 1x 1x     7x 1x 1x     6x 1x     5x 1x     4x 1x     3x 2x     3x 3x 1x               3x 1x     2x 2x       2x             1x 1x 1x 1x 1x         4x 4x 4x 4x 4x 4x             2x 1x     1x 1x      
import chalk from 'chalk'
import EventEmitter from 'events'
import logger from '@wdio/logger'
 
import { getRunnerName } from './utils'
 
const log = logger('@wdio/cli')
 
export default class WDIOCLInterface extends EventEmitter {
    constructor (config, specs, totalWorkerCnt, isWatchMode = false) {
        super()
 
        /**
         * Colors can be forcibly enabled/disabled with env variable `FORCE_COLOR`
         * `FORCE_COLOR=1` - forcibly enable colors
         * `FORCE_COLOR=0` - forcibly disable colors
         */
        this.hasAnsiSupport = !!chalk.supportsColor.hasBasic
        this.specs = specs
        this.config = config
        this.totalWorkerCnt = totalWorkerCnt
        this.isWatchMode = isWatchMode
        this.inDebugMode = false
        this.specFileRetries = config.specFileRetries || 0
 
        this.on('job:start', ::this.addJob)
        this.on('job:end', ::this.clearJob)
 
        this.setup()
        this.onStart()
    }
 
    setup () {
        this.jobs = new Map()
        this.start = new Date()
 
        /**
         * The relationship between totalWorkerCnt and these counters are as follows:
         * totalWorkerCnt - retries = finished = passed + failed
         */
        this.result = {
            finished: 0,
            passed: 0,
            retries: 0,
            failed: 0
        }
        this.messages = {
            /**
             * messages from worker reporters
             */
            reporter: {}
        }
    }
 
    onStart() {
        this.log(chalk.bold(`\nExecution of ${chalk.blue(this.totalWorkerCnt)} spec files started at`), this.start.toISOString())
        if (this.inDebugMode) {
            this.log(chalk.bgYellow.black('DEBUG mode enabled!'))
        }
        if (this.isWatchMode) {
            this.log(chalk.bgYellow.black('WATCH mode enabled!'))
        }
        this.log('')
    }
 
    onSpecRunning(cid) {
        this.onJobComplete(cid, this.jobs.get(cid), 0, chalk.bold.cyan('RUNNING'))
    }
 
    onSpecRetry(cid, job, retries) {
        this.onJobComplete(cid, job, retries, chalk.bold.yellow('RETRYING'))
    }
 
    onSpecPass(cid, job, retries) {
        this.onJobComplete(cid, job, retries, chalk.bold.green('PASSED'))
    }
 
    onSpecFailure(cid, job, retries) {
        this.onJobComplete(cid, job, retries, chalk.bold.red('FAILED'))
    }
 
    onJobComplete(cid, job, retries, message) {
        const details = [`[${cid}]`, message]
        if (job) {
            details.push('in', getRunnerName(job.caps), this.getFilenames(job.specs))
        }
        if (retries > 0) {
            details.push(`(${retries} retries)`)
        }
 
        return this.log(...details)
    }
 
    onTestError(payload) {
        let error = { type: 'Error', message: typeof payload.error === 'string' ? payload.error : 'Uknown error.' }
        if (payload.error) {
            error.type = payload.error.type || error.type
            error.message = payload.error.message || error.message
        }
        return this.log(`[${payload.cid}]`, `${chalk.red(error.type)} in "${payload.fullTitle}"\n${chalk.red(error.message)}`)
    }
 
    getFilenames(specs = []) {
        if (specs.length > 0) {
            return '- ' + specs.join(', ').replace(new RegExp(`${process.cwd()}`, 'g'), '')
        }
        return ''
    }
 
    /**
     * add job to interface
     */
    addJob({ cid, caps, specs }) {
        this.jobs.set(cid, { caps, specs })
        this.onSpecRunning(cid)
    }
 
    /**
     * clear job from interface
     */
    clearJob ({ cid, passed, retries }) {
        const job = this.jobs.get(cid)
 
        this.jobs.delete(cid)
        const retryAttempts = this.specFileRetries - retries
        const retry = !passed && retries > 0
        if (!retry) {
            this.result.finished++
        }
 
        if (passed) {
            this.result.passed++
            this.onSpecPass(cid, job, retryAttempts)
        } else if (retry) {
            this.totalWorkerCnt++
            this.result.retries++
            this.onSpecRetry(cid, job, retryAttempts)
        } else {
            this.result.failed++
            this.onSpecFailure(cid, job, retryAttempts)
        }
    }
 
    /**
     * for testing purposes call console log in a static method
     */
    log (...args) {
        // eslint-disable-next-line no-console
        console.log(...args)
        return args
    }
 
    /**
     * event handler that is triggered when runner sends up events
     */
    onMessage (event) {
        if (event.origin === 'debugger' && event.name === 'start') {
            this.log(chalk.yellow(event.params.introMessage))
            this.inDebugMode = true
            return this.inDebugMode
        }
 
        if (event.origin === 'debugger' && event.name === 'stop') {
            this.inDebugMode = false
            return this.inDebugMode
        }
 
        if (!event.origin) {
            return log.warn(`Can't identify message from worker: ${JSON.stringify(event)}, ignoring!`)
        }
 
        if (event.origin !== 'reporter') {
            return this.log(event.cid, event.origin, event.name, event.content)
        }
 
        if (event.name === 'printFailureMessage') {
            return this.onTestError(event.content)
        }
 
        if (!this.messages[event.origin][event.name]) {
            this.messages[event.origin][event.name] = []
        }
 
        this.messages[event.origin][event.name].push(event.content)
        if (this.isWatchMode) {
            this.printReporters()
        }
    }
 
    sigintTrigger () {
        /**
         * allow to exit repl mode via Ctrl+C
         */
        if (this.inDebugMode) {
            return false
        }
 
        const isRunning = this.jobs.size !== 0
        const shutdownMessage = isRunning
            ? 'Ending WebDriver sessions gracefully ...\n' +
            '(press ctrl+c again to hard kill the runner)'
            : 'Ended WebDriver sessions gracefully after a SIGINT signal was received!'
        return this.log('\n\n' + shutdownMessage)
    }
 
    printReporters () {
        /**
         * print reporter output
         */
        const reporter = this.messages.reporter
        this.messages.reporter = {}
        for (const [reporterName, messages] of Object.entries(reporter)) {
            this.log('\n', chalk.bold.magenta(`"${reporterName}" Reporter:`))
            this.log(messages.join(''))
        }
    }
 
    printSummary() {
        const totalJobs = this.totalWorkerCnt - this.result.retries
        const elapsed = (new Date(Date.now() - this.start)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0]
        const retries = this.result.retries ? chalk.yellow(this.result.retries, 'retries') + ', ' : ''
        const failed = this.result.failed ? chalk.red(this.result.failed, 'failed') + ', ' : ''
        const percentCompleted = totalJobs ? Math.round(this.result.finished / totalJobs * 100) : 0
        return this.log(
            '\nSpec Files:\t', chalk.green(this.result.passed, 'passed') + ', ' + retries + failed + totalJobs, 'total', `(${percentCompleted}% completed)`, 'in', elapsed,
            '\n'
        )
    }
 
    finalise () {
        if (this.isWatchMode) {
            return
        }
 
        this.printReporters()
        this.printSummary()
    }
}