Share

How to use wait commands in Selenium WebDriver in Java ?

Author: neptune | 22nd-Feb-2022 | views: 888
#Selenium #Testing #Java

What are Wait commands in Selenium?

In automation testing waits are used to halt or pause the execution of test cases for a certain amount of time before throwing an exception or moving to the next step.

Why do we need Selenium Wait commands?

The waits are essential when it comes to automation testing. In automation testing, sometimes the application doesn't work as we expect. In such scenarios we want the driver to wait for a certain amount of time before throwing an exception.
To fulfill the purpose we have different types of waits commands.

Selenium provides these three types of waits.

  1. Implicit Wait

  2. Explicit Wait

  3. Fluent Wait

Let’s explore these waits in detail.






1. Implicit Wait

Implicit wait is a globally declared wait which means it will apply for each element in the entire duration for which the browser is open.


To implement implicit wait, import the following package.

import java.util.concurrent.TimeUnit;

Syntax:

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


Above line will implement implicit wait for 10 seconds for each element.

2. Explicit Wait

Explicit wait is used for specific conditions to fulfil before proceeding to another element or step. When we are not aware about the time element will take to load.

Explicit wait is more intelligent, but can only be applied for specified elements.


1.To implement Explicit wait, import the following package.
import org.openqa.selenium.support.ui.ExpectedConditions

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


2. Then initialise the wait object.

WebDriverWait wait = new WebDriverWait(driver,30);


3.  Apply a wait object for the Submit button to clickable.

WebDriverWait wait = new WebDriverWait(driver,30);

wait.until(ExpectedConditions.

visibilityOfElementLocated

(By.xpath("//div[contains(text(),'Submit')]")));


Above code will implement Explicit wait for 30 seconds for the submit button to be clickable.






3.Fluent Wait

In Fluent Wait WebDriver waits for a certain time till the web element becomes visible. It also defines how frequently WebDriver will check if the condition appears before throwing the “ElementNotVisibleException”.


It checks web elements repeatedly at regular intervals until timeout happens or until the element is found. It is commonly used in Ajax applications. We can set a default polling period as needed.


Syntax:

Wait wait = new FluentWait(WebDriver reference)

.withTimeout(timeout, SECONDS)

.pollingEvery(timeout, SECONDS)

.ignoring(Exception.class);

Example of Fluent Wait Command

1.Declare and initialise a fluent wait

FluentWait wait = new FluentWait(driver);

//Specify the timeout of the wait

wait.withTimeout(600, TimeUnit.MILLISECONDS);

//Specify polling time

wait.pollingEvery(50, TimeUnit.MILLISECONDS);

//Specify what exceptions to ignore

wait.ignoring(NoSuchElementException.class)

//This is how we specify the condition to wait on.

wait.until(ExpectedConditions.alertIsPresent());


Thanks for Reading ! 
If you have any query Please let me know in comment section.





anonymous | March 20, 2022, 11:23 p.m.

Really good explanation. Add more about selenium automation....



Related Blogs
Automate Ticket Booking in SpiceJet from Delhi to Bengaluru using Selenium and Cucumber.
Author: neptune | 17th-May-2022 | views: 4369
#Selenium #Cucumber
We are going to automate a ticket booking using Selenium WebDriver and Cucumber BDD...
Selenium, Cucumber, JUnit, TestNG dependencies for Selenium project.
Author: neptune | 25th-May-2022 | views: 1576
#Selenium #Testing #Cucumber #Projects
We guide you how to update the pom.xml file for Selenium Maven project...
Top 50+ Selenium Interviews Questions 2022 based on Years of Experience
Author: neptune | 31st-May-2022 | views: 823
#Selenium #Testing #Interview
Every interview difficulty is based on how many years of experience you have in that field. For the Selenium Automation Tester I have divided the question on the number of years of experience...
Different types of software testing ?
Author: neptune | 27th-Jul-2022 | views: 761
#Testing #IT
Testing is the process to improve the performance of software. Examples: Unit, Integration, Regression, Smoke, Alpha, Beta, System, Stress Performance, Object-Oriented Testing etc...
Top 10 Selenium Interview Questions with answers (2021).
Author: neptune | 23rd-Feb-2022 | views: 724
#Selenium
In this article I will cover top 10 Selenium interview questions...
Mostly asked Cucumber interview questions in selenium automation interviews.
Author: neptune | 28th-May-2022 | views: 631
#Selenium #Testing #Cucumber
We are going to explore widely asked Cucumber interview questions in selenium automation interviews...
25 Basic Java interview questions.
Author: neptune | 30th-May-2022 | views: 468
#Interview #Java
We will explore 25 basic Java interview questions...
How to create Selenium Cucumber Project in Eclipse.
Author: neptune | 25th-May-2022 | views: 362
#Selenium #Cucumber
First, divide the process into the various steps to understand working in brief of a project. Steps in brief: We’ll start with initializing the browser driver and then log in to the web page...
Best way to reverse a String for beginners in Java.
Author: neptune | 08th-May-2022 | views: 330
#Java
We will explore How to reverse a String in Java using simple way. If you are beginner in Java then you are at the right place...
Getting started with Selenium WebDriver.
Author: neptune | 15th-Apr-2022 | views: 262
#Selenium #Testing
In this blog I will give you an overview of Selenium WebDriver and Also discuss about advantages and disadvantages of Selenium WebDriver...
How to create a Selenium, Cucumber Automation project in Eclipse ?
Author: neptune | 19th-Apr-2022 | views: 244
#Selenium #Testing #Cucumber
Problem Statement : Let’s consider this particular project where we will try automating the process of booking a flight ticket. Let’s get started and see how it’s done using Selenium...
Roadmap To Become Test Automation Engineer
Author: neptune | 25th-Jun-2022 | views: 240
#Testing
When I was starting my journey in test automation, a lot of questions comes to my mind. Such as…. “From where do I start?” “Best tools to use?” “Which Programming language?” “How to plan your work?”...
Top 10 Selenium Interview Questions.
Author: neptune | 03rd-Jun-2022 | views: 155
#Selenium #Interview
Locator is a command that tells Selenium IDE which GUI elements (like Text Box, Buttons, Check Boxes etc) we are going to use or perform some automation task...
Why we use Robot Class in Automation Testing?
Author: neptune | 23rd-Aug-2022 | views: 90
#Selenium
Robot class is used to generate native system input events for the purpose of automation testing. It is used to perform mouse and keyboard events on windows applications or popups...
Difference b/w String, StringBuffer, and StringBuilder
Author: neptune | 23rd-Sep-2022 | views: 17
#Java
String is a sequence of characters in Java. String is a class in the java.lang package. String objects are immutable in nature...
View More