Categories
angularjs javascript protractor selenium

Switching to new window with Selenium/Protractor Javascript

Looking for some help on how I should be getting ahold of a new “pop-up” window that is triggered to display after I click a “login” button.

I am able to get to when the window is displaying but I do not believe that the code I am currently using to grab the window “handle” is working properly. My situation is a bit different in that I am using protractor inside my pages, but the new window comes up is NOT angular based, so I must switch over to using just selenium WebDriver while I am in that window. (Anyone have any idea if there could be issues with this approach?)

Below you can find the code snippet that I am using to create the selenium driver, as well as below that trying to “switch to / grab handle” of the new window that is popping up. I know that it is not working correctly because I keep receiving “No Such Element” errors in the code that follows trying to find a form on the page.

    // Create selenium webdriver / driver
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
// Now make sure that the new window is popping up and we are navigating correctly to it
var handlePromise = browser.driver.getAllWindowHandles();
handlePromise.then(function (handles) {
// parentHandle = handles[0];
var popUpHandle = handles[1];
// Change to new handle
browser.driver.switchTo().window(popUpHandle);
var popUpHandleFinal = browser.driver.getWindowHandle();
expect(popUpHandleFinal).toEqual(popUpHandle);
});

Couple things about this:

  1. If I remove the “browser” in the line “browser.driver.switchTo().window(popUpHandle)” so it reads as “driver.switchTo().window(popUpHandle)” I receive back and error that reads as” UnknownError: unknown error: ‘name’ must be a nonempty string” After doing some searching on this it is because the “switchTo()” method on driver cannot be null. This error is cleared up if I just use the code shown above.

  2. I am not 100% sure if I should be using protractor (global “browser” var) or using the straight “driver” (Selenium) that I set before this as the way to get the windows.

Thank you for your help