Selenium C# Tutorial: Using Implicit Wait In Selenium C#

Himanshu Sheth

Posted On: March 20, 2020

view count78621 Views

Read time7 Min Read

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium C# Tutorial.

While designing test automation scripts, it is quite imperative that we consider all the major challenges that might come while performing Selenium test automation. There are certain scenarios where the test script is trying to perform an operation on an element that is not loaded (or not yet visible) and the test ends up failing due to this. This happens as the web elements on the page are loaded dynamically via AJAX (Asynchronous JavaScript and XML) and JavaScript or any other front-end framework.

To tackle this issue we have ‘Wait’, which is one of the most important and widely used commands in Selenium. It helps us to provide us ample wait or pause time in our script execution. Thereby ensuring that our scripts don’t fail while performing automation testing with Selenium.

In this section of our on-going Selenium C# tutorial series, we will talk about what are Selenium waits, why are they important, & how to implement an Implicit Wait in Selenium C# with examples.

What Are Waits In Selenium?

Selenium WebDriver is said to have a blocking API. It is an out-of-process library that instructs the web browser what exactly needs to be done. On the other hand, the web platform has an asynchronous nature. This is the primary reason why the Selenium WebDriver does not track the real-time state of the DOM (Document Object Model).

Race conditions could occur in a scenario where the developer uses a Selenium command to navigate to a particular URL (or web page) and gets a No Element Found error while trying to locate the element. To overcome such issues that can lead to race conditions between the web browser and the WebDriver script, the WebDriverWait class in Selenium C# can be used.

Waits in Selenium enable the test execution to be paused for a specified time (ideally in a few seconds) to address issues that can occur due to time lag. The added delay is a counter-mechanism to ensure that the particular web element is loaded before any action is performed on the element.

Take this certification to master the fundamentals of Selenium automation testing with C# and prove your credibility as a tester.

Here’s a short glimpse of the Selenium C# 101 certification from LambdaTest:

Why Are Selenium ‘Waits’ Important?

The front-end of most of the web applications are built on JavaScript or AJAX, using frameworks such as React, Angular, or others that take a certain time for the web elements to load entirely on the browser.

When Selenium comes across operations on web elements that are still loading, it throws the ElementNotVisibleException. Selenium waits can resolve such problems.

In the code snippet, the test scenario for our Selenium C# tutorial is for flight search on a flight booking website, which generates a ‘NoSuchElementException’ when the search operation is performed using Selenium test automation script.

As the page load is not complete i.e. the search for flights from source to destination is in progress, the script fails to find the ‘Book Now’ button. This results in the ‘NoSuchElementException’ for our Selenium test automation script.

selenium-csharp-tutorial

There are different types of waits in Selenium C# namely – Implicit wait, Explicit wait, and Fluent Wait. In this Selenium C# tutorial, we will have cover the Implicit wait in Selenium C#.

What Are Implicit Waits In Selenium C#?

Now moving ahead with our Selenium C# tutorial, Some developers use the System.Threading.Thread.Sleep(msecs) command to suspend the execution of the currently executing thread for a specified duration (specified in milliseconds). Usage of System.Threading.Thread.Sleep is considered to be a bad practice. The downside of this approach is that the Selenium WebDriver waits for irrespective of whether the web element is found or not. Usage of System.Threading.Thread.Sleep is considered to be a bad practice.

The shortcomings of the System.Threading.Thread.Sleep can be overcome using Implicit wait in Selenium C#. Implicit wait in Selenium halts the execution of the WebDriver for a specified duration of time until the desired web element is located on the page.

Unlike System.Threading.Thread.Sleep, the Implicit wait in Selenium does not wait for the complete time duration. Implicit wait in Selenium is also referred to as dynamic wait. If the particular web element is located before the expiry of the specified duration, it proceeds to execute the next line of code in the implementation.

If the particular web element is not located within the time duration, ElementNotVisibleException or NoSuchElementException is raised. Hence, implicit wait in Selenium tells the Selenium WebDriver to wait for a particular time duration (passed as a parameter), before the exception is raised.

The advantage of using implicit wait in Selenium C# is that it is applicable for all the web elements specified in the Selenium test automation script till the time WebDriver instance (or IWebDriver object) is alive.

Syntax of implicit wait in Selenium C#

The default time value for the implicit wait is zero. Implicit wait polls for the presence of the web element every 500 milliseconds. Now to take our Selenium C# tutorial further, we move on to the features of implicit wait in Selenium test automation.

When To Use Implicit Wait In Selenium?

Though there are different types of Selenium waits (explicit wait and fluent wait), there are some key features that differentiate implicit wait in Selenium from other types of waits.

Example of Implicit Wait in Selenium C#

To demonstrate implicit wait in Selenium C#, we take the same example of EaseMyTrip. The major difference is that we have added an implicit wait of 30 seconds.

As an implicit wait is added, a wait of 30 seconds is added to locate the ‘Book Now’ button.

Due to this wait, the page load gets completed and we proceed with the flight ticket booking for our Selenium C# tutorial.

Also read: Selenium Waits: Implicit, Explicit, Fluent And Sleep

Conclusion

In this Selenium C# tutorial, we had a look at why implicit wait in Selenium is necessary for Selenium test automation. The primary usage of Implicit wait in Selenium is to add a certain amount of delay of loading of the required web elements before an operation is performed on the element. That’s all for this article.

In case you haven’t gone through the previous article of this series on NUnit Testing, I’d urge you to do so. I will see you in the next tutorial of this Selenium C# tutorial series where I’ll show you how to use explicit wait in Selenium C#. Until next time. Happy testing!

Update: We’ve now completed the Selenium C# tutorial series, so in order to help you easily navigate through the tutorials we’ve compiled the list of tutorials.

Selenium C# Tutorials With Examples

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 128



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free