0

I am testing a angularjs page and using selenium(java) to write automation scripts for the same.

The following is the code that I use for the page synchronization wait before proceeding to next screen action

public static boolean angularHasFinishedProcessing() {
    ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            driver = GetDriver();//This is to get the driver in current action.
            String hasAngularFinishedScript = "var callback = arguments[arguments.length - 1];\n" +
                    "var el = document.querySelector('html');\n" +
                    "if (!window.angular) {\n" +
                     "console.log('1'); \n" +
                    "    callback('false')\n" +
                    "}\n" +
                    "if (angular.getTestability) {\n" +
                    "    angular.getTestability(el).whenStable(function(){callback('true')});\n" +
                    "} else {\n" +
                    "console.log('hello3'); \n" +
                    "    if (!angular.element(el).injector()) {\n" +
                    "        callback('false')\n" +
                    "    }\n" +
                    "    var browser = angular.element(el).injector().get('$browser');\n" +
                    "    browser.notifyWhenNoOutstandingRequests(function(){callback('true')});\n" +
                    "}";

            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            String isProcessingFinished = javascriptExecutor.executeAsyncScript(hasAngularFinishedScript).toString();

            return Boolean.valueOf(isProcessingFinished);
        }
    };

    WebDriverWait wait = new WebDriverWait(driver, 60);
    boolean bRet = (wait.until(pageLoadCondition));
    if (bRet) {
        return bRet;
    } else
        return false;
}

The issue is isProcessingFinished is always false, the console always writes 1 (meaning window.angular always returns false).

Also, Since, there is no way I can debug the javascript snippet during the execution, I don't know if there is any other issue. Could someone help please?

3
  • You could use a conditional statement for the doubtful code and protractor screenshot functionality instead of loging the errors Commented Jan 26, 2018 at 19:57
  • Selenium is too low level library, so you should use some wrappers of it. Take a look at selenide.org, there you can use smart waits and it will solve all your problems Commented Jan 26, 2018 at 20:07
  • Is the variable window.angular present when you check it in the console? Commented Jan 26, 2018 at 20:10

1 Answer 1

2

1) For how to debug javascript

Add a breakpoint before this wait function, run script until stopped at this breakpoint, Open DevTool of browser and execute window.angular in console Tab to see it's true or false.

And you can continue to execute the rest code lines of your javascript snippet in console Tab to examine any code issue or work as expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.