0

I am writing an automation tool for a website. Therefore I am using Selenium in Java. For the real automation purpose I mainly use JavaScript through JavascriptExecutor. Most of the time everything works fine, but sometimes it crashes (e.g. 1 out of 10 times). I have the impression that then the code was just too fast. I am using implicit wait and explicit wait from the WebdriverWait class. I think this waits just wait for the dom or the elements within, but they are not waiting until all scripts are done. Therefore I need a function or snippet. As mentioned the website is using vue and angular.

Thanks in advance!

2 Answers2

0

In your case, there is possibility that your script get failed while navigating to other html page by clicking link or button. If your application fails/crashes in these scenarios, include page load time on implicit wait as well.

0

You can add an explicit wait that will wait for angular to finish processing any pending requests:

public static ExpectedCondition angularHasFinishedProcessing() {
    return new ExpectedCondition() {
        @Override
        public Boolean apply(WebDriver driver) {
            JavascriptExecutor jsexec = ((JavascriptExecutor) driver)
            String result = jsexec.executeScript("return (window.angular != null) && (angular.element(document).injector() != null) && (angular.element(document).injector().get('$http').pendingRequests.length === 0)")
            return Boolean.valueOf(result);
        }
    };
}

To use it you would then do:

WebDriverWait wait = new WebDriverWait(driver, 15, 100);
wait.until(angularHasFinishedProcessing());

This used to remove of lot of flakiness in Angular automation for me.

I would suggest always using explicit waits and never using implicit waits, implicit waits will make negative checks take forever. Also don't ever mix implicit and explicit waits, it has the potential to cause all sorts of strange undefined behaviour.

Ardesco
  • 7,281
  • 26
  • 49
  • Is it enough to wait for angular, to cover everything? –  Mar 13 '19 at 21:34
  • I'm not sure what you mean by cover everything. This should remove the majority of issues where you are waiting for angular to finish processing the page before you start working on it. It's not a silver bullet though, there may be other explicit actions you need to wait for as part of your automation flows. This should get rid of a lot of the noise and allow you to focus on things that are more likely to be real issues – Ardesco Mar 14 '19 at 11:42