Tuesday, January 5, 2016

E2E Test - Protractor - Redirect to non-angular page

This one happens when I try to redirect browser to a non-angular web page in my e2e-test.
I got the following error message:

 Failed: Error while waiting for Protractor to sync with the page: "window.angular is undefined. This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See http://git.io/v4gXM for details"  


Simplest workaround:


Set browser.ignoreSynchronization = true in advance.
Then put browser.sleep(msTimeToDelay) after the code which triggers redirection, before the code expecting something from the target page.
Like this.

 browser.ignoreSynchronization = true;  
 redirectButton.click();  
 browser.sleep(2000);  
 expect(browser.getCurrentUrl()).toBe('www.google.com');  

The most important magic word is "browser.ignoreSynchronization = true;".

If you don't set it to true, no matter what you do Protractor will stand in your way and give you error message mentioned above.

I know because I spent whole afternoon today trying to redirect browser to another page, and Protractor just wouldn't let me.

I googled many solutions, most of them use browser.wait to wait for some element in target page to be present, didn't work for me though.

At last I had this SO post to come to my rescue, then I realized that browser.ignoreSynchronization is what I missed.

The reason why Protractor can be so annoying when you want redirect to other non-angular page, is that It always wait Angular to finish some promises, such like $http request or $timeout.

When Protractor found the target page not an angular app, it can't wait for nothing, it then gets pissed off and stop running your test.

Can call it a pain-in-my-ass day then~

No comments:

Post a Comment