DevLabs Alliance - WhatsApp
DevLabs Alliance Logo

Selenium WebDriver is a platform-independent tool used to automate testing of web applications to verify that it operates as demanded. Selenium supports many different browsers such as Firefox, IE, Chrome, and Safari. However, this tool does not qualify for applications based on the windows.


It also helps in supporting many different programming languages such as Java, Perl, C#, PHP, and Ruby for writing the test scripts.

The Wait Commands in Selenium

The wait commands are crucial when there is a need to execute Selenium tests. In addition, waits help recognize and troubleshoot the issues that may occur due to changes in time lag. For example, when the browser loads a page, then there is a possibility that the elements with which we want to interact with may load at different periods.


Hence, it becomes difficult to recognize the element but also if the component is not determined, it will show the “ElementNotVisibleException” exception. Using Selenium Waits, this problem can be resolved very quickly.

Different Waits of Selenium Web Driver

❖    Implicit Wait in Selenium

The Implicit Wait in Selenium tells the Selenium WebDriver to wait before throwing “No Such Element Exception” for a specific amount of time.


  • Implicit Wait time applies to all the elements in the script
  • There is no need to define “ExpectedConditions” on the element to be identified in the implicit wait
  • It is advised to use when the elements are identified with the time frame specified in Selenium implicit wait


For example, if we declare an implicit wait with a time frame of 10 seconds and the element is not found on the web page within that period, it will throw an exception.


Import the following package to add implicit waits in test scripts:


import java.util.concurrent.TimeUnit;

Implicit Wait Syntax:

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

Example of Implicit Wait:

package com.dla;

import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

public class ImplicitWaitTest {

protected WebDriver driver; @Test

public void dlaImplicitWaits() throws InterruptedException

{

System.setProperty (“webdriver.chrome.driver”,”.\\chromedriver.exe” ); driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ; String expectedTitle = “DevLabs Alliance”;

String actualTitle = “” ;

// launch Chrome and redirect it to the Base URL driver.get(“https://www.devlabsalliance.com/” );

//get the actual value of the title actualTitle = driver.getTitle();

//compare the actual title with the expected title

if (actualTitle.equals(expectedTitle))

{

System.out.println( “Test Passed”) ;

}

else {

System.out.println( “Test Failed” );

}

//close browser driver.close();

}

}

👉What is Selenium Testing?

👉How do I automate in Selenium?


❖    Explicit Wait in Selenium


The Explicit Wait in Selenium tells the web driver to wait for particular conditions (Expected Conditions) or wait for maximum time surpassed before throwing the “ElementNotVisibleException” exception. The explicit wait is an intelligent type, but it can be applied only to certain elements. It has better scope than implicit wait as it waits for dynamically loaded Ajax elements.


Once an explicit wait is indicated, we must use “ExpectedConditions” or configure how frequently we check the condition using Fluent Wait.


  • Explicit Wait time is utilized only to those elements which we intend.
  • There is a need to specify “ExpectedConditions” on the element to be identified in explicit wait.
  • It is suggested to use when the elements take a long time to load and also to verify and check the property of the element like(visibility of element located or part to be clickable)


Different types of Expected Conditions which can be used in Selenium Explicit Wait are


  1. elementToBeSelected()
  2. alertIsPresent()
  3. elementSelectionStateToBe()
  4. elementToBeClickable()
  5. invisibilityOfElementWithText()
  6. presenceOfElementLocated()
  7. textToBePresentInElement()
  8. invisibilityOfTheElementLocated()
  9. presenceOfAllElementsLocatedBy()


Import the following packages into the script to use Explicit Wait in test scripts:


import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait

Explicit Wait Syntax:

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);

Example of Explicit Wait:

package com.dla;

import java.util.concurrent.TimeUnit; 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 org.testng.annotations.Test;

