0

I have E2E tests (using Selenium WebDriver) for an Angular 4/ASP.NET Core Web API application.

I am suffering from occasional timeout/wait issues within some of the tests.

The E2E framework has a method WaitForAngular() which uses some Javascript (taken from Protractor) that waits for Angular to indicate it has loaded all 'testabilities' before continuing (see https://stackoverflow.com/a/39349865/95423)

However this doesn't seem totally reliable and occasionally on a run we end up with indefinite waits occurring.

The other approach we use is implicit waits for the element in question to be rendered:

var url = "targetURL"
Driver.Instance.Navigate().GoToUrl(url);

var waitSeconds = 15;
var navWait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(waitSeconds))
{
    Message = $"The targetURL failed to load in {waitSeconds} seconds"
};

navWait.Until(x => x.FindElementById(targetUrlId) != null || x.FindElementById(forbiddenId) != null);

I'm seeing more robust results from the second approach, however it means more code and potentially more brittle tests if the condition of the implicit wait is not carefully considered.

Which is the better approach here, or is there an alternative?

TonE
  • 2,975
  • 5
  • 30
  • 50

1 Answers1

0

Which version of Angular are you using?

If you just need to wait for an element to be rendered you could try:

import { browser } from 'protractor';
const waitLimit = 15000;
browser.wait(() => {
  return myHtmlElement.isDisplayed();
}, waitLimit);
verymadmango
  • 124
  • 1
  • 5
  • Thanks, however the question is more 'which would be better: wait for a specific element to be rendered or wait for angular to signal its done' – TonE May 21 '18 at 12:06
  • Protractor should automatically apply waitForAngular between each webdriver action, you shouldn't need to manually trigger it, however there are some events angular struggles to keep track of, in my case some of the custom animations or scroll events were not taken into account and I think that's where waiting for a particular element to render or be present where you expect it is a valid approach. Hope that helps! – verymadmango May 21 '18 at 12:25
  • We're using Selenium, not Protractor to drive our tests. We've just 'borrowed' some code from Protractor to implement a WaitForAngular method. – TonE May 21 '18 at 16:02