Wait Commands in Selenium or Synchronization commands in Selenium Webdriver

In Selenium, one of the challenges of web application testing is dealing with the dynamic nature of web pages. Web pages can take time to load, and elements may only appear after some time.
Types of wait Selenium: Selenium provides various wait mechanisms to help wait for an element to appear, disappear, or be clickable. These wait mechanisms can be classified into three types: implicit wait, explicit wait, fluent wait, pageLoadTimeout, and setScriptTimeout
 
 
Selenium wait commands or Synchronazation commands in 
==================Selenium 4 Version==================
 
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
 
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Selenium Quiz")));
 
 
 Wait<WebDriver> wait = new org.openqa.selenium.support.ui.FluentWait<WebDriver>(driver)
        .withTimeout(Duration.ofSeconds(50))
        .pollingEvery(Duration.ofMilliSeconds(2))
        .ignoring(NoSuchElementException.class);
wait.until(
        d -> {
        driver.findElement(By.linkText("Selenium")).click();    
          return true;
        });
 
driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2));
 
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
 
robot.delay(900);
 
Thread.sleep(10000);
 
==================Selenium 3 Version==================
 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
WebDriverWait wait=new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
 
driver.manage().timeouts().setScriptTimeout(2, TimeUnit.MINUTES);
 
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
 
Wait<WebDriver>  wait = new FluentWait<WebDriver>(driver)
.withTimeout(50, SECONDS)
.polingEvery(5, SECONDS)
.ignoring(NoSuchElemntException.class);

 

 

Examples :-

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;

    @Test
    public void threadSleep() throws Exception {
        driver.get("http://www.seleniumlearn.com");
        Thread.sleep(9000);
        driver.findElement(By.linkText("Java")).click();
    }

@Test
    public void implicitlyWait() {
        driver.get("http://www.seleniumlearn.com");
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.findElement(By.linkText("Java")).click();
    }

@Test
    public void explicitwaits() {
        driver.get("http://www.seleniumlearn.com/selenium");
        WebDriverWait wait=new WebDriverWait(driver,9);
        wait.until(ExpectedConditions.elementToBeClickable(By.id("edit-search-block-form--2")));

        driver.findElement(By.id("edit-search-block-form--2")).sendKeys("selenium");
    }

    @BeforeTest

    public void beforeTest() {
        driver=new FirefoxDriver();
        //System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
        //driver = new ChromeDriver();
        driver.manage().window().maximize();

    }

}

Implicit Wait:
An implicit wait is a global setting that applies to all elements in a Selenium script. It waits a specified time before throwing an exception if the element is not found. We can set the wait once per session and can’t change it later. The default value is 0.

We can set the implicit wait to 10 seconds with the following statement. We should set the implicit wait right after initializing the WebDriver instance:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

With implicit wait, we don’t need to explicitly wait for anything in our tests.

Explicit Wait :

Explicit Waits also known as Dynamic Waits because it is highly specific conditioned and more flexible wait that allows us to wait for a specific condition to be met before continuing test execution.

It is implemented by WebDriverWait class.

An explicit wait We can define the condition, such as the presence or absence of an element, using ExpectedConditions class. An exception is thrown if the condition is not met within the specified time.

In order to declare explicit wait, one has to use ExpectedConditions. The following Expected Conditions can be used in Explicit Wait.

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()

To use Explicit Wait in test scripts, import the following packages into the script.

import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
Then, Initialize A Wait Object using WebDriverWait Class.

Explicit Wait Syntax

WebDriverWait wait = new WebDriverWait(driver,30);
Here, the reference variable is named <wait> for the <WebDriverWait> class. It is instantiated using the WebDriver instance. The maximum wait time must be set for the execution to layoff. Note that the wait time is measured in seconds.

First, we’ll create a Wait instance with a timeout of 10 seconds for our test:

WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("techlearn.in")));

Now we can use this Wait instance and call until() with the ExpectedConditions. In this scenario, we can use ExpectedConditions.visibilityOfElementLocated(By.xpath("webelement");

 

Thread.sleep(10000)

Thread.sleep() method will tell the webdriver to be stable for a particular time which u will pass in the sleep method..eg, thread.sleep(10000).This will tell webdriver to wait for 10 seconds,whereas implicit wait will continue to execute the next line if element is present, or will wait until 9seconds to find the element. If not it will  through an error (element not found exception,waiting for nine seconds)