Interacting (and closing) pop up windows

Here’s an example of how to interact (and close) a pop up window.

var _ = require('lodash');
var oldWindow, newWindow;
$browser.get("").then(function() {
    return $browser.switchTo().frame(0);
}).then(function() {
    return $browser.findElement({xpath: '/html/body/p/a'}).click();
}).then(function() {
    return $browser.getWindowHandle().then(function(handle) {
            oldWindow = handle;
            console.log('initial window is:' + oldWindow);
}).then(function() {
    return $browser.getAllWindowHandles().then(function(handles) {
        newWindow = _.difference(handles, [oldWindow])[0];
    console.log('new window is:' + newWindow);
  return $browser.switchTo().window(newWindow);


I’m having some trouble getting this sort of function to work with my scripts, and the example also isn’t working for me. Is there an updated version of this example that works?


@ritchie_buckland it looks like the example site has changed their page a little bit. Instead of using return $browser.switchTo().frame(0) try using return $browser.switchTo().frame(2). The difference is due to the Google ads that are now injected at the top of the page, which is causing the desired iframe to be the 3rd on the page, not the 1st.

Cheers, We were able to get this working and from there get it working with our tests.