Handle Waits in Selenium With C#

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

  • visibility_of_element_located
  • presence_of_element_located
  • title_is
  • visibility_of
  • element_selection_state_to_be
  • presence_of_all_elements_located
  • element_located_to_be_selected
  • alert_is_present
  • element_located_selection_state_to_b e
  • staleness_of
  • element_to_be_clickable
  • invisibility_of_element_located
  • frame_to_be_available_and_switch_to _it
  • text_to_be_present_in_element_value
  • text_to_be_present_in_element
  • element_to_be_selected
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

  • The implicit wait is asked to wait for a specific amount of time for the element to be available on the DOM of the page.
  • Explicit wait is asked to wait till a certain condition is satisfied.
  • Implicit wait is applied to all elements on the webpage.
  • The explicit wait is not a global wait and is applied to a particular scenario.
  • Implicit wait does not require you to meet any conditions.
  • Explicit wait is required to satisfy a particular condition.

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 :)


Similar Articles