[Selenium] common functions comparison
1.Wait for element in default time or self defined time
When the element need some time to be present , be visible, be not present or be not visible, for example : loading icon, waiting time is very import to get the element.
2.Explicit wait
(1) new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator));
(2) new WebDriverWait(driver, 10). until(ExpectedConditions.visibilityOf(locator));
(3) new WebDriverWait(driver, 10). until(ExpectedConditions.presenceOfElementLocated(locator);
(4)
Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return el.findElement(By.cssSelector("div.rptstatus.rptcomplete"));
}
};
//Detect every 2 seconds, the maximum time is 120 seconds
WebDriverWait wait = new WebDriverWait(driver, 120, 2);
wait.withMessage("A processing icon should display in the Status column in the row.”)
wait.until(waitFn);
3.waitForElementVisible VS waitForElementPresent
5.Some element might be Present/Visible, it also might not. But both conditions are correct.
For example : alert dialog
In this condition, we need to use try{ ...} catch()
6.Find element under another element
permissionEl.findElement(By.cssSelector("input[value='true']"))
SeleniumUtil.waitForElementVisible(driver, By.cssSelector(".top-bottom-split"), workSpaceEl);
public void catchIfPopUpDialogAndClickClose(){
try{
SeleniumUtil.waitForElementVisible(driver, By.cssSelector("input#btnClose")).click();
logger.info("Dialog pops up");
}
catch(Exception e)
{
logger.info("No dialog pops up");
}
}