Wait commands in Selenium - Implicit - Explicit - Fluent Wait

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")));
 
driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2));
 
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
 
 
 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;
        });
 
robot.delay(900);
 
Thread.sleep(10000);
 
 
==================Selenium 3==================
 
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);

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 is a 
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.

The polling frequency of the WebDriver to check the expected condition is fixed at 500 ms. As the explicit wait is not set globally, we can use different conditions and timeouts for everything.

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