Implicit Wait
By using Implicit wait we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.ebay.in/cat/mobiles/brand/Samsung");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Explicit Wait
It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, invisible, etc.
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds); element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));Fluent Wait
Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page..
package PageFactory; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.WebDriverWait; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; /** * Created by Niraj on 2/10/2015. */ public class ConceptPage { WebDriver driver; @FindBy(xpath="//input[@type='submit' and @title='See more']") WebElement btnSeeMore; @FindBy(xpath="//div[@class='itm'][21]//div[@class='itemttl']") WebElement thelement; public static final int DEFAULT_WAIT_4_PAGE = 12; public static final int DEFAULT_WAIT_4_ELEMENT = 15; public ConceptPage(WebDriver driver){ this.driver=driver; PageFactory.initElements(driver,this); } public void verify21stElement(){ WebElement webElement = waitforElementUsingFluent(driver, By.xpath("//div[@class='itm'][21]//div[@class='itemttl']//a"), 20);FluentWait wait = new FluentWait(driver).withTimeout(timeOutInSeconds,TimeUnit.SECONDS).pollingEvery(200,TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class); element = (WebElement) wait.until(ExpectedConditions.visibilityOfElementLocated(by));
public void verify21stElement(){
WebElement webElement = waitForElement
(driver, By.xpath("//div[@class='itm'][21]//div[@class='itemttl']//a"), 20);
System.out.println("Element present : "+ webElement.getAttribute("href"));
}
//Explicit wait function definition here...:
public static WebElement waitForElement(WebDriver driver, final By by, int timeOutInSeconds) {
WebElement element;
try{
//To use WebDriverWait(), we would have to nullify implicitlyWait().
//Because implicitlyWait time also set "driver.findElement()" wait time.
//info from: https://groups.google.com/forum/?fromgroups=#!topic/selenium-users/6VO_7IXylgY
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_ELEMENT, TimeUnit.SECONDS); //reset implicitlyWait
return element; //return the element
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//Fluent wait function definition goes here...
public static WebElement waitforElementUsingFluent(WebDriver driver, By by, int timeOutInSeconds){
WebElement element;
FluentWait wait = new FluentWait(driver).withTimeout(timeOutInSeconds,TimeUnit.SECONDS).pollingEvery(200,TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);
element = (WebElement) wait.until(ExpectedConditions.visibilityOfElementLocated(by));
return element;
}
public void clickOnSeeMore(){
btnSeeMore.click();
}
}
// Main test goes here...
package Selenium; import PageFactory.ConceptPage;import PageFactory.HomePage;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test; import java.util.concurrent.TimeUnit; /** * Created by Niraj on 2/5/2015. */public class LoginWithPageFactory { WebDriver driver; LoginPage loginPage; HomePage homePage; @BeforeTest public void setup(){ driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.ebay.in/cat/mobiles/brand/Samsung");
} @Test(priority = 1) public void test_AJAX(){ driver.get("http://www.ebay.in/cat/mobiles/brand/Samsung"); ConceptPage conceptPage = new ConceptPage(driver); conceptPage.clickOnSeeMore(); conceptPage.verify21stElement();
} public void teardown(){ driver.close(); driver.quit();; } }