gpt4 book ai didi

javascript - Selenium WebDriver - 等待 Angular 请求(Java)

转载 作者:行者123 更新时间:2023-12-03 15:47:28 36 4
gpt4 key购买 nike

我正在为基于 Angular 应用程序创建测试,我需要帮助。我必须创建一个等待,等待所有未决请求被处理。基本上在我的场景中,我想标记一个复选框,但它仅在 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: unknown error: Cannot read property 'element' of undefined (Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 5 milliseconds Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\lpaczek\AppData\Local\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 代码,而且我是其中的新手。

最佳答案

你可以等待angular完成它的工作

ngwebDriver包会给你这种能力

var driver = new NgWebDriver(driver);

driver.waitForAngularRequestsToFinish();

关于javascript - Selenium WebDriver - 等待 Angular 请求(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430250/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com