しばらくIDEを使用していません。これは私がWebDriverに使うものです。しかし、アルゴリズムは翻訳されます。 JavaScriptはJavaScriptです。つまり、それはあなたのフレームワークに依存します。私が使用してプロトタイプのために
public boolean waitForAngularToLoad(WebDriver driver, int waitTimeInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L);
ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
try {
return ((Boolean)((JavascriptExecutor)driver).executeScript(
"return angular.element(document.body).injector().get(\'$http\').pendingRequests.length == 0;"
));
}
catch (Exception e) {
// Angular not found
log.info("Not found: " + "return angular.element(document.body).injector().get(\'$http\').pendingRequests.length == 0;");
return true;
}
}
};
// wait for browser readystate complete; it is arguable if selenium does this all the time
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState;")
.toString().equals("complete");
}
};
return wait.until(libraryLoad) && wait.until(jsLoad);
}
:アンギュラについては
、私はこの使用
public boolean waitForPrototypeToLoad(WebDriver driver, int waitTimeInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L);
// wait for jQuery to load
ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
try {
return ((Boolean)((JavascriptExecutor)driver).executeScript("return Ajax.activeRequestCount == 0;"));
}
catch (Exception e) {
// Prototype not found
log.info("Not found: " + "return Ajax.activeRequestCount == 0;");
return true;
}
}
};
// wait for browser readystate complete; it is arguable if selenium does this all the time
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState;")
.toString().equals("complete");
}
};
return wait.until(libraryLoad) && wait.until(jsLoad);
}
をjQueryのために、私は誰もがそれをしない、あなたはスピナーロジックのための待ち時間をカスタマイズする必要がある(これを使用します異なって):
public boolean waitForJSandJQueryToLoad(WebDriver driver, long waitTimeInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L);
/*
* If you are curious about what follows see:
* http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedCondition.html
*
* We are creating an anonymous class that inherits from ExpectedCondition and then implements interface
* method apply(...)
*/
ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
boolean isAjaxFinished = false;
boolean isLoaderSpinning = false;
boolean isPageLoadComplete = false;
try {
isAjaxFinished = ((Boolean)((JavascriptExecutor)driver).executeScript("return jQuery.active == 0;"));
} catch (Exception e) {
// no Javascript library not found
isAjaxFinished = true;
}
try { // Check your page, not everyone uses class=spinner
// Reduce implicit wait time for spinner
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
// isLoaderSpinning = driver.findElement(By.className("spinner")).isDisplayed(); // This is the default
// Next was modified for GoComics
isLoaderSpinning = driver.findElement(By.cssSelector("#progress_throbber > ul > li:nth-child(1) > img[alt='spinner']")).isDisplayed();
if (isLoaderSpinning)
log.info("jquery loader is spinning");
} catch (Exception f) {
// no loading spinner found
isLoaderSpinning = false;
} finally { // Restore implicit wait time to default
driver.manage().timeouts().implicitlyWait(new DriverFactory().getImplicitWait(), TimeUnit.SECONDS);
}
isPageLoadComplete = ((JavascriptExecutor)driver).executeScript("return document.readyState;")
.toString().equals("complete");
if (!(isAjaxFinished & !(isLoaderSpinning) & isPageLoadComplete))
log.info(isAjaxFinished + ", " + !(isLoaderSpinning) +", " + isPageLoadComplete);
return isAjaxFinished & !(isLoaderSpinning) & isPageLoadComplete;
}
}; // Terminates statement started by ExpectedCondition<Boolean> libraryLoad = ...
return wait.until(libraryLoad);
}
単純に 'waitForEval |ウィンドウ。$。アクティブ| 0 ' –