protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Protractor will not switchTo().window(handle). nameOrHandle Undefined error.

Open rlrhett opened this issue 13 years ago • 17 comments

Question cross-posted on SO. http://stackoverflow.com/q/18394261/1226721

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 goes to my main page, clicks the login button and waits for the loggin popup.

The setup:

var ptor;
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. (.../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:

  1. that popUpHandle is defined

  2. 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.

rlrhett avatar Aug 23 '13 18:08 rlrhett

I usually just look through the webdriver code when trying to find documentation. Check out https://code.google.com/p/selenium/source/browse/javascript/webdriver/webdriver.js and search for 'switchTo'.

I think your problem is that popUpHandle is not defined until after ptor.getAllWindowHandlers(), so when you run ptor.switchTo().window(popUpHandle) it's not yet defined. I can't quite tell though - could you post the entire test?

juliemr avatar Aug 24 '13 00:08 juliemr

I am not sure I understand what you are saying. ptor.getAllWindowHandlers() is run in the BeforeEach setup. In fact, I have it in a runs/waitFor block to make sure that it has finished and the promise has been fulfilled before running the test. As far as I can tell ptor.switchTo().window(popUpHandle) is being run after ptor.getAllWindowsHandlers() has returned. Every test I run before the switchTo() including expect(popUpHandle).toBeDefined() passes.

Regardless, that is the entire test. Here it is as one block of text if that helps:

var Pby, parentHandle, popUpHandle, ptor, util;

util = require('util');

Pby = protractor.By;

parentHandle = {};

popUpHandle = {};

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

describe('login', function() {
  return it('should switch to popUp\'s handle', function() {
    var handle;
    expect(popUpHandle).toBeDefined();
    handle = driver.switchTo().window(popUpHandle).getWindowHandle();
    expect(handle).toEqual(popUpHandle);
  });
});

rlrhett avatar Aug 24 '13 16:08 rlrhett

Any further thoughts? Is this a bug? Is anyone else having this issue? Does this problem occur with straight webDriverJS? Is there more I can add to shed light on this problem? I am completely stalled on e2e testing unless I can figure out how to log into the app, so I am open to any suggestions.

Thanks.

rlrhett avatar Aug 27 '13 03:08 rlrhett

This is indeed an issue with the control flow. In protractor, expect is patched so that it understands webdriver promises, and waits for them to resolve before executing. That's why your expect(popUpHandle).toBeDefined(); is passing, but popUpHandle is still empty later.

You should be able to fix by rearranging your test like this:

var Pby ptor, util;

util = require('util');

Pby = protractor.By;

var handlePromise;

beforeEach(function() {
  ptor = protractor.getInstance();
  ptor.get('#/');
  ptor.findElement(Pby.className('btn')).click();
  handlePromise = ptor.getAllWindowHandles();
});

describe('login', function() {
  return it('should switch to popUp\'s handle', function() {
    handlePromise.then(function(handles) {
      var popUpHandle = handles[1];
      var handle = driver.switchTo().window(popUpHandle).getWindowHandle();
      expect(handle).toEqual(popUpHandle);
    });
});

juliemr avatar Aug 27 '13 17:08 juliemr

That solved the undefined error. I still don't understand why the waitsFor didn't do the job, but at least I can move on with setting up tests. Thanks!

rlrhett avatar Aug 28 '13 17:08 rlrhett

Just as information for other possible users, I modified a bit the promise handle:


            var handles = handlePromise.then(function (handles) {
                popUpHandle = handles[1];
                var handle = browser.switchTo().window(popUpHandle);
                handle = browser.getWindowHandle();
                expect(handle).toEqual(popUpHandle);
                browser.driver.executeScript('window.focus();');
            });

And it worked beautifully. After this point I could send the data to the inputs in the window and do whatever I wanted with it. Pretty.

DanielaValero avatar Jan 29 '14 09:01 DanielaValero

Hi @DanielaValero , I tried the way you provided here, I got an error. Could you please check it? I posted it here: http://stackoverflow.com/questions/25873294/how-to-switch-to-a-new-pop-up-windowa-non-angular-app-to-run-protractor Thank you.

jasonnjit avatar Sep 16 '14 15:09 jasonnjit

Hi @juliemr, could you please check this issue? http://stackoverflow.com/questions/25873294/how-to-switch-to-a-new-pop-up-windowa-non-angular-app-to-run-protractor. Thank you.

jasonnjit avatar Sep 16 '14 15:09 jasonnjit

Hi @juliemr and @DanielaValero , me also facing the same issue reported by @jasonnjit . can u please provide me the solution

Thanks in ADVANCE!!!!

balakmr08 avatar Sep 18 '14 11:09 balakmr08

Hello,

I am not currently active with this issue. Can't help right now. Sorry

DanielaValero avatar Sep 18 '14 12:09 DanielaValero

HI,

Iam also having same problem with angularJS page . The size of handles are showing only. how to enter text in popup text field.

prakashomp avatar Oct 01 '14 09:10 prakashomp

HI guys ,

My pop up is also angular based , so my friends asked me to use some what like this:

element(by.id("").element(by.model("<model_name>")).sendKeys("value");

and its working for me.

balakmr08 avatar Oct 01 '14 10:10 balakmr08

@balakmr08 Is that working for a pop-up? Cause I found switchTo() only works for a new window or a new tab, but not a new pop-up.

jasonnjit avatar Oct 02 '14 20:10 jasonnjit

@jasonnjit : yes , its working for popup . U need to use any specific attribute to get into the popup (like id , name or css of the popup) and using this we can access inner elements

balakmr08 avatar Oct 20 '14 12:10 balakmr08

Hi everyone, I can still reproduce this with 3.0.0 and webdriver 2.53.0. Code:


it('checks tabs', () => {
  const url1 = 'http://stackoverflow.com/';
  const url2 = 'http://programmers.stackexchange.com/';

  let windowHandles = {
    oldTab: '',
    newTab: ''
  };

  await browser.get(url1);
  await browser.getWindowHandle().then(handle => {
     windowHandles.oldTab = handle;
  });

  await browser.executeScript('window.open("' + url2 + '", "whatever")');

  await browser.getAllWindowHandles()
    .then(handles => {
      expect(handles[0]).toEqual(windowHandles.oldTab);

      windowHandles.newTab = handles[1];
      return browser.driver.switchTo().window(windowHandles.oldTab);
    })
    .then(() => {
      let handle = browser.driver.getWindowHandle();
      expect(handle).toEqual(windowHandles.oldTab);
    })
    .then(() => browser.sleep(6000));
});

If you are using stackoverflow, here's the link for the same question there: http://stackoverflow.com/questions/36578927/protractor-3-0-0-webdriver-2-53-0-switching-tabs-error

latobibor avatar Apr 12 '16 16:04 latobibor

Hello, I have the same issue. I am using :

  1. Selenium WebDriver dll. Version 2.53.0 Installed by Nuget
  2. IEDriverServer.exe Version 2.53.0 64 bit version downloaded from Selenium Website

My code is same as @latobibor 's code, and it gets stuck on line browser.driver.switchTo().window(windowHandles.oldTab);

Is it a Selenium Bug, or is this the wrong syntax to Switch Window Handle?

when i debug the issue, i get the following exception after waiting a long time :

An exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll but was not handled in user code

Additional information: The HTTP request to the remote WebDriver server for URL http://127.0.0.1:4444/wd/hub/session/36fa4074-3b34-4dea-9da8-c8278b135c4f/window timed out after 240 seconds.

sophiaatif avatar Apr 22 '16 16:04 sophiaatif

For a single pop-up I just do browser.switchTo().frame(1); and works like a champ.

shane-reaume avatar Mar 22 '17 01:03 shane-reaume