All files / wdio-applitools-service/src index.js

100% Statements 30/30
100% Branches 20/20
100% Functions 8/8
100% Lines 29/29

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      1x   1x             12x             12x 12x 12x   12x 3x       9x 8x     9x 9x 9x             3x 1x     2x 2x 1x     1x         2x 1x     1x 1x       2x 1x     1x       2x 1x     1x      
import logger from '@wdio/logger'
import { Eyes, Target } from '@applitools/eyes-webdriverio'
 
const log = logger('@wdio/applitools-service')
 
const DEFAULT_VIEWPORT = {
    width: 1440,
    height: 900
}
 
export default class ApplitoolsService {
    constructor () {
        this.eyes = new Eyes()
    }
 
    /**
     * set API key in onPrepare hook and start test
     */
    beforeSession (config) {
        const key = config.applitoolsKey || process.env.APPLITOOLS_KEY
        const serverUrl = config.applitoolsServerUrl || process.env.APPLITOOLS_SERVER_URL
        const applitoolsConfig = config.applitools || {}
 
        if (!key) {
            throw new Error('Couldn\'t find an Applitools "applitoolsKey" in config nor "APPLITOOLS_KEY" in the environment')
        }
 
        // Optionally set a specific server url
        if (serverUrl) {
            this.eyes.setServerUrl(serverUrl)
        }
 
        this.isConfigured = true
        this.eyes.setApiKey(key)
        this.viewport = Object.assign(DEFAULT_VIEWPORT, applitoolsConfig.viewport)
    }
 
    /**
     * set custom commands
     */
    before () {
        if (!this.isConfigured) {
            return
        }
 
        global.browser.addCommand('takeSnapshot', (title) => {
            if (!title) {
                throw new Error('A title for the Applitools snapshot is missing')
            }
 
            return this.eyes.check(title, Target.window())
        })
    }
 
    beforeTest (test) {
        if (!this.isConfigured) {
            return
        }
 
        log.info(`Open eyes for ${test.parent} ${test.title}`)
        global.browser.call(() => this.eyes.open(global.browser, test.title, test.parent, this.viewport))
    }
 
    afterTest () {
        if (!this.isConfigured) {
            return
        }
 
        global.browser.call(::this.eyes.close)
    }
 
    after () {
        if (!this.isConfigured) {
            return
        }
 
        global.browser.call(::this.eyes.abortIfNotClosed)
    }
}