6
votes

We have an app built using UI router with some resolves for the routes. We are trying to to run some protractor tests against it but it seems like the default browser.get() is not waiting for ui router to fully resolve

Here is our simple test

describe('coach pages', function() {

  beforeEach(function() {
    browser.get("/");
  });

  it('should have the right title', function() {
    expect(browser.getTitle()).toBe('The Title');
  });

  it('should have a heading', function() {
    expect(element(by.css('#test')).isPresent()).toBeTruthy();
  });

});

The first one passes, it finds the page title, but the heading doesnt

if we add this

browser.sleep(1000);

just before the second expect it passes?

shouldnt protractor wait for the resolves to finish by default? is it some kind of conflict with UI router?

Its worth adding that any routes that dont have resolves it does seem to wait for it as expected, it only affect routes where there is a resolve attached?

1
Just adding a reference to the related issue on github:github.com/angular/protractor/issues/789#issuecomment-54083578 - morgs32

1 Answers

0
votes

Before performing any action, Protractor asks Angular to wait until the page is synchronized. This means that all timeouts and http requests are finished.

That is a quote from timeouts from protractor

So if you would like to have protractor waiting for a method to complete, wrap it with $timeout. $timeout default time is 0 second. However, you do need to realize $timeout calls $apply which starts digest cycle. This may have performance implication. You just need to try it out yourself.

Update

The old answer wasn't the best approach and quite different from what I used later on.

The most reliable way of waiting for a page to be loaded is to wait for an element appear or rendered on that page. In that sense, that is much more reliable.