Explicit Wait is related with certain conditions to wait; Implicit Wait with specific time to wait for an Element.
Explicit Wait
Here, the driver waits for 10 secs till the web element is found; If not, it simply throws a Timeout Exception.
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("Value")));
or|
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Value")));
WebDriverWait can't be declared globally and throw NullPointerException; whereas the below snippets will help you to do so.
// Handles any locator
@Test
public void Test01() throws Exception {
driver.get("www.xyz.com");
wait().until(ExpectedConditions.presenceOfElementLocated(By.xpath("Value")));
}
private WebDriverWait wait()
{
return new WebDriverWait(driver, 20);
}
// Handles any locator [This method works for page object pattern]
private WebDriver driver;
private final Wait<WebDriver> wait;
public Classname(WebDriver driver) { //constructor
this.driver = driver;
wait = new WebDriverWait(driver, 20);
}
@Test
public void Test01() throws Exception {
driver.get("www.xyz.com");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("Value")));
}
// Handle the locators by id, name, xpath, css, etc.,
@Test
public void Test02() throws Exception {
driver.get("www.xyz.com");
waitForID("Value");
}
public void waitForID(String id)
{
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
}
Explicit Wait
Here, the driver waits for 10 secs till the web element is found; If not, it simply throws a Timeout Exception.
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("Value")));
or|
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Value")));
WebDriverWait can't be declared globally and throw NullPointerException; whereas the below snippets will help you to do so.
// Handles any locator
@Test
public void Test01() throws Exception {
driver.get("www.xyz.com");
wait().until(ExpectedConditions.presenceOfElementLocated(By.xpath("Value")));
}
private WebDriverWait wait()
{
return new WebDriverWait(driver, 20);
}
// Handles any locator [This method works for page object pattern]
private WebDriver driver;
private final Wait<WebDriver> wait;
public Classname(WebDriver driver) { //constructor
this.driver = driver;
wait = new WebDriverWait(driver, 20);
}
@Test
public void Test01() throws Exception {
driver.get("www.xyz.com");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("Value")));
}
// Handle the locators by id, name, xpath, css, etc.,
@Test
public void Test02() throws Exception {
driver.get("www.xyz.com");
waitForID("Value");
}
public void waitForID(String id)
{
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
}
Comments
Post a Comment