November 23

0  comments

What is Wait command in Selenium?

The wait functions are essential when it comes to executing Selenium tests. They help to observe and troubleshoot issues that may occur due to variation in time lag.
This is achieved by refreshing the entire web page and re-loading the new web elements. At times, there can be Ajax calls as well. Thus, a time lag can be seen while reloading the web pages and reflecting the web elements.
Users are often found navigating through various web pages back and forth. Thus, navigate() commands/methods provided by the WebDriver helps the user to simulate the real time scenarios by navigating between the web pages with reference to the web browser’s history.



Why do we need waits in Selenium?

Most of the web applications are developed using Ajax and Javascript. When a page is loaded by the browser the elements which we want to interact with may load at different time intervals.

 

With this, it not only becomes difficult to identify the element but also if the element is not located, it will throw an “ElementNotVisibleException” exception. 


By using Waits, we can resolve this problem. The time gap that is required for web page to get load can be achieved using wait and in this way, we are not getting stuck in “ElementNotVisibleException” and our test scripts will not fail or stop.

Types of Waits

Selenium supports two types of waits and they are as follows
1. Implicit Wait
2. Explicit Wait


The most widely used waits are Implicit and Explicit waits.



Implicit wait in Selenium

The Implicit Wait in Selenium is used to tell the web driver to wait for a certain amount of time before it throws a "No Such Element Exception". The default setting is 0. Once we set the time, the web driver will wait for the element for that time before throwing an exception.


The key point to note here is, unlike Thread.sleep(), it does not wait for the complete duration of time. In case it finds the element before the duration specified, it moves on to the next line of code execution, thereby reducing the time of script execution.

Therefore, Implicit wait is also referred to as dynamic wait. If it does not find the element in the specified duration, it throws “ElementNotVisibleException”.


Syntax to implicit wait: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


Let us now check how we can write code for implicit waits


Scenario where we can use Implicit wait in selenium webdriver.


Let us consider scenario where we have a grocery searching website where we need to search for a product and add to cart and later need to place order for item that we have searched, usually when we search something on website it takes some seconds to load search results and also when we move from one page to another website it takes some seconds to load and before the page is getting loaded completely webdriver will try to click on products element that we need to add to cart, in such situation we can use implicit wait and provide a global wait that can be applied for every search event or page loading event on website.


In the above code, we have given Implicit Wait as 10 seconds, which implies the maximum wait time is 10 seconds for the element to load or to arrive at the output.
Implicitly wait is applied globally which means it is always available for all the web elements throughout the driver instance. It implies if the driver is interacting with 100 elements then, implicitly wait is applicable for all the 100 elements.

Explicit wait in Selenium

The Explicit Wait in Selenium is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or maximum time exceeded before throwing "ElementNotVisibleException" exception. 

It is an intelligent kind of wait, but it can be applied only for specified elements. It gives better options than
implicit wait as it waits for dynamically loaded Ajax elements. Once we declare explicit wait we must use "ExpectedConditions"


To declare explicit wait, one must 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()


Few scenarios where we can use Explicit wait in selenium web driver.


Condition 1 – Suppose I have a web page which has some login form and after login, it takes a lot of time to load Account page or Home page. This page is dynamic it means sometimes it takes 10 seconds to load the homepage, sometimes it is 15 second and so on. In such situations, Explicit wait helps us to wait until a specific page is not present.
Condition 2 – You are working on hotel reservation application and you have filled a web form and clicked on the submit button. Now you must wait until the specific data is not displayed. In this case, again you can use Explicit wait in which you can give waits till specific or set of elements are not displayed.


Syntax to explicit wait: 


WebDriverWait wait=new WebDriverWait(WebDriveReference,TimeOut);


In the above syntax, I have created an object of WebDriver wait and passed driver reference and timeout as parameters. This is how you need to write Explicit Waits.


Let us now check how we can write code for explicit waits.



In the above example I have declared WebDriverWait with 20 seconds and later used same explicit wait to wait for text box to get load and then sending value into that text box. 

In this way we can provide explicit wait and execute our test scripts without getting into “ElementNotVisibleException”. In this way we can use explicit waits.

Difference between Implicit and Explicit wait.

Implicit Wait

  • Applies to all elements in a test script.
  • No need to specify “ExpectedConditions” on the element to be located
  • Most effective when used in a test case in which the elements are located with the time frame specified in implicit wait

Explicit Wait

  • Applies only to specific elements as intended by the user.
  • Must always specify “ExpectedConditions” on the element to be located
  • Most effective when used when the elements are taking a long time to load. Also useful for verifying property of the element such as visibilityOfElementLocated, elementToBeClickable,elementToBeSelected

Fluent Wait in Selenium

The Fluent Wait in Selenium is used to define maximum time for the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.


It checks for the web element at regular intervals until the object is found or timeout happens.


Frequency: Setting up a repeat cycle with the time frame to verify/check the condition at the regular interval of time.


Scenario where we can use fluent wait in selenium web driver:


Let us consider a scenario where an element is loaded at different intervals of time based on network speed or trying to fetch results from data base. The element might load within 10 seconds, 15 seconds or even more then that if we declare an explicit wait of 20 seconds.


 It will wait till the specified time before throwing an exception. In such scenarios, the fluent wait is the ideal wait to use as this will try to find the element at different frequency until it finds it, or the final timer runs out.


Syntax to Fluent wait:


Wait wait = new FluentWait(WebDriver reference).withTimeout(Duration.ofSeconds(SECONDS)) .pollingEvery(Duration.ofSeconds(SECONDS)).ignoring(Exception.class);


Code for Fluent Wait:

Let us now check how we can write code for fluent waits.



In the above example, we are declaring a fluent wait with the timeout of 30 seconds and the frequency is set to 5 seconds by ignoring "NoSuchElementException".



Implicit, Explicit and Fluent Wait are the different waits used in selenium. Usage of these waits are totally based on the elements which are loaded at different intervals of time.

 

It is always not recommended to use Thread.Sleep() while Testing our application or building our framework.


I hope this article will be useful. Please let me know your feedback in the comments section.


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}