All files / webdriverio/src/commands/browser reloadSession.js

100% Statements 11/11
85.71% Branches 6/7
100% Functions 2/2
100% Lines 10/10

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                                                  4x         4x   4x 4x                 4x 4x 4x   4x 4x     4x    
/**
 *
 * Creates a new Selenium session with your current capabilities. This is useful if you
 * test highly stateful application where you need to clean the browser session between
 * the tests in your spec file to avoid creating hundreds of single test files with WDIO.
 * Be careful though, this command affects your test time tremendously since spawning
 * new Selenium sessions is very time consuming especially when using cloud services.
 *
 * <example>
    :reloadSync.js
    it('should reload my session with current capabilities', () => {
        console.log(browser.sessionId) // outputs: e042b3f3cd5a479da4e171825e96e655
        browser.reloadSession()
        console.log(browser.sessionId) // outputs: 9a0d9bf9d4864160aa982c50cf18a573
    })
 * </example>
 *
 * @alias browser.reloadSession
 * @type utility
 *
 */
 
import WebDriverRequest from 'webdriver/build/request'
 
export default async function reloadSession () {
    const oldSessionId = this.sessionId
 
    /**
     * end current running session
     */
    await this.deleteSession()
 
    const { w3cCaps, jsonwpCaps } = this.options.requestedCapabilities
    const sessionRequest = new WebDriverRequest(
        'POST',
        '/session',
        {
            capabilities: w3cCaps, // W3C compliant
            desiredCapabilities: jsonwpCaps // JSONWP compliant
        }
    )
 
    const response = await sessionRequest.makeRequest(this.options)
    const newSessionId = response.sessionId || (response.value && response.value.sessionId)
    this.sessionId = newSessionId
 
    Eif (Array.isArray(this.options.onReload) && this.options.onReload.length) {
        await Promise.all(this.options.onReload.map((hook) => hook(oldSessionId, newSessionId)))
    }
 
    return newSessionId
}