All files / wdio-sauce-service/src service.js

100% Statements 84/84
97.22% Branches 70/72
100% Functions 15/15
100% Lines 84/84

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      1x   1x   1x       28x 28x             27x 27x 27x 27x 27x             27x 11x 11x   27x 11x 11x     27x 27x 27x 27x       1x       5x 3x                 2x       2x   2x       2x 1x         2x 1x         5x 3x     2x 2x       4x                           3x         4x 2x     2x 2x             5x 1x     4x           4x 1x     4x   4x 3x 3x     1x 3x 3x         4x 1x     3x   3x 2x 2x     1x 3x 1x 1x       2x 1x 1x 1x     1x 1x 1x             9x         9x   9x 1x           9x 7x   7x 6x     7x     9x 45x 36x     9x     9x 9x      
import SauceLabs from 'saucelabs'
import logger from '@wdio/logger'
 
const jobDataProperties = ['name', 'tags', 'public', 'build', 'custom-data']
 
const jasmineTopLevelSuite = 'Jasmine__TopLevel__Suite'
 
const log = logger('@wdio/sauce-service')
 
export default class SauceService {
    constructor () {
        this.testCnt = 0
        this.failures = 0 // counts failures between reloads
    }
 
    /**
     * gather information about runner
     */
    beforeSession (config, capabilities) {
        this.config = config
        this.capabilities = capabilities
        this.api = new SauceLabs(this.config)
        this.isRDC = 'testobject_api_key' in this.capabilities
        this.isServiceEnabled = true
 
        /**
         * if no user and key is specified even though a sauce service was
         * provided set user and key with values so that the session request
         * will fail (not for RDC tho due to other auth mechansim)
         */
        if (!this.isRDC && !config.user) {
            this.isServiceEnabled = false
            config.user = 'unknown_user'
        }
        if (!this.isRDC && !config.key) {
            this.isServiceEnabled = false
            config.key = 'unknown_key'
        }
 
        this.config.user = config.user
        this.config.key = config.key
        this.sauceUser = this.config.user
        this.sauceKey = this.config.key
    }
 
    beforeSuite (suite) {
        this.suiteTitle = suite.title
    }
 
    beforeTest (test) {
        if (!this.isServiceEnabled || this.isRDC) {
            return
        }
 
        /**
         * in jasmine we get Jasmine__TopLevel__Suite as title since service using test
         * framework hooks in order to execute async functions.
         * This tweak allows us to set the real suite name for jasmine jobs.
         */
        /* istanbul ignore if */
        if (this.suiteTitle === 'Jasmine__TopLevel__Suite') {
            this.suiteTitle = test.fullName.slice(0, test.fullName.indexOf(test.title) - 1)
        }
 
        const context = test.parent === jasmineTopLevelSuite ? test.fullName : test.parent + ' - ' + test.title
 
        global.browser.execute('sauce:context=' + context)
    }
 
    afterSuite (suite) {
        if (Object.prototype.hasOwnProperty.call(suite, 'error')) {
            ++this.failures
        }
    }
 
    afterTest (test) {
        if (!test.passed) {
            ++this.failures
        }
    }
 
    beforeFeature (uri, feature) {
        if (!this.isServiceEnabled || this.isRDC) {
            return
        }
 
        this.suiteTitle = feature.name || feature.getName()
        global.browser.execute('sauce:context=Feature: ' + this.suiteTitle)
    }
 
    afterStep (uri, feature) {
        if (
            /**
             * Cucumber v1
             */
            feature.failureException ||
            /**
             * Cucumber v2
             */
            (typeof feature.getFailureException === 'function' && feature.getFailureException()) ||
            /**
             * Cucumber v3, v4
             */
            (feature.status === 'failed')
        ) {
            ++this.failures
        }
    }
 
    beforeScenario (uri, feature, scenario) {
        if (!this.isServiceEnabled || this.isRDC) {
            return
        }
 
        const scenarioName = scenario.name || scenario.getName()
        global.browser.execute('sauce:context=Scenario: ' + scenarioName)
    }
 
    /**
     * update Sauce Labs job
     */
    after (result) {
        if (!this.isServiceEnabled && !this.isRDC) {
            return
        }
 
        let failures = this.failures
 
        /**
         * set failures if user has bail option set in which case afterTest and
         * afterSuite aren't executed before after hook
         */
        if (global.browser.config.mochaOpts && global.browser.config.mochaOpts.bail && Boolean(result)) {
            failures = 1
        }
 
        const status = 'status: ' + (failures > 0 ? 'failing' : 'passing')
 
        if (!global.browser.isMultiremote) {
            log.info(`Update job with sessionId ${global.browser.sessionId}, ${status}`)
            return this.updateJob(global.browser.sessionId, failures)
        }
 
        return Promise.all(Object.keys(this.capabilities).map((browserName) => {
            log.info(`Update multiremote job for browser "${browserName}" and sessionId ${global.browser[browserName].sessionId}, ${status}`)
            return this.updateJob(global.browser[browserName].sessionId, failures, false, browserName)
        }))
    }
 
    onReload (oldSessionId, newSessionId) {
        if (!this.isServiceEnabled && !this.isRDC) {
            return
        }
 
        const status = 'status: ' + (this.failures > 0 ? 'failing' : 'passing')
 
        if (!global.browser.isMultiremote) {
            log.info(`Update (reloaded) job with sessionId ${oldSessionId}, ${status}`)
            return this.updateJob(oldSessionId, this.failures, true)
        }
 
        const browserName = global.browser.instances.filter(
            (browserName) => global.browser[browserName].sessionId === newSessionId)[0]
        log.info(`Update (reloaded) multiremote job for browser "${browserName}" and sessionId ${oldSessionId}, ${status}`)
        return this.updateJob(oldSessionId, this.failures, true, browserName)
    }
 
    async updateJob (sessionId, failures, calledOnReload = false, browserName) {
        if (this.isRDC) {
            await this.api.updateTest(sessionId, { passed: failures === 0 })
            this.failures = 0
            return
        }
 
        const body = this.getBody(failures, calledOnReload, browserName)
        await this.api.updateJob(this.sauceUser, sessionId, body)
        this.failures = 0
    }
 
    /**
     * VM message data
     */
    getBody (failures, calledOnReload = false, browserName) {
        let body = {}
 
        /**
         * set default values
         */
        body.name = this.suiteTitle
 
        if (browserName) {
            body.name = `${browserName}: ${body.name}`
        }
 
        /**
         * add reload count to title if reload is used
         */
        if (calledOnReload || this.testCnt) {
            let testCnt = ++this.testCnt
 
            if (global.browser.isMultiremote) {
                testCnt = Math.ceil(testCnt / global.browser.instances.length)
            }
 
            body.name += ` (${testCnt})`
        }
 
        for (let prop of jobDataProperties) {
            if (!this.capabilities[prop]) {
                continue
            }
 
            body[prop] = this.capabilities[prop]
        }
 
        body.passed = failures === 0
        return body
    }
}