Brodrick
Brodrick

Reputation: 43

Navigating from one single page angular app to another with protractor gives "javascript error: document unloaded while waiting for result"

I've looked at this: JavascriptError: javascript error: document unloaded while waiting for result

and it kind of works, but I'm hung up on the idea that there should be a way to do this without sleeping the browser.

Basically, our application consists of several connected single page angular apps. When we go from the login page to the user home page we trigger a page load.

When testing this flow with protractor (login to home page --> do something on home page), about 80% of the time Angular will give me:

Failed: javascript error: document unloaded while waiting for result

on the line immediately after the login information is submitted.

I can solve this problem with a sleep in "waitForNewAngularPage" immediately after the login code as such:

TEST:

helper.doSomeDBSetup();
helper.login(userEmail, password);
helper.waitForNewAngularPage("home");

performTestOnThisPage();

Wait for page code:

exports.waitForNewAngularPage = function (page) {
  return browser.driver.wait(function () {
    return browser.driver.getCurrentUrl().then(function (url) {
      return url.indexOf(page) > -1;
    });
  }, TIMEOUT).then(function() {
    // TODO - we shouldn't need to sleep.  We need to figure out how to load angular when switching
    // between different single page angular apps so that protractor doesn't get confused.
    return browser.sleep(1000);
  });
};

Login code:

exports.login = function (email, password) {
  browser.get(browser.baseUrl + "/login");
  exports.waitForNewAngularPage("login");

  //login user
  element(By.css('[role="email"]')).sendKeys(email);
  element(By.css('[role="password"]')).sendKeys(password);
  element(By.css('[role="submit"]')).click();
  browser.sleep(500); // TODO - this should not require a sleep.
};

Is there any way to help protractor not get lost when transitioning between different single page angular apps?

Given that the tests don't work when using this login function without sleeps in both places it seems like protractor gets confused both before and after the page transition when we switch between apps, but I don't know how to solve it or if there's something else I should wait for.

(I've looked at https://github.com/angular/protractor/issues/2600 and similar issues and adding as many browser.waitForAngular()s doesn't fix anything without having these sleeps.)

Upvotes: 2

Views: 1250

Answers (2)

Brodrick
Brodrick

Reputation: 43

It looks like nobody's really found a better way around this, so I'll just answer for posterity. Check out the comment on the original question for what we did: Navigating from one single page angular app to another with protractor gives "javascript error: document unloaded while waiting for result"

As unsatisfying as it may be, I'm pretty sure we just went with using the sleeps @MichielThalen (We're now using React with Nightwatch for our front end/integration tests, 2 years saw a lot of changes :P)

Upvotes: 1

Emile Muny
Emile Muny

Reputation: 1

Try increasing your Protractor defaultTimeoutInterval in your config file.

jasmineNodeOpts: {
  defaultTimeoutInterval: 30000 
}

Also you can read more here, https://github.com/angular/protractor/blob/master/docs/timeouts.md

Upvotes: 0

Related Questions