Waiting Mechanism

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Waiting mechanism in Selenium

Waiting mechanism in Selenium and Java can be categorized as below:

Demonstrate a program which don't use waiting mechanism to understand the


importance of waiting mechanism in Selenium

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

public static void main(String args[]) {

WebDriver driver = new FirefoxDriver();

driver.get("https://iamsandesh23.github.io/selenium.github.io/");
driver.manage().window().maximize();

driver.findElement(By.className("dropbtn")).click();

driver.findElement(By.linkText("Facebook")).click();

Using Thread.sleep() in Java to overcome the waiting problems


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

public static void main(String args[]) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

driver.findElement(By.className("dropbtn")).click();

Thread.sleep(3000);

driver.findElement(By.linkText("Facebook")).click();
}

}
Implicit Wait - Instead of halting the program till the specified time is
reached, Implicit wait will wait for all the web elements dynamically (i.e.
Global wait)
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

public static void main(String args[]) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(5,
TimeUnit.SECONDS);

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

driver.findElement(By.className("dropbtn")).click();

driver.findElement(By.linkText("Facebook")).click();

}
Explicit Wait - Instead of waiting for all the statements in the program,
Explicit wait will wait only for the specific web element
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Demo {

public static void main(String args[]) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

WebDriverWait wait = new WebDriverWait(driver, 5);

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

driver.findElement(By.className("dropbtn")).click();

WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.l
inkText("Facebook")));

element.click();

WebDriverWait wait = new WebDriverWait(driver, 5);


WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Fac
ebook")));
element.click();

Also demonstrate 'ElementToBeClickable'

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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Demo {

public static void main(String args[]) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

WebDriverWait wait = new WebDriverWait(driver, 5);

driver.get("https://iamsandesh23.github.io/selenium.github.io/");

driver.manage().window().maximize();

driver.findElement(By.className("dropbtn")).click();

WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.linkT
ext("Facebook")));

element.click();

You might also like