Search code examples
javascriptangularjsprotractorangularjs-e2egulp-protractor

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() {
    loginPage.get();
    loginPage.setEmail('[email protected]');
    loginPage.setPassword('12345678');
    loginPage.submit()

    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() {
  loginPage.get();
  loginPage.setEmail('[email protected]');
  loginPage.setPassword('12345678');
  loginPage.submit()
  expect(browser.getCurrentUrl()).toBe(loginPage.appUrl + '/#/home')
})

The above test case also works perfectly if I don't use browser.getCurrentUrl(), I am getting the following error when I use getCurrentUrl, this loginPage.submit() 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 https://github.com/angular/protractor/blob/master/docs/faq.md. 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 allScriptsTimeout, getPageTimeout to protractor configuration file.
  2. Added defaultTimeoutInterval 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 timeout error message, I found this SO questions link1,link2, I tried all which is not working

Basically all the solutions says to use wait, sleep, waitForAngular. I tried all in thenable fashion, since all returns promise. I found this issue is because of using browser.getCurrentUrl. 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 $http is handled by protractor. Should I explicitly use promises etc.

Any help is greatly appreciated.


Solution

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