All files / wdio-firefox-profile-service/src launcher.js

100% Statements 32/32
100% Branches 18/18
100% Functions 6/6
100% Lines 32/32

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          9x 9x   9x 1x     8x     8x   8x 6x       2x   2x             8x 11x 4x     7x     8x 1x     8x       8x   8x 6x 7x   6x     6x     2x 2x   2x 1x     1x         7x   1x     6x 6x        
import Profile from 'firefox-profile'
import { promisify } from 'util'
 
export default class FirefoxProfileLauncher {
    async onPrepare (config, capabilities) {
        this.config = config
        this.capabilities = capabilities
        // Return if no profile options were specified
        if (!this.config.firefoxProfile) {
            return
        }
 
        this.profile = new Profile()
 
        // Set preferences and proxy
        this._setPreferences()
 
        if (!Array.isArray(this.config.firefoxProfile.extensions)) {
            return this._buildExtension()
        }
 
        // Add the extension
        await promisify(::this.profile.addExtensions)(this.config.firefoxProfile.extensions)
 
        return this._buildExtension()
    }
 
    /**
     * Sets any preferences and proxy
     */
    _setPreferences () {
        for (const [preference, value] of Object.entries(this.config.firefoxProfile)) {
            if (['extensions', 'proxy', 'legacy'].includes(preference)) {
                continue
            }
 
            this.profile.setPreference(preference, value)
        }
 
        if (this.config.firefoxProfile.proxy) {
            this.profile.setProxy(this.config.firefoxProfile.proxy)
        }
 
        this.profile.updatePreferences()
    }
 
    async _buildExtension () {
        const zippedProfile = await promisify(::this.profile.encoded)()
 
        if (Array.isArray(this.capabilities)) {
            this.capabilities
                .filter((capability) => capability.browserName === 'firefox')
                .forEach((capability) => {
                    this._setProfile(capability, zippedProfile)
                })
 
            return
        }
 
        for (const browser in this.capabilities) {
            const capability = this.capabilities[browser].capabilities
 
            if (!capability || capability.browserName !== 'firefox') {
                continue
            }
 
            this._setProfile(capability, zippedProfile)
        }
    }
 
    _setProfile(capability, zippedProfile) {
        if(this.config.firefoxProfile.legacy) {
            // for older firefox and geckodriver versions
            capability.firefox_profile = zippedProfile
        } else {
            // for firefox >= 56.0 and geckodriver >= 0.19.0
            capability['moz:firefoxOptions'] = capability['moz:firefoxOptions'] || {}
            capability['moz:firefoxOptions'].profile = zippedProfile
        }
    }
}