vishnu vishnu - 1 year ago 297
Javascript Question

Protractor browser.getCurrentUrl() is not working on route change

I have just started using protractor for e2e testing of an Angular single page application. I have started the test with login page. This the test I wrote for fail case

Tesr Case 1

it('Login with wrong email', function() {

expect(browser.getCurrentUrl()).toBe(loginPage.appUrl + '/#/login')

The above code works perfectly. I am testing login failure with url, if the url haven't changed consider it as a failure. Q1)Is this the right approach? should I check for the error message, I have seen an example like this, so testing login failure with url.

Test Case 2

This is where I am getting the error, test case to check the successful login.

it('Login with correct email', function() {
expect(browser.getCurrentUrl()).toBe(loginPage.appUrl + '/#/home')

The above test case also works perfectly if I don't use
, I am getting the following error when I use
, this
is a successful login attempt and redirects to another route.

Timed out waiting for Protractor to synchronize with the page after 11 seconds. Please see The following tasks were pending

I have googled this issue and found lot of solution and none of them seems to work.

Tried Out solutions

  1. Added
    to protractor configuration file.

  2. Added
    to configuration file

  3. Found following SO questions link1, link2 and this github issue, tried all three and none seems to be working

  4. When I googled with the
    error message, I found this SO questions link1,link2, I tried all which is not working

Basically all the solutions says to use
. I tried all in
fashion, since all returns promise. I found this issue is because of using
. Let me know where I am doing wrong and would like to know deep about e2e testing with protractor.

I would like to know the basics of protractor, how it works on route change and how an async user action like
is handled by protractor. Should I explicitly use promises etc.

Any help is greatly appreciated.

Answer Source

In an ideal world, you should not be adding any waits and Protractor should naturally wait for Angular to be ready and in general work in sync with it.

In a real world, unfortunately, there is often a need to add waits. Though, prefer Explicit Waits with specific conditions to hardcoded browser.sleep() calls which should generally be avoided. In this case, we can add a wait to wait for a specific URL, see this answer.