Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 823
Sumeet Avatar asked May 27 '15 13:05

Sumeet


2 Answers

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

like image 115
Get Avatar answered Oct 18 '22 08:10

Get


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.

like image 4
mrroot5 Avatar answered Oct 18 '22 08:10

mrroot5