ExplicitWait’s newly introduced method ‘Duration.of()’ in Selenium 4

ExplicitWait’s​​ newly introduced method ‘Duration.of()​​ in Selenium​​ 4​​ 

What you will Learn:

  • Newly introduced​​ Duration.of()​​ method in Explicit Wait (Selenium 4)

  • Code snippets

Newly introduced Duration.of() method in Explicit Wait (Selenium 4)​​ 

We use WebDriverWait to explicitly wait for an element to load on a page.​​ 

Before we practically see the enhanced Explicit Wait approach​​ in Selenium​​ 4,​​ let us quickly see the older approach in Selenium 3.​​ 

Let us automate a simple use case:​​ 

Let us inspect the link ‘LIFETIME MEMBERSHIP TO ALL LIVE TRAININGS’



Below is the code snippet for Selenium 3
.​​ Notice that the method is deprecated in selenium 4, but we can still use it.​​ In the old approach, we used to write (driver, <timeInSeconds>)

//Old Approach (Selenium 3)

 WebElement elem1 =​​ new​​ WebDriverWait(driver, 10) .until(ExpectedConditions.elementToBeClickable(By.linkText("LIFETIME MEMBERSHIP TO ALL LIVE TRAININGS")));

 System.out.println(elem1.getText());

  elem1.click();

Below is the complete code with older approach:

Let us run this.

Below is the console log that prints the link’s text

Also notice that selenium clicks the link ‘LIFETIME MEMBERSHIP TO ALL LIVE TRAININGS’

Let us now look at the new approach. In the new approach, notice below that we have Duration.ofSeconds() method:

//New Approach (Selenium​​ 4)

 WebElement elem1 =​​ new​​ WebDriverWait(driver,​​ Duration.ofSeconds(10)) .until(ExpectedConditions.elementToBeClickable(By.linkText("LIFETIME MEMBERSHIP TO ALL LIVE TRAININGS")));

 System.out.println(elem1.getText());

  elem1.click();

There are many other options available, example,​​ ofDays, ofHours, ofNanos​​ etc as can be seen below

So let us comment the old approach​​ 

Run the script, we would get the same output as we saw with old approach.

Let us intentionally introduce an error in the linkText so that the test case fails​​ 

Save and run the test.

Notice below that Selenium waits for 10 seconds for the desired element to load on webpage.​​ When it does not find the expected element,​​ TimeoutException​​ is thrown

See the console log

Code snippet​​ 

package​​ sel4scripts;

 

import​​ java.time.Duration;

import​​ org.openqa.selenium.By;

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.WebElement;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ org.openqa.selenium.support.ui.ExpectedConditions;

import​​ org.openqa.selenium.support.ui.WebDriverWait;

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ ExplicitWaitSel4 {

 

public​​ static​​ void​​ main(String[]​​ args) {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

driver.get("https://www.selenium-tutorial.com/courses");

/*

 //Old Approach (Selenium​​ 3)

 WebElement elem1 = new WebDriverWait(driver, 10)

 .until(ExpectedConditions.elementToBeClickable(By.linkText("LIFETIME MEMBERSHIP TO ALL LIVE TRAININGS")));

 System.out.println(elem1.getText());

 elem1.click();

 */

 ​​ //New Approach (Selenium​​ 4)

 WebElement​​ elem1​​ =​​ new​​ WebDriverWait(driver, Duration.ofSeconds(10))

 .until(ExpectedConditions.elementToBeClickable(By.linkText("LIFETIME MEMBERSHIP TO ALL LIVE TRAININGS")));

 System.out.println(elem1.getText());

elem1.click();

 }

 

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024