Today most of web application using Ajax, When page is load some element load in a different time of interval or some time request send without page loading if the element not present then it through element not found exception or not visible element etc.. To handle this Webdriver provides two type of wait “implicit” and “explicit” wait
Implicit Wait: implicit wait provide to load DOM object for a particular of time before trying to locate element on page. Default implicit wait is 0. We need to set implicit wait once and it apply for whole life of Webdriver object. Add below line of code in test for implicit wait. However implicit wait slow down execution of your test scripts if your application responding normally.
Custom-expected condition: webdriver provide you to create custom wait condition.
Here is an example:
Implicit Wait: implicit wait provide to load DOM object for a particular of time before trying to locate element on page. Default implicit wait is 0. We need to set implicit wait once and it apply for whole life of Webdriver object. Add below line of code in test for implicit wait. However implicit wait slow down execution of your test scripts if your application responding normally.
driver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
Example:
package
com.test;
import
java.util.concurrent.TimeUnit;
import
org.junit.Assert;
import
org.openqa.selenium.*;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.AfterMethod;
import
org.testng.annotations.BeforeMethod;
import
org.testng.annotations.Test;
public class
DataDrivenTest {
private WebDriver driver;
private String baseUrl;
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl =
"http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl +
"/wiki/Main_Page");
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys("India");
driver.findElement(By.id("searchButton")).click();
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
Explicit Wait: In explicit wait you can write custom code for a particular element to wait for particular time of period before executing next steps in your test. This provide you better option than implicit wait. Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this.
ExpectedConditions class provide some set of predefine condition to wait element. Here is some condition which is use in ExpectedConditions class:
ExpectedConditions class provide some set of predefine condition to wait element. Here is some condition which is use in ExpectedConditions class:
- alertIsPresent() : Alert is present
- elementSelectionStateToBe: an element state is selection.
- elementToBeClickable: an element is present and clickable.
- elementToBeSelected: element is selected
- frameToBeAvailableAndSwitchToIt: frame is available and frame selected.
- invisibilityOfElementLocated: an element is invisible
- presenceOfAllElementsLocatedBy: present element located by.
- refreshed: wait for a particular condition when page refresh.
- textToBePresentInElement: text present on particular an element
- textToBePresentInElementValue: and element value present for a particular element.
- visibilityOf: an element visible.
- titleContains: title contains
package com.test;
import
java.util.concurrent.TimeUnit;
import
org.junit.Assert;
import
org.openqa.selenium.*;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.ui.ExpectedConditions;
import
org.openqa.selenium.support.ui.WebDriverWait;
import
org.testng.annotations.AfterMethod;
import
org.testng.annotations.BeforeMethod;
import
org.testng.annotations.Test;
public class
DataDrivenTest {
private WebDriver driver;
private String baseUrl;
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl =
"http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl +
"/wiki/Main_Page");
//explicit wait for search field
WebDriverWait wait = new
WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("searchInput")));
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys("India");
driver.findElement(By.id("searchButton")).click();
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
Custom-expected condition: webdriver provide you to create custom wait condition.
Here is an example:
package
com.test;
import
java.util.concurrent.TimeUnit;
import
org.junit.Assert;
import
org.openqa.selenium.*;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.ui.ExpectedCondition;
import
org.openqa.selenium.support.ui.WebDriverWait;
import
org.testng.annotations.AfterMethod;
import
org.testng.annotations.BeforeMethod;
import
org.testng.annotations.Test;
public class
DataDrivenTest {
private WebDriver driver;
private String baseUrl;
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl =
"http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl +
"/wiki/Main_Page");
//custom explicit wait for search field
new WebDriverWait(driver, 10)
.until(new
ExpectedCondition(){
@Override
public WebElement
apply(WebDriver d) {
return
d.findElement(By.id("searchInput"));
}});
driver.findElement(By.id("searchInput")).sendKeys("India");
driver.findElement(By.id("searchButton")).click();
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
super example
ReplyDeleteThank you for this wonderful article.
ReplyDeleteHi, Nice article; Could you advice if Imlicit Wait is set to 10 seconds how many times would the driver poll the DOM if element is not found? is it only twice or more if the element is not there? thaks NM pellasg@gmail.com
ReplyDeleteHi, Nice Article. Got a question. How many times would webdriver poll the SOM if Implicit wait is set to 10 seconds ? is it only twice for any time set? Start and at the end of time set ... or is it every 500 mls checks? thanks
ReplyDelete