6

I am trying to check the pop up for facebook login opening on click of button .

Error : Object [object Object] has no method 'getWindowHandle'.

Code Snippet generating error :

describe('Tests', function() {
  var ptor;
  var handlePromise;
  var util = require('util');

  beforeEach(function() {
    ptor = protractor.getInstance();
    handlePromise = ptor.getAllWindowHandles();
    var handlesDone = false;
    ptor.get('/SiteB_Upgrade_Device/app/index.html#/Recommendations#page');
    ptor.findElement(by.id('fb')).click();
    ptor.ignoreSynchronization = true;
  });

  describe('login', function() {
    return it('should switch to popUp\'s handle', function() {
      handlePromise.then(function(handles) {
        var popUpHandle = handles[0];
        var handle = browser.driver.switchTo().window(popUpHandle).getWindowHandle();
        expect(handle).toEqual(popUpHandle);
      });
    },30000);
  });
});
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
monish
  • 153
  • 1
  • 2
  • 10
  • have you tried `browser.driver.switchTo().window(popUpHandle).getWindowHandle().then(function (handle) { expect(handle).toEqual(popupHandle) });` ? – Furzel Feb 11 '14 at 13:51
  • Thanks for your reply,but i have tried that but it doesn't work. – monish Feb 11 '14 at 14:28
  • What you might try if it is possible in your test case is checking for the url rather than the handle, something like `expect(browser.getCurrentUrl()).toEqual('my/expected/url')` could do the trick if you run into troubles with `getWindowHandle()` – Furzel Feb 11 '14 at 14:41
  • getCurrentUrl() matches the url of parent window but we need to test url of pop up window . – monish Feb 12 '14 at 11:11
  • Don't have much time today to investigate but I noticed your use handles[0] which refer to the original window, the popup should be in handles[1]. – Furzel Feb 12 '14 at 14:52

1 Answers1

6

Here is what I currently use to navigate through popups/tabs :

// do stuff that will trigger the popup
// ...
browser.getAllWindowHandles().then(function (handles) {
  // switch to the popup
  browser.switchTo().window(handles[1]);
  // make sure the popup is now focused
  expect(browser.getCurrentUrl()).toEqual('popup/url');
  // do stuff with the popup
  // ...
  // go back to the main window
  browser.switchTo().window(handles[0]);
  // make sure we are back to the main window
  expect(browser.getCurrentUrl()).toEqual('original/url');
});

You just need to make sure that your popup is really a new window and not juste some kind of popover ( in which case you can just target it with css selectors ).

Another thing to keep in mind when you change tabs/popups it that the target page might not have angularjs loaded in it, which will render protractor useles. If you face this case you can simply use browser.driver as a replacement for browser to navigate a non angular page.

Hope this helps.

Furzel
  • 606
  • 8
  • 18
  • I have tried browser.driver also but getting error : Object [object Object] has no method 'getWindowHandle' Code Snippet : it('User should be able to login with correct Login credentials', function() { var ptor = protractor.getInstance(); ptor.sleep(9999); ptor.findElement(by.id('fb')).click().then(function() { handlePromise.then(function(handles) { var popUpHandle = handles[1]; browser.driver.switchTo().window(popUpHandle).getWindowHandle().then( function (handle) { expect(handle).toEqual(popupHandle) }); }); },30000); – monish Feb 18 '14 at 14:11
  • http://elementalselenium.com/tips/4-work-with-multiple-windows suggests we should not assume 0 is the main and 1 is the popup? – Jeremy Kahan May 04 '17 at 03:34