ImplicitlyWait method in Selenium C#

This tutorial is a continuation of the previous tutorial where we have discussed what is wait, how many types of wait in Selenium C#, and using the one method of Implicit Wait which is SetPageLoadTimeout() method.

Now in this tutorial, we will discuss the second Implicit Wait method which is ImplicitlyWait() method.

ImplicitlyWait() :

So now the second method is ImplicitlyWait() this will be applicable for the element. So inside the same TestWebDriverWait class, we will remove this line of code ObjectRepository.Driver.Manage().Timeouts(). SetPageLoadTimeout (TimeSpan.FromSeconds(40));

After navigating to the URL that is this particular URL we are going to type certain text to this TextBox. So we are going to type TextBoxHelper.TypeInTextBox(By.XPath("//input[@class='gLFyf gsfi']"),"C#"); if we check this XPath so there is one matching node related to this.

Now let us set the value for implicit wait ObjectRepository.Driver.Manage().Timeouts(). ImplicitlyWait(TimeSpan.FromSeconds(50)); so this wait will be applicable for the element.

[TestClass]
public class TestaWebDriverWait
{
[TestMethod] 
public void TestWait() 
{ 
NavigationHelper.NavigateToUrl("https://www.google.com/");
ObjectRepository.Driver.Manage().Timeouts(). SetPageLoadTimeout(TimeSpan.FromSeconds(50));
TextBoxHelper.TypeInTextBox(By.XPath("//input[@class='gLFyf gsfi']"),"C#");
}
}

And also we will put a method in our BaseClass.

NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite());

So by default, it is going to open the website which is present inside the App.config. so we are going to put the debug point, build our solution and run the script in debug mode.

Output: so it has hit the debug point if we do a stepover it is going to open the new page, so now we are going to set the implicit wait to 50 seconds now before clicking on stepover we will just click Back.

So this time what will happen is that the particular element where we are trying to put the value which is not present at that element.

Now again we will do a stepover as we can see here the web driver is still waiting for that element and this is because of the implicit wait.

We will click on the forward button so it will open the webpage where we have that element as we can see here once the element appears automatically it supplies the value “C#” over here and it continues with the normal flow.

Because of this particular property, our action will wait for 50 seconds not exactly 50 seconds but the duration will be 50 seconds maximum.

After that, it is going to throw us the NoSuchElementException if the maximum value is very less we will add one more configuration in our App.config for the element.

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

After that add the key inside the AppConfigKeys class.

public const string ElementLoadTimeout="ElementLoadTimeout";

Similarly, we will add a method inside the IConfig interface.

int GetElementLoadTimeout();

Now in AppConfigReader class write the definition of this class.

public int GetElementLoadTimeout()
{
string timeout=ConfigurationManager.AppSettings.Get(AppConfigKeys.ElementLoadTimeout);
if(timeout==null)
   return 30;
 return Convert.ToInt32(timeout);
}

So inside the BaseClass, we will call this method.

ObjectRepository.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(ObjectRepository.Config.GetElementLoadTimeout());

In BaseClass.cs enable the NavigationHelper line and in TestWebDriverWait.cs here remove the line of code which is

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

Now rebuild our solution and run the script in debug mode.

Output: so as we can see here it has opened the webpage and typed the “C#” in the TextBox.

So in this manner, we can set the implicit wait for the element.