0

I am trying to work with Selenium in Java with Angular 5 based website. Selenium does not support it directly, but JavascriptExecutor can help validating the page components finished loading.

The problem is, I do not know how to implement the JavaScript to validate this.

I am using:

return window.getAngularTestability === undefined

to validate Angular 5 exists in the current page, but the next part of the implementation is a mystery to me.

I know I have to use return window.getAngularTestability somehow.

Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42
Leon Proskurov
  • 135
  • 1
  • 14
  • Can you update the question with _how_ you are using `return window.getAngularTestability === undefined` currently and how differently you want it? – undetected Selenium Apr 25 '18 at 12:15
  • Possible duplicate of [How to use JavaScript with Selenium WebDriver Java](https://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-java) – JeffC Apr 25 '18 at 12:49
  • @JeffC this is not relevant for my question, not duplicate of the issue you linked. thank you for your input – Leon Proskurov May 01 '18 at 07:53

2 Answers2

0

You can create a generic java method for running any javascript within your Java code. Refer below block of code:-

 public void executeJavascript(String script) {
        ((JavascriptExecutor) driver).executeScript(script);
    }

You can pass your return javascript statements as parameters to this method.

Manmohan_singh
  • 1,776
  • 3
  • 20
  • 29
0

I found an answer after a lot of research and searching the web.

The solution is not mine, so i do not deserve the credit.

ExpectedCondition<Boolean> expectation = driver -> ((JavascriptExecutor) driver).executeAsyncScript(
            "var callback = arguments[arguments.length - 1];" +
                    "if (document.readyState !== 'complete') {" +
                    "  callback('document not ready');" +
                    "} else {" +
                    "  try {" +
                    "    var testabilities = window.getAllAngularTestabilities();" +
                    "    var count = testabilities.length;" +
                    "    var decrement = function() {" +
                    "      count--;" +
                    "      if (count === 0) {" +
                    "        callback('complete');" +
                    "      }" +
                    "    };" +
                    "    testabilities.forEach(function(testability) {" +
                    "      testability.whenStable(decrement);" +
                    "    });" +
                    "  } catch (err) {" +
                    "    callback(err.message);" +
                    "  }" +
                    "}"
    ).toString().equals("complete");
    try {
        WebDriverWait wait = new WebDriverWait(webDriver(), 15);
        wait.until(expectation);
    } catch (Throwable error) {
        new Exception("Timeout waiting for Page Load Request to complete.");
    }
Leon Proskurov
  • 135
  • 1
  • 14