Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor E2E Testing Error : Object [object Object] has no method 'getWindowHandle'

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);
  });
});
like image 619
monish Avatar asked Feb 11 '14 11:02

monish


1 Answers

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.

like image 200
Furzel Avatar answered Oct 29 '22 18:10

Furzel