自定义等待硒用于angular7应用程序

时间:2019-04-15 09:29:23

标签: java selenium angular7

我们正在使用Selenium Webdriver自动化Angular 7应用程序。我需要使用Javascript或JQuery进行自定义的等待,这些等待将等待页面呈现并等待$ http响应完成。

我尝试了显式等待,但是由于元素已加载到页面上但仍在加载并尝试使用ng Webdriver,但它们无法正常工作。

1 个答案:

答案 0 :(得分:0)

这些可能对您有所帮助。在访问任何元素之前,请检查Jquery / Angular是否完成。

  public static boolean isJQueryDone() {
            Object jsResponse = tryJavascript("return jQuery.active;");
            if (jsResponse instanceof Long) {
                return ((Long) jsResponse) == 0;
            } else if (jsResponse instanceof String) {
                String response = (String) jsResponse;
                return (response.startsWith("{\"hCode\"") || response.isEmpty());
            } else {
                return true;
            }
        }

        public static boolean isAngularDone() {
            Object jsResponse = tryJavascript("return window.getAllAngularTestabilities().filter(x=>!x.isStable()).length;");
            if (jsResponse instanceof Long) {
                return ((Long) jsResponse) == 0;
            } else if (jsResponse instanceof String) {
                String response = (String) jsResponse;
                return response.isEmpty();
            } else {
                return true;
            }
        }
        public static synchronized Object tryJavascript(String script, Object... args) {
            try {
                return execJavascript(script, args);
            } catch (Exception ignore) {
                return "";
            }
        }