public class ExplicitWaitTest { protected WebDriver driver; @Test

public void dlaExplicitWaits() throws InterruptedException

{

System.setProperty (“webdriver.chrome.driver”,”.\\chromedriver.exe” ); driver = new ChromeDriver();

WebDriverWait wait=new WebDriverWait(driver, 20); String expectedTitle = “DevLabs Alliance”; String actualTitle = “” ;

// launch Chrome and redirect it to the Base URL driver.get(“https://www.devlabsalliance.com/” );

//get the actual value of the title actualTitle = driver.getTitle();

//compare the actual title with the expected title

if (actualTitle.contentEquals(expectedTitle))

{

System.out.println( “Test Passed”) ;

}

else {

System.out.println( “Test Failed” );

}

WebElement dlaWaitLink; dlaWaitLink=

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( “//*[@id=\”menu- item-2851\”]/a”)));

dlaWaitLink.click();

}

}


👉Advanced Selenium Interview Questions

👉Top 50 Selenium Interview Questions for SDET

Fluent Wait in Selenium

The Fluent Wait in Selenium defines the maximum time for the web driver to wait for a condition and the number with which we want to verify the condition before throwing an “ElementNotVisibleException” exception. It monitors the web element at fixed intervals until the object is discovered or a timeout occurs.


In the fluent waita repeat cycle is set up with the time frame to verify or check the condition at fixed time intervals.


Consider a situation when an element loads at different intervals of time. For example, suppose an element in the web page sometimes loads in 5 seconds, sometimes in 10 seconds, or sometimes even takes 30 seconds. In such cases, the fluent wait is the ideal wait to use, as this will try to locate the element at different frequencies until it recognizes it or the final timer elapses.

Fluent Wait Syntax:

Wait wait = new FluentWait(WebDriver reference)

.withTimeout(Duration.ofSeconds(SECONDS))

.pollingEvery(Duration.ofSeconds(SECONDS))

.ignoring(Exception.class);

Example of Fluent Wait:

package com.dla;

import org.testng.annotations.Test; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; import java.util.function.Function;

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.FluentWait;

import org.openqa.selenium.support.ui.Wait;

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

import org.testng.annotations.Test;

public class FluentWaitTest { protected WebDriver driver; @Test

public void dlaFluentWait() throws InterruptedException

{

System.setProperty (“webdriver.chrome.driver”,”.\\chromedriver.exe” ); String expectedTitle = “DevLabs Alliance”;

String actualTitle = “” ; driver = new ChromeDriver();

// launch Chrome and redirect it to the Base URL driver.get(“https://www.devlabsalliance.com/” );

//get the actual value of the title actualTitle = driver.getTitle();

//compare the actual title with the expected title

if (actualTitle.contentEquals(expectedTitle))

{

System.out.println( “Test Passed”) ;

}

else {

System.out.println( “Test Failed” );

}

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

.withTimeout(30, TimeUnit.SECONDS)

.pollingEvery(5, TimeUnit.SECONDS)

.ignoring(NoSuchElementException.class);

WebElement dlaWaitLink = wait.until(new Function<WebDriver, WebElement>(){

2851\”]/a”));

public WebElement dla(WebDriver driver ) {

return driver.findElement(By.xpath(“//*[@id=\”menu-item-

}

});

dlaWaitLink.click();

//close browser driver.close() ;

}

}

Conclusion

Hence, waits in selenium are necessary to execute the tests. Implicit, Explicit, and Fluent Wait are the various waits used in Selenium. Using these waits depends on the elements, which are loaded at different time intervals. Unlike Implicit waits, Explicit waits are used for a particular situation only. Therefore, Waits enables the user to diagnose the issues or problems while redirecting to different web pages.


👉Selenium Architect Certification Training

👉Selenium with Python Certification Training

👉Selenium Professional Training

👉Certified Selenium with C# Professional Training

Know Our Author

DevLabs Alliance Author Bio

Admin


DevLabs Alliance TwitterDevLabs Alliance LinkedInDevLabs Alliance Instagram

Author Bio

DevLabs Alliance conducts career transformation workshops & training in Artificial Intelligence, Machine Learning, Deep Learning, Agile, DevOps, Big Data, Blockchain, Software Test Automation, Robotics Process Automation, and other cutting-edge technologies.

INQUIRY

Want To Know More


Email is valid



Phone


By tapping continuing, you agree to our Privacy Policy and Terms & Conditions

“ The hands-on projects helped our team put theory into practice. Thanks to this training, we've achieved seamless collaboration, faster releases, and a more resilient infrastructure. ”
DevLabs Alliance Blogs Page Review
Vijay Saxena

SkillAhead Solutions

DevLabs Alliance Footer section
DevLabs Alliance LinkedIn ProfileDevLabs Alliance Twitter ProfileDevLabs Alliance Facebook ProfileDevLabs Alliance Facebook Profile
DevLabs Alliance Logo

USA

1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA

DevLabs Alliance ISO 9001

DevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer SectionDevLabs Alliance Footer Section

`Copyright © DevLabs Alliance. All rights Reserved`

|

Refund & Reschedule Policy

Privacy Policy

Terms of Use