Introduction

In this article, we will look at how to handle waits using selenium with C#.

Pre-requisite

C# Test Automation project setup needs to be done.

When we develop Test automation scripts and run and see them on our machine, sometimes we may face synchronization issues. Some developers/testers use default wait which is thread.sleep() for a specific action to occur at a certain step in their code. This is bad practice to handle synchronization. So, to handle these synchronization issues, we use the concept of waits.

Handling waits in Selenium using C# is crucial for ensuring that your automated tests interact with web elements at the right time, especially in cases where elements might take some time to appear or change their state.

Implicit Wait in Selenium

Implicit waits are set globally for the entire duration of your WebDriver instance.

They tell the WebDriver to wait for a certain amount of time before throwing an exception.

This is a global setting that applies to every element location call for the entire session. The default value is 0, which means that if the element is not found, it will immediately return an error. If an implicit wait is set, the driver will wait for the duration of the provided value before returning the error. Note that as soon as the element is located, the driver will return the element reference, and the code will continue executing.

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

Explicit Wait in Selenium

Explicit waits allow you to wait for a certain condition to occur before proceeding further in the code.

Explicit waits are also known as dynamic waits because it is highly specific condition. It is implemented by the WebDriverWait class. To understand why you need Explicit Wait in Selenium, you must go through the basic knowledge of the wait statements in a program. In simple terms, you must know some conditions.

The conditions are elements to be clickable and visible.

Some of the Conditions

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("elementId")));

Fluent waits in Selenium

Fluent waits are similar to explicit waits but offer more flexibility.

Fluent Wait is quite similar to explicit Wait. It is similar in terms of management and functioning.

In Fluent Wait, you can perform wait for action for an element only when you are unaware of the time it might take to be clickable or visible.

You can configure them with polling intervals and ignore specific exceptions.

DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver);
fluentWait.Timeout = TimeSpan.FromSeconds(30);
fluentWait.PollingInterval = TimeSpan.FromSeconds(2);
fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
IWebElement element = fluentWait.Until(ExpectedConditions.ElementIsVisible(By.Id("elementId")));

Hard Wait in Selenium

While not the best practice, you can use Thread.Sleep to introduce a static wait. However, it is generally discouraged because it can lead to unnecessarily long test execution times.

Thread.Sleep(5000); // Sleep for 5 seconds

Difference between Implicit and Explicit

Summary

I hope you have good knowledge of handling waits using selenium with C#. So, use it whenever it is required for a particular condition.

Thanks.

Happy learning :)