Skip to main content

Implicit and Explicit Waits in selenium with examples

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)));      
   }

Comments

Popular posts from this blog

Jenkins CLI examples

Automate Jenkins with the CLI or the REST API Jenkins is used a lot to automate your build process and make it a real continuous integration process. But you can even take it a step further and automate the configuration of Jenkins itself.  In this short instruction I will show two ways to do this: the CLI and the REST API. With these capabilities you can for example write programs to create, backup, restore, start and view Jenkins jobs.  Download the Try-it-out-yourself code to provision an Ubuntu VM with Jenkins installed. This way you can immediately try the examples below.  The CLI   The Jenkins CLI is distributed inside the jenkins.war, but you have to download it before you can use it. Suppose your Jenkins url is: http://localhost:8080/jenkins Then the CLI can be downloaded like this:  wget http://localhost:8080/jenkins/jnlpJars/jenkins-cli.jar Note In the remainder of this document I assume the Jenkins url is http://localhost:8080/jenkins. You shoul

Jenkins Matrix Jobs

For many similar different combination build setup. Example Diff OS v/s Diff platform Configuration Matrix CHROME IE Test Module1 Test Module2 Test Module3 Test Module4 Plugin required:  matrix-project Wiki:  https://wiki.jenkins-ci.org/display/JENKINS/Matrix+Project+Plugin Location to download plugin: http://updates.jenkins-ci.org/download/plugins/ Install Plugin: Jenkins>Manage jenkins> Manage Plugin> Advansed> Browse and install Result Look like: Configuration: