All files / wdio-appium-service/src launcher.js

98.21% Statements 55/56
100% Branches 32/32
90.91% Functions 10/11
100% Lines 55/55

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              1x 1x       13x 13x 13x       8x   8x     8x 1x     7x 7x       8x   8x 8x   6x 2x         15x 7x 7x         8x 8x     8x 12x 6x 6x         8x   8x 2x 2x 1x 1x   2x         2x     2x   2x 2x 2x 2x       2x 2x   1x       1x         10x 1x     9x 9x 7x   7x 1x     6x     6x 4x     9x       4x   4x      
import logger from '@wdio/logger'
import { spawn } from 'child_process'
import { createWriteStream, ensureFileSync } from 'fs-extra'
import paramCase from 'param-case'
import { promisify } from 'util'
import getFilePath from './utils/getFilePath'
 
const log = logger('@wdio/appium-service')
const DEFAULT_LOG_FILENAME = 'appium.txt'
 
export class AppiumLauncher {
    constructor() {
        this.logPath = null
        this.command = ''
        this.appiumArgs = []
    }
 
    async onPrepare(config) {
        const appiumConfig = config.appium || {}
 
        this.logPath = appiumConfig.logPath || config.outputDir
 
        // Set config command
        if (appiumConfig.command) {
            this.command = appiumConfig.command
        } else {
            // Windows expects node to be explicitely set as command and appium module path as it's first argument
            this.command = 'node'
            this.appiumArgs.push(this._getAppiumCommand())
        }
 
        // Append remaining arguments
        this.appiumArgs.push(...this._cliArgsFromKeyValue(appiumConfig.args || {}))
 
        const asyncStartAppium = promisify(this._startAppium)
        this.process = await asyncStartAppium(this.command, this.appiumArgs, this.waitStartTime)
 
        if (typeof this.logPath === 'string') {
            this._redirectLogStream(this.logPath)
        }
    }
 
    onComplete() {
        if(this.process) {
            log.debug(`Appium (pid: ${process.pid}) killed`)
            this.process.kill()
        }
    }
 
    _startAppium(command, args, waitStartTime, callback) {
        log.debug(`Will spawn Appium process: ${command} ${args.join(' ')}`)
        let process = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] })
        let error
 
        process.stdout.on('data', (data) => {
            if (data.includes('Appium REST http interface listener started')) {
                log.debug(`Appium started with ID: ${process.pid}`)
                callback(null, process)
            }
        })
 
        // only capture first error to print it in case Appium failed to start.
        process.stderr.once('data', err => { error = err })
 
        process.once('exit', (exitCode) => {
            let errorMessage = `Appium exited before timeout (exit code: ${exitCode})`
            if (exitCode == 2) {
                errorMessage += '\n' + (error || 'Check that you don\'t already have a running Appium service.')
                log.error(errorMessage)
            }
            callback(new Error(errorMessage), null)
        })
    }
 
    _redirectLogStream(logPath) {
        const logFile = getFilePath(logPath, DEFAULT_LOG_FILENAME)
 
        // ensure file & directory exists
        ensureFileSync(logFile)
 
        log.debug(`Appium logs written to: ${logFile}`)
        const logStream = createWriteStream(logFile, { flags: 'w' })
        this.process.stdout.pipe(logStream)
        this.process.stderr.pipe(logStream)
    }
 
    _getAppiumCommand(moduleName = 'appium') {
        try {
            return require.resolve(moduleName)
        } catch (err) {
            log.error('appium is not installed locally.\n' +
            'If you use globally installed appium please add\n' +
            "appium: { command: 'appium' }\n" +
            'to your wdio.conf.js!')
            throw err
        }
    }
 
    _cliArgsFromKeyValue(keyValueArgs) {
        if (Array.isArray(keyValueArgs)) {
            return keyValueArgs
        }
 
        const cliArgs = []
        for (let key in keyValueArgs) {
            const value = keyValueArgs[key]
            // If the value is false or null the argument is discarded
            if ((typeof value === 'boolean' && !value) || value === null) {
                continue
            }
 
            cliArgs.push(`--${paramCase(key)}`)
 
            // Only non-boolean and non-null values are added as option values
            if (typeof value !== 'boolean' && value !== null) {
                cliArgs.push(this._sanitizeCliOptionValue(value))
            }
        }
        return cliArgs
    }
 
    _sanitizeCliOptionValue(value) {
        const valueString = String(value)
        // Encapsulate the value string in single quotes if it contains a white space
        return /\s/.test(valueString) ? `'${valueString}'` : valueString
    }
}