Implicit Waits:
WebDriver polls the DOM for a certain duration when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit waits:
WebDriver allow your code to halt program execution,
or freeze the thread,
until the condition you pass it resolves.
The condition is called with a certain frequency
until the timeout of the wait is elapsed.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='email']")));
Here the tool waits a maximum time of 60Sec., if the object you are
looking is displayed in 10 sec. then the execution proceeds with the
next step after 10 secs. rather than waiting for 60 secs.
(OR)
WebElement myDynamicElement = (new WebDriverWait(driver,
60))
.until(new
ExpectedCondition(){
@Override
public WebElement apply(WebDriver
d) {
return d.findElement(By.id("myDynamicElement"));
}});
(OR)
public static void waitForElement(final By by) {
int watingTime = 60;
WebElement
myDynamicElement = null;
try{
myDynamicElement = new WebDriverWait(driver, watingTime)
.until(new
ExpectedCondition() {
@Override
public WebElement apply(WebDriver
d) {
return d.findElement(by);
}
});
}catch (Exception e) {
throw new RuntimeException("Exception while waitForElement:",e);
}
}
Here WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully or wait for maximum of 60 sec.
Wait For the Progress Bar or Loading process To Disappear:
public void waitForProgressBarToDisappear(WebDriver driver, String objectLocator) throws SFExceptionHandle {
try {
boolean progressBar = false;
Thread.sleep(1000);
progressBar = isProgressBarActive(driver, objectLocator);
while (progressBar) {
Thread.sleep(1000);
progressBar = isProgressBarActive(driver, objectLocator);
}
} catch (InterruptedException e) {
APPLICATION_LOGS.error("waitForProgressBarToDisappear.: " + e.toString());
}
}
private boolean isProgressBarActive(WebDriver driver, String objectLocator) {
boolean indicator = false;
try {
WebElement loadingIndicator = driver.findElement(By.id(""));
if (loadingIndicator == null) {
indicator = false;
} else {
indicator = loadingIndicator.isDisplayed();
}
} catch (Exception e) {
APPLICATION_LOGS.error("isProgress Bar Active", e);
indicator = false;
}
return indicator;
}
(OR)
boolean progressBar = driver.findElement(by.id("")).isDisplayed();
while (progressBar) {
try {
Thread.sleep(1000);
progressBar = driver.findElement(by.id("")).isDisplayed();
} catch (InterruptedException e) {
}
}
}
How To Wait For Page To Load/Ready:
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
// Initially bellow given if condition will check ready state of
// page.
if (js.executeScript("return document.readyState").toString().equals("complete")) {
return;
}
for (int i = 0; i < timeInSeconds; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
// To check page ready state.
if (js.executeScript("return document.readyState").toString().equals("complete")) {
break;
}
}
} catch (Exception e) {
}
}
Wait for Javascript and jQuery to finish loading:
public boolean waitForJSandJQueryToLoad(WebDriver driver, int timeOutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
// wait for jQuery to load
ExpectedCondition jQueryLoad = new ExpectedCondition() {
@Override
public Boolean apply(WebDriver driver) {
try {
return ((Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active") == 0);
} catch (Exception e) {
// no jQuery present
return true;
}
}
};
// wait for Javascript to load
ExpectedCondition jsLoad = new ExpectedCondition() {
@Override
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) getDriver()).executeScript("return document.readyState").toString()
.equals("complete");
}
};
return wait.until(jQueryLoad) && wait.until(jsLoad);
No comments:
Post a Comment