Switch to a window that opens by link

2,727 views
Skip to first unread message

Andrei Smeu

unread,
Apr 24, 2017, 3:03:07 AM4/24/17
to NightwatchJs
hello, 

i have a problem with a nightwatch script: I want to switch window from the start one to a new one that is open by clicking a link. I cannot give url for the new tab because it has unique link.
Example:
-open the url;
-clicks the link button;
-[needs the switch window command]
-waitforelementvisible.

thank you,
Andrei

Bill Kowal

unread,
Apr 24, 2017, 9:08:10 AM4/24/17
to NightwatchJs
Good morning Andrei,

You're looking for the switchWindow command: http://nightwatchjs.org/api/#switchWindow

Here is an example custom command from my code that I call after clicking a link to verify title on the new page and close it then switch back to the original page. You could just as easily close the original page and/or work with the new page in your test.

exports.command = function (browser, expectedTitle) {
browser.pause(200).perform(function (done) {
done();
});
browser.windowHandles(function (result) {
handle = result.value[1];
browser.switchWindow(handle);
});
browser.assert.title(expectedTitle);
browser.closeWindow();
browser.windowHandles(function (result) {
handle = result.value[0];
browser.switchWindow(handle);
});
}

Dan Caseley

unread,
Apr 25, 2017, 9:18:57 AM4/25/17
to NightwatchJs
I do something similar, only I stash value[0] the first time I call windowHandles so that I can switch back.
I recall that there's a reason it's done this way, but no code comments... Previous Me has stabbed me in the back, yet again!

Bill Kowal

unread,
Apr 25, 2017, 9:50:44 AM4/25/17
to NightwatchJs
Thanks Dan! I'm not sure why previous me thought it was a good idea to call windowHandles twice. I wasn't able to just save the value and use it outside of the context of the callback, how did you accomplish this?

For my purposes I realized I could just do everything inside of one windowHandles, but it would be nice to know how to stash it for the future. I just did this for now.

    browser.windowHandles(function (result) {
        browser.switchWindow(result.value[1]);
        browser.assert.title(expectedTitle);
        browser.closeWindow();
        browser.switchWindow(result.value[0]);
    });

Dan Caseley

unread,
Apr 27, 2017, 7:57:31 AM4/27/17
to NightwatchJs
Absolutely. I did the same thing, but with vars early on.

browser.windowHandles(function(result) {
 
var originalWindowHandle = result.value[0];
 
var popUpWindowHandle = result.value[1];
  browser
.switchWindow(popUpWindowHandle)
 
//Do things with your popup
  browser
.switchWindow(originalWindowHandle);
});

testingtools.co - Srinivas P

unread,
Jan 1, 2018, 12:07:37 AM1/1/18
to NightwatchJs
Thank you for the response, I have a small query here.. I see that you have given browser.pause(200).perform ... 

I was looking for a more robust solution, which can be used across different applications and to work for applications which have different load times for pages. 

i.e. if we can wait till the new page loads rather giving a hard coded value of 200 or any other value, it should be dynamic enough.. and then we can switch window, the problem is if we keep a certain amount time which is fixed, I sometimes get the number of windows in windowHandles as 1 instead of 2, but if we give more pause time, I am getting correct count which is 2. Any help on this is highly appreciated.

Hope I did not confuse.. please let me know if you need any more explanation.

Suresh Dubey

unread,
Jan 11, 2018, 5:24:02 AM1/11/18
to NightwatchJs
Hi All,

Below code will work in a happy scenario only. Let assume click does work as expected and the new tab is not opened. Then mentioned code will close your main window.

How can we manage such situation in Nightwatch?
Message has been deleted

Dan Caseley

unread,
Apr 23, 2020, 11:44:45 AM4/23/20
to nightw...@googlegroups.com
It's old code. Have you checked if it's valid in modern Nightwatch?

On Thu, 23 Apr 2020 at 15:00, Govind Narayan <govind.n...@gmail.com> wrote:
This is not working for nightwatch in macOS(chrome browser), not checked in window machine though :( 

 
On Monday, April 24, 2017 at 6:38:10 PM UTC+5:30, Bill Kowal wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "NightwatchJs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nightwatchjs/ZsxwaJdT7u0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nightwatchjs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nightwatchjs/57f05a0f-36dc-4616-883f-876c5e83f476%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages