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

100% Statements 33/33
100% Branches 16/16
100% Functions 13/13
100% Lines 33/33

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      1x       8x 2x     6x             6x   6x 4x 6x   2x 1x   1x       5x   5x 5x 1x   4x                   4x 4x   1x 1x         4x 1x     3x 1x       2x   2x 2x 1x   1x                   1x 1x   1x 1x        
import BrowserstackLocalLauncher from 'browserstack-local'
import logger from '@wdio/logger'
 
const log = logger('@wdio/browserstack-service')
 
export default class BrowserstackLauncherService {
    onPrepare(config, capabilities) {
        if (!config.browserstackLocal) {
            return log.info('browserstackLocal is not enabled - skipping...')
        }
 
        const opts = {
            key: config.key,
            forcelocal: true,
            onlyAutomate: true,
            ...config.browserstackOpts
        }
 
        this.browserstackLocal = new BrowserstackLocalLauncher.Local()
 
        if (Array.isArray(capabilities)) {
            capabilities.forEach(capability => {
                capability['browserstack.local'] = true
            })
        } else if (typeof capabilities === 'object') {
            capabilities['browserstack.local'] = true
        } else {
            throw TypeError('Capabilities should be an object or Array!')
        }
 
        let timer
        return Promise.race([
            new Promise((resolve, reject) => {
                this.browserstackLocal.start(opts, err => {
                    if (err) {
                        return reject(err)
                    }
                    resolve()
                })
            }),
            new Promise((resolve, reject) => {
                /* istanbul ignore next */
                timer = setTimeout(function () {
                    reject('Browserstack Local failed to start within 60 seconds!')
                }, 60000)
            })]
        ).then(function (result) {
            clearTimeout(timer)
            return Promise.resolve(result)
        }, function (err) {
            clearTimeout(timer)
            return Promise.reject(err)
        })
    }
 
    onComplete(exitCode, config) {
        if (!this.browserstackLocal || !this.browserstackLocal.isRunning()) {
            return
        }
 
        if (config.browserstackLocalForcedStop) {
            return process.kill(this.browserstackLocal.pid)
        }
 
        let timer
        return Promise.race([
            new Promise((resolve, reject) => {
                this.browserstackLocal.stop(err => {
                    if (err) {
                        return reject(err)
                    }
                    resolve()
                })
            }),
            new Promise((resolve, reject) => {
                /* istanbul ignore next */
                timer = setTimeout(function () {
                    reject('Browserstack Local failed to stop within 60 seconds!')
                }, 60000)
            })]
        ).then(function (result) {
            clearTimeout(timer)
            return Promise.resolve(result)
        }, function (err) {
            clearTimeout(timer)
            return Promise.reject(err)
        })
    }
}