Tuesday 23 August 2016

Selenium: WebDriver: Explicit timeouts


In my previous post, I explained about WebDriver.Timeouts api, which is used to set implicit timeouts. Implicit timeouts are generic, which means it is applied to all the elements in web page. Some times you want to wait for more time for a specific web element to load. To handle these kinds of conditions, WebDriver provides a way to set the timeout specific to this element. Following snippet shows how to set explicit timeout.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class App {

 public static void main(String[] args) throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://localhost:8080/application/");

  WebElement element = (new WebDriverWait(driver, 30))
    .until(new ExpectedCondition<WebElement>() {
     @Override
     public WebElement apply(WebDriver d) {
      return d.findElement(By.name("userName"));
     }
    });

  System.out.println(element.getText());

  driver.quit();
 }
}


Previous                                                 Next                                                 Home

No comments:

Post a Comment