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

100% Statements 9/9
100% Branches 6/6
100% Functions 1/1
100% Lines 9/9

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                                                                            3x 1x           2x 1x     1x   1x 1x   1x 1x    
 
/**
 *
 * Open new window in browser. This command is the equivalent function to `window.open()`. This command does not
 * work in mobile environments.
 *
 * __Note:__ When calling this command you automatically switch to the new window.
 *
 * <example>
    :newWindowSync.js
    it('should open a new tab', () => {
        browser.url('http://google.com')
        console.log(browser.getTitle()) // outputs: "Google"
 
        browser.newWindow('https://webdriver.io', 'WebdriverIO window', 'width=420,height=230,resizable,scrollbars=yes,status=1')
        console.log(browser.getTitle()) // outputs: "WebdriverIO ยท Next-gen WebDriver test framework for Node.js"
 
        browser.closeWindow()
        console.log(browser.getTitle()) // outputs: "Google"
    });
 * </example>
 *
 * @param {String} url            website URL to open
 * @param {String=} windowName     name of the new window
 * @param {String=} windowFeatures features of opened window (e.g. size, position, scrollbars, etc.)
 * @return {String}                id of window handle of new tab
 *
 * @uses browser/execute, protocol/getWindowHandles, protocol/switchToWindow
 * @alias browser.newWindow
 * @type window
 */
 
import newWindowHelper from '../../scripts/newWindow'
 
export default async function newWindow (url, windowName = 'New Window', windowFeatures = '') {
    /*!
     * parameter check
     */
    if (typeof url !== 'string') {
        throw new Error('number or type of arguments don\'t agree with newWindow command')
    }
 
    /*!
     * mobile check
     */
    if (this.isMobile) {
        throw new Error('newWindow command is not supported on mobile platforms')
    }
 
    await this.execute(newWindowHelper, url, windowName, windowFeatures)
 
    const tabs = await this.getWindowHandles()
    const newTab = tabs.pop()
 
    await this.switchToWindow(newTab)
    return newTab
}