I'm doing automated tests(Selenium Java) in a web application that is based on Angular 14.2.12. I check if Angular is loaded using the following code(I paste the whole thing together with jQuery and JS).
public class JSWaiter { private static WebDriver jsWaitDriver; private static WebDriverWait jsWait; private static JavascriptExecutor jsExec; //Get the driver from relevant test public static void setDriver(ChromeDriver driver) { jsWaitDriver = driver; jsWait = new WebDriverWait(jsWaitDriver, Duration.ofSeconds(120)); jsExec = (JavascriptExecutor) jsWaitDriver; } //Wait for JQuery Load public static void waitForJQueryLoad() { //Wait for jQuery to load ExpectedCondition jQueryLoad = driver -> ((Long) ((JavascriptExecutor) jsWaitDriver) .executeScript("return jQuery.active") == 0); //Get JQuery is Ready boolean jqueryReady = (Boolean) jsExec.executeScript("return jQuery.active==0"); //Wait JQuery until it is Ready! if (!jqueryReady) { //Wait for jQuery to load jsWait.until(jQueryLoad); } } //Wait for Angular Load public static void waitForAngularLoad() { String angularReadynessScript = "return window.getAllAngularTestabilities().findIndex(x=>!x.isStable()) === -1"; angularLoads(angularReadynessScript ); } private static void angularLoads(String angularReadynessScript) { try { ExpectedCondition angularLoad = driver -> { assert driver != null; return Boolean.valueOf(((JavascriptExecutor) driver) .executeScript(angularReadynessScript ).toString()); }; boolean angularReady = Boolean.parseBoolean(jsExec.executeScript(angularReadynessScript ).toString()); if (!angularReady) { jsWait.until(angularLoad); } } catch (Exception e) { System.out.println("Error occurred: " + e.getMessage()); e.printStackTrace(); } } //Wait Until JS Ready public static void waitUntilJSReady() { WebDriverWait wait = new WebDriverWait(jsWaitDriver, Duration.ofSeconds(120)); JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver; //Wait for Javascript to load ExpectedCondition jsLoad = driver -> ((JavascriptExecutor) jsWaitDriver) .executeScript("return document.readyState").toString().equals("complete"); //Get JS is Ready boolean jsReady = jsExec.executeScript("return document.readyState").toString().equals("complete"); //Wait Javascript until it is Ready! if (!jsReady) { //Wait for Javascript to load wait.until(jsLoad); } } //Wait Until JQuery and JS Ready public static void waitUntilJQueryReady() { JavascriptExecutor jsExec = (JavascriptExecutor) jsWaitDriver; //First check that JQuery is defined on the page. If it is, then wait AJAX Boolean jQueryDefined = (Boolean) jsExec.executeScript("return typeof jQuery != 'undefined'"); if (jQueryDefined) { //Pre Wait for stability (Optional) sleep(20); //Wait JQuery Load waitForJQueryLoad(); //Wait JS Load waitUntilJSReady(); //Post Wait for stability (Optional) sleep(20); } else { System.out.println("jQuery is not defined on this site!"); } } //Wait Until Angular and JS Ready public static void waitUntilAngularReady() { try { Object angularCheckVersion = jsExec.executeScript( "return getAllAngularRootElements()[0].attributes['ng-version']"); if (angularCheckVersion != null) { Boolean angularPageLoaded = (Boolean) jsExec.executeScript( "return window.getAllAngularTestabilities().findIndex(x=>!x.isStable()) === -1"); if (!angularPageLoaded) { sleep(20); waitForJQueryLoad(); waitForAngularLoad(); sleep(20); } } } catch (Exception e) { System.out.println("Error occurred: " + e.getMessage()); e.printStackTrace(); } } //Wait Until JQuery Angular and JS is ready public static void waitJQueryAngular() { waitUntilJQueryReady(); waitUntilAngularReady(); } public static void sleep(long milis) { try { Thread.sleep(milis); } catch (InterruptedException e) { e.printStackTrace(); } } }
When I check the readiness of Angular with the command:
window.getAllAngularTestabilities().findIndex(x=>!x.isStable()) === -1everything seems ok, but there is a problem of the type that I am running automated tests, where part of the test cases is based on this popup(assertion) that pops up(is a dynamic element) in app-alerts as a component.
When this popup pops up the above command instead of returning true returns false in the console by which it does not detect this popup and my tests fail.
I have spent two days searching information on this topic and unfortunately found nothing. I would be very grateful for any help.