0% found this document useful (0 votes)
30 views5 pages

Waiting Mechanism

The document discusses different waiting mechanisms in Selenium like using Thread.sleep(), implicit waits, and explicit waits. It provides code examples to demonstrate using each of these waiting mechanisms and how they help overcome issues with waiting for elements when automating tests without proper waits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Waiting Mechanism

The document discusses different waiting mechanisms in Selenium like using Thread.sleep(), implicit waits, and explicit waits. It provides code examples to demonstrate using each of these waiting mechanisms and how they help overcome issues with waiting for elements when automating tests without proper waits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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