'Focusing on angular4 popup window with Protractor

I'm unable to get my UI test to focus on a popup window. At this stage, I'd be happy to simply click PayBill, wait for the popup window (which loads instantly), then click Cancel.

The latest version of my code reads:

paybillButton.click();
browser.sleep(500);

browser.getAllWindowHandles().then(function (handles) {
  newWindowHandle = handles[1];
  browser.switchTo().window(newWindowHandle).then(function () {
    cancelButton.click();
  });
});

But it keeps failing between browser.switchTo() and .window(newWindowHandle).then ...

Error:

Failed: null value in entry: handle=null
WebDriverError: null value in entry: handle=null
    at Object.checkLegacyResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:505:15)
    at parseHttpResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509:13)
    at doSend.then.response (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:440:13)
    at process._tickCallback (internal/process/next_tick.js:103:7)
From: Task: WebDriver.switchTo().window(undefined)
    at thenableWebDriverProxy.schedule (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:816:17)
    at TargetLocator.window (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:1755:25)

I've also tried the code below from Failed: null value in entry: name=null error while switching Popup window ...

function windowCount(count) {
  return function () {
    return browser.getAllWindowHandles().then(function (handles) {
      return handles.length >= count;
    });
  };
};
browser.wait(windowCount(2), 10000);

browser.getAllWindowHandles().then(function (handles) {
  browser.switchTo().window(handles[1]);
  cancelButton.click();
});

But that's returning this error:

Failed: Wait timed out after 10002ms
TimeoutError: Wait timed out after 10002ms
    at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2107:17
    at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1366:14)
    at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2970:14)
    at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2953:27)
    at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2813:27)
    at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:676:7
    at process._tickCallback (internal/process/next_tick.js:103:7)
From: Task: <anonymous wait>
    at scheduleWait (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2094:20)
    at ControlFlow.wait (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2408:12)


Solution 1:[1]

The switchTo method is a wrapper to the selenium-webdriver switchTo method. The switchTo method returns a TargetLocator object and the TargetLocator object does not have a window method. The correct way to do this is to use the frame method.

browser.getAllWindowHandles().then((handles) => {
  let newWindowHandle = handles[1];
  browser.switchTo().frame(newWindowHandle).then(() => {
    cancelButton.click();
  });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cnishina