Sumeet
Sumeet

Reputation: 71

How to control new window in nightwatchjs

Am using nightwatchjs to automate submission of a support form. Clicking a link using nightwatchjs opens the form page in a new window. How can I set focus on that window? Any help is greatly appreciated.

Upvotes: 6

Views: 12459

Answers (2)

Get
Get

Reputation: 61

Use the switchWindow command. https://nightwatchjs.org/api/switchWindow.html

Upvotes: 6

mrroot5
mrroot5

Reputation: 1971

I try to give you a more detailed explanation with examples of code. Currently I use chromedriver 2.43.1 (installed with npm), selenim standalone 3.141.5 and nigtwatch 0.9.21.

module.exports = {
    'Test new tab open': function (browser) {
        browser.url('http://localhost:8000')
            .click('#a-href-with-target-blank')
        // Switch to new tab
        browser.window_handles(function (result) {
            // 0 == current main window, 1 == new tab
            var handle = result.value[1];
            browser.switchWindow(handle);
        });
        // do something
        browser.closeWindow(); // Close tab
        // Switch to main window
        browser.window_handles(function (result) {
            // 0 == current main window, 1 == new tab
            var handle = result.value[0];
            browser.switchWindow(handle);
        });
    }
};

Hope it helps.

Upvotes: 4

Related Questions