All files / wdio-selenium-standalone-service/src launcher.js

100% Statements 31/31
100% Branches 14/14
100% Functions 5/5
100% Lines 31/31

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                1x 1x       10x 10x 10x   10x       9x 9x 9x 9x 9x   9x 8x     9x   9x 4x     9x 3x 3x 3x           3x 2x         1x     1x   1x 1x 1x     10x 2x 1x 1x        
import logger from '@wdio/logger'
 
import { promisify } from 'util'
import fs from 'fs-extra'
import SeleniumStandalone from 'selenium-standalone'
 
import getFilePath from './utils/getFilePath'
 
const DEFAULT_LOG_FILENAME = 'selenium-standalone.txt'
const log = logger('@wdio/selenium-standalone-service')
 
export default class SeleniumStandaloneLauncher {
    constructor () {
        this.seleniumLogs = null
        this.seleniumArgs = {}
        this.seleniumInstallArgs = {}
 
        return this
    }
 
    async onPrepare (config) {
        this.seleniumArgs = config.seleniumArgs || {}
        this.seleniumInstallArgs = config.seleniumInstallArgs || {}
        this.seleniumLogs = config.seleniumLogs
        this.skipSeleniumInstall = !!config.skipSeleniumInstall
        this.watchMode = !!config.watch
 
        if (!this.skipSeleniumInstall) {
            await promisify(SeleniumStandalone.install)(this.seleniumInstallArgs)
        }
 
        this.process = await promisify(SeleniumStandalone.start)(this.seleniumArgs)
 
        if (typeof this.seleniumLogs === 'string') {
            this._redirectLogStream()
        }
 
        if (this.watchMode) {
            process.on('SIGINT', this._stopProcess)
            process.on('exit', this._stopProcess)
            process.on('uncaughtException', this._stopProcess)
        }
    }
 
    onComplete () {
        // selenium should not be killed in watch mode
        if (!this.watchMode) {
            this._stopProcess()
        }
    }
 
    _redirectLogStream () {
        const logFile = getFilePath(this.seleniumLogs, DEFAULT_LOG_FILENAME)
 
        // ensure file & directory exists
        fs.ensureFileSync(logFile)
 
        const logStream = fs.createWriteStream(logFile, { flags: 'w' })
        this.process.stdout.pipe(logStream)
        this.process.stderr.pipe(logStream)
    }
 
    _stopProcess = () => {
        if (this.process) {
            log.info('shutting down all browsers')
            this.process.kill()
        }
    }
}