'Protractor (WebDriverJS) cannot switchTo window. nameOrHandle is not defined

I have gone around this as many ways as I can think, and still can't get this to work. I am trying to switch to a popUp as a way to automate a login. (Protractor is just a wrapper on WebDriverJS that adds some AngularJS functionality.) The webDriver goes to my main page, clicks the login button and waits for the loggin popup.

So far I have:

var ptor =  protractor.getInstance();
beforeEach(function() {
  var handlesDone = false;
  ptor = protractor.getInstance();
  ptor.get('#/');
  runs(function() {
    return ptor.findElement(protractor.By.className('btn')).click();
  });
  waits(3000);
  runs(function() {
    return ptor.getAllWindowHandles().then(function(handles) {
      popUpHandle = handles[1];
      parentHandle = handles[0];
      return handlesDone = true;
    });
  });
  waitsFor(function() {
    return handlesDone;
  });
});

So far so good, next I want to make sure that I have, in fact, a window handle for my pop-up:

describe('login', function() {
  it('should switch to popUp\'s handle', function() {
    expect(popUpHandle).toBeDefined();

finally, I try to switch to this window:

    ptor.switchTo().window(popUpHandle).getWindowHandle().then(function(handle) {
      expect(handle).toEqual(popUpHandle);
    });
  });
});

Yet no matter what I have tried, I keep getting the following error:

login
    should switch to popUp's handle

Failures:

  1) login should switch to popUp's handle
   Message:
     ReferenceError: nameOrHandle is not defined
   Stacktrace:
     ReferenceError: nameOrHandle is not defined
    at webdriver.WebDriver.TargetLocator.window (.../node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:1385:32)
    at null.<anonymous> (.../test/e2e/e2e-spec.js:40:21)
    at ...node_modules/protractor/jasminewd/index.js:54:12
    at webdriver.promise.ControlFlow.runInNewFrame_ (.../node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1438:20)
    at webdriver.promise.ControlFlow.runEventLoop_ (.../node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1303:8)
    at Timer.exports.setInterval.timer.ontimeout (timers.js:234:14)
==== async task ====

Finished in 5.388 seconds
1 test, 2 assertions, 1 failure

as you can see, I have 2 assertions:

that popUpHandle is defined

that the handle after the switchTo is the same as the popUpHandle

I have tested that I have 2 handles total. I have tested that they are both strings. I have tested that they are different from each other. In this example I test that popUpHandle is defined. All those test pass. Yet no mater what I do, when I try to plug a handle into the .switchTo().window() method I get the same "nameOrHandle is not defined".

I'm stumped. There is so little documentation on WebDriverjs I can't even be sure that switchTo is implemented. Does anyone know what is going on here?

Thanks.



Solution 1:[1]

Add ptor.ignoreSynchronization = true or browser.ignoreSynchronization = true just before you switchTo() the other window. (Use browser.ignoreSynchronization=true for the new implementation where browser replaces protractor.getInstance(). Refer this link)

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 Community