Implicit Wait in Selenium C#

In this tutorial, we will discuss different types of Wait Mechanism in Selenium Web Driver in C#.

Wait in Selenium Web Driver C#

The wait mechanism plays a very important role while writing the automation script. It is possible that we might need to wait for a certain element while performing the action.

If the wait logic is not proper or if the proper wait is not introduced then in that case there are chances that the script will fail with “TimeoutException” or “NoSuchElementException”.

Types of Wait in Selenium C#

So there are two types of wait in web driver that are

  • Implicit Wait
  • Explicit Wait
Implicit Wait:

In the case of an implicit wait once it is set, it is going to be applied wherever you refer to the driver object, and also the limitation with this is that you cannot define any additional logic or condition for the wait.

Explicit Wait:

On the other hand, the explicit wait is much more flexible, it can be applicable for a certain block of code, and also you can define an additional logic or condition for this wait.

Using Implicit Wait in Selenium C#

Here today we learn about the implicit wait and we will discuss two methods

  • SetPageLoadTimeout()
  • ImplicitlyWait()

So in our visual studio, we will go to the IWebDriver Interface we have a method called Manage() which will give us IOptions interface.

If we go inside this there is a method called Timeouts() which will return us the ITimeouts interface if we go inside that those are the methods which we are going to use.

So the first method ImplicitlyWait will control the load time of the element and the second method SetPageLoadTimeout will control the page load time.

So the hierarchy of calling this method is the same first using the driver property we need to call Manage() after that we need to call Timeouts() and after that, we need to call these two methods.

SetPageLoadTimeout() :

So here we are going to add a directory called WebDriverWait and inside this, we will add a class called TestWebDriverWait, inside this create a method named as TestWait.

public class TestaWebDriverWait 
public void TestWait()
ObjectRepository.Driver.Manage().Timeouts(). SetPageLoadTimeout(TimeSpan.FromSeconds(40));

In order to call the method, we are going to use ObjectRepository.Driver.Manage().Timeouts(). SetPageLoadTimeout() and here we need to specify the duration.

Whenever we launch the browser our web driver will wait for a certain duration until the page loads completely.

If the duration for loading the page is more than the value specified, here it is going to throw us TimeoutException otherwise it is going to wait till the page load so in the above program we have specified 40 seconds.

So whenever we load a page inside the browser it will wait for 40 seconds before throwing us the TimeoutException.

If the page is loaded completely in less than 40 seconds that the flow will be normal, and if the page is taking more than 40 seconds to load it is going to throw us a TimeoutException.

In NavigationHelper.NavigateToUrl(""); we are specifying the webpage URL. Now put the breakpoint and build our solution and then run the script in debug mode.

Output: so it has hit the breakpoint if we do a stepover it is going to set the page load Timeout to 40 seconds, and if we again do a stepover it is going to open the webpage inside the browser.

So if the page loading time is more than 40 seconds it is going to throw us the TimeoutException otherwise if it’s less than 40 seconds it will go with the normal flow.

So as we can see here the WebDriver is still waiting for its page to load completely and once it is done we can see there it continues its normal flow.

To test the failure case we are setting the page load Timeout value very less, in the previous program where we have given 40  replace it with 1 which is very less for that webpage, and again we will run the script in debug mode.

ObjectRepository.Driver.Manage().Timeouts(). SetPageLoadTimeout(TimeSpan.FromSeconds(1));

Output: so it has hit the breakpoint and if we do a stepover it is going to set the Timeout value to 1 second and if we again do a stepover it is going to launch the page but this time we will get the exception.

So as we can see here it is throwing us as the exception WebDriverTimeoutException and the reason behind it is the same that the Timeout property is set to 1 second and the page is taking more than 1 second to load.

Now we will add a configuration inside the App.config file to specify the default page load Timeout value. Inside our App.config file we will add one more configuration the key will be key=”PageLoadTimeout” and value=”40″ seconds.

<add key="PageLoadTimeout" value="40" />

After that, we need to add this key to our AppConfigKeys class  so go to Settings -> AppConfigKeys inside this add

public const string PageLoadTimeout="PageLoadTimeout";

For this, we are going to add a method inside the interface. So the return type of the method will be int GetPageLoadTimeout(); and we need to implement this method inside our AppConfigReader here we are going to implement this method, so we will use this line of code

string timeout= ConfigurationManager.AppSettings.Get(AppConfigKeys.PageLoadTimeout);

Where we will read the data from App.config file so here-string as I told you earlier everything which is present inside the App.config will be in the form of string and if we are going to put a check then if(timeout==null)

So we are going to return a default value that is 30 seconds otherwise we are going to return the value which is coming from App.config so return Convert.ToInt32(timeout);

public int GetPageLoadTimeout()
string timeout= ConfigurationManager.AppSettings.Get(AppConfigKeys.PageLoadTimeout);
     return 30;
 return Convert.ToInt32(timeout);

So “timeout” will be a string we are going to convert to an integer and then return. So inside our BaseClass here we are going to use that method.

After selecting the type of browser we will call ObjectRepository.Driver.Manage().Timeouts(). SetPageLoadTimeout() and this will be (ObjectRepository.Config.GetPageLoadTimeout()), and this will be TimeSpan.FromSeconds so it will be 40 seconds.

ObjectRepository.Driver.Manage().Timeouts(). SetPageLoadTimeout(TimeSpan.FromSeconds(ObjectRepository.Config.GetPageLoadTimeout()));

So here we have discussed what is wait and how many types of wait are present in Selenium C#, Types of Implicit Wait methods, and only one method of them is how to use SetPageLoadTimeout() method.

In the next tutorial, we will discuss the second implicit wait method which is ImplicitlyWait() method.