Selenium WebDriver - 等待角度请求(Java)

时间:2017-08-01 06:19:06

标签: javascript java angularjs selenium automated-tests

我正在为基于角度的应用创建测试,我需要帮助。 我必须创建一个等待,直到所有待处理的请求都将被处理。 基本上在我的场景中我想标记一个复选框,但它仅在GET请求完成时可用,如果我使用简单Thread.sleep(2000)一切正常,但我知道这不是解决此问题的可靠方法。 我尝试了两种方法:

首先:

   AdditionalConditions wait = new AdditionalConditions();
        wait.untilAngularFinishHttpCalls();



 public void untilAngularFinishHttpCalls() {
        final String javaScriptToLoadAngular =
                "var injector = window.angular.element('app-root').injector();" +
                        "var $http = injector.get('$http');" +
                        "return ($http.pendingRequests.length === 0)";

        ExpectedCondition<Boolean> pendingHttpCallsCondition = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript(javaScriptToLoadAngular).equals(true);
            }
        };
        WebDriverWait wait = new WebDriverWait(SharedDriver.getDriver(), 20); // timeout = 20 secs
        wait.until(pendingHttpCallsCondition);
    }

在这里我得到以下错误:

  

org.openqa.selenium.WebDriverException:未知错误:无法读取   属性'element'未定义(会话信息:chrome = 59.0.3071.115)
  (司机信息:chromedriver = 2.31.488763   (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform = Windows NT   10.0.15063 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:5毫秒构建信息:   版本:'2.31.0',修订版:   '1bd294d185a80fa4206dfeab80ba773c04ac33c0',时间:'2013-02-27   13:51:26'系统信息:os.name:'Windows 10',os.arch:'amd64',   os.version:'10 .0',java.version:'1.8.0_121'驱动程序信息:   org.openqa.selenium.chrome.ChromeDriver功能   [{applicationCacheEnabled = false,rotate = false,   mobileEmulationEnabled = false,networkConnectionEnabled = false,   铬= {chromedriverVersion = 2.31.488763   (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),   userDataDir = C:\用户\ lpaczek \应用程序数据\本地\ TEMP \ scoped_dir13872_15975},   takesHeapSnapshot = true,pageLoadStrategy = normal,   databaseEnabled = false,handlesAlerts = true,hasTouchScreen = false,   version = 59.0.3071.115,platform = XP,browserConnectionEnabled = false,   nativeEvents = true,acceptSslCerts = true,locationContextEnabled = true,   webStorageEnabled = true,browserName = chrome,takesScreenshot = true,   javascriptEnabled = true,cssSelectorsEnabled = true,setWindowRect = true,   unexpectedAlertBehaviour =}]

我也试过了:

   WebDriverWait wait = new WebDriverWait(SharedDriver.getDriver(), 15, 500);
        wait.until(AdditionalConditions.angularHasFinishedProcessing());


public class AdditionalConditions {
    public static ExpectedCondition<Boolean> angularHasFinishedProcessing() {
        return new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                return Boolean.valueOf(((JavascriptExecutor)
                        driver).executeScript("return (window.angular !== undefined) && (angular.element(document).injector() !== undefined) && (angular.element(document).injector(). get('$http').pendingRequests.length === 0)").toString());
            }
        };
    }

在这里,我在15秒后得到超时。似乎它不起作用。 如果可以,请提供帮助,据我所知,我需要执行JavaScript代码并且我是新手。

1 个答案:

答案 0 :(得分:0)

你可以等待角度来完成它的工作

ngwebDriver包将为您提供该功能

var driver = new NgWebDriver(driver);

driver.waitForAngularRequestsToFinish();