Well, here we will discuss one more wait class which is the “DefaultWait” class.
DefaultWait class in Selenium C#
DefaultWait class is a dynamic wait class. When we use WebDriverWait class that class has a limitation that using a WebDriverWait class we can apply dynamic wait logic only on the web driver object but using the DefaultWait class we can apply the wait logic on other components also such as the web element.
And the steps for using DefaultWait class are similar to the WebDriverWait class. These are the following steps
Prerequisite:
set the “Implicit Wait” to 1.
Step1: create the instance of the “DefaultWait” class.
Step2: specify the “PollingInterval”.
Step3: specify the “Exception” to ignore.
Step4: create a “function” for wait logic.
Step5: call the “Until” method with the wait logic.
So in Visual Studio inside our TestScript, we will add one more directory called DefaultWait, inside this we will create a class called HandleDefaulWait inside it create a method called TestDefaultWait().
Wait Logic:
So here on the Bugzilla webpage as we can see we have a DropDown list that contains certain values so we are going to put a wait on this particular element and the wait logic is that whenever the user selects the “major” as a value the wait should terminate.
[TestClass] public class HandleDefaulWait { [TestMethod] public void TestDefaultWait() { NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite()); LinkHelper.ClickLink(By.LinkText("File a Bug")); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_login"), ObjectRepository.Config.GetUsername()); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_password"), ObjectRepository.Config.GetPassword()); ButtonHelper.ClickButton(By.Id("log_in")); LinkHelper.ClickLink(By.LinkText("testng")); ObjectRepository.Driver.Manage().Timeouts(). ImplicitlyWait(TimeSpan.FromSecond(1)); DefaultWait<IWebElement> wait=new DefaultWait<IWebElement> (ObjectRepository.Driver.FindElement(By.Id("bug_Severity"))); wait.PollingInaterval= TimeSpan.FromMilliseconds(200); wait.Timeout= TimeSpan.FromSeconds(50); wait.IgnoreExceptionTypes(typeof(NoSuchElementException),typeof(ElementNotVisibleException)); Console.WriteLine("After Wait: {0},wait.Until(changeofValue())); } private Func<IWebElement,string> changeofValue() { return ((x) => { Console.WriteLine("Waiting for value change"); SelectElement select= new SelectElement(x); if(select.SelectedOption.Text.Equals("major")) return select.SelectedOption.Text; return null; }); } }
Explanation of code:
So from the class DropDownList.cs we will copy this piece of code which is written in the above program, where we were navigating to the desired page. Once we are on the desired page we will use ObjectRepository.Driver.Manage().Timeouts(). ImplicitlyWait(TimeSpan.FromSecond(1));
Here we will create WebDriverWait and if we go to the class definition of this we can see here this particular class inherits from DefaultWait.
In WebDriverWait we have a constructor that will be applicable only on IWebDriver and if we go inside the DefaultWait so here we have a constructor where we can define on which component we need to apply the wait.
So we are using this class for creating the dynamic wait logic for DropDownList. Then we need to specify on which component we want to apply the wait logic so in our case, it is a DefaultWait<IWebElement> wait=new DefaultWait<IWebElement>
Here in brackets, we need to specify the web element on which we need to apply the wait so we will do inspect the element Its id is “bug_Severity” so (ObjectRepository.Driver.FindElement(By.Id("bug_Severity")));
In the constructor itself, we are specifying which web element we want to apply the DefaultWait. After that specify the PollingInterval wait.PollingInaterval= TimeSpan.FromMilliseconds(200);
After that we need to specify the Timeout so wait.Timeout= TimeSpan.FromSeconds(50);
then wait.IgnoreExceptionTypes(typeof(NoSuchElementException),typeof(ElementNotVisibleException));
At last, we will call the Until method for this again we need to create a function it will take input as IWebElement and return it as the generic.
private Func
delegate <IWebElement>
and this IWebElement is coming because we have specified the DefaultWait to be applicable on IWebElement.
So IWebElement and the return type is let’s say string and changeofValue()
inside this, we are going to create the anonymous function.
So return ((x)=>
as we know in order to handle the select DropDownList we need to use the Select class. So we will use SelectElement select= new SelectElement (x);
and we will specify to the x because x is of type web element now.
And we put a check and we are using SelectedOption which is going to give the options which are selected or the value which is selected inside the DropDownList and comparing the string of given object or value with “major”.
then return us return select.SelectedOption.Text;
otherwise, return the null. Again we are putting the Console.WriteLine statement now we will call this function inside the Until method because the entire statement will return us a string.
So again we are using Console.WriteLine
with Until method. Now put a debug point over wait.IgnoreExceptionTypes(typeof(NoSuchElementException),typeof(ElementNotVisibleException));
here and build the solution and run the script in debug mode.
Output: so it has hit the Debug point and we are at the required page so currently, the value which is selected inside the DropDownList is “Enhancement”, we will do a stepover now again we will call our Dynamic Wait for the wait.
so here this particular Dynamic Wait will wait until the value changes to “major”. So it is still waiting, now as soon as we change the value over here to “major” it is able to find the required string and our wait is terminated.
As soon as we select that value the wait is over. If we look at the output of our Test so it shows it is waiting until it caught the “major” value.
So in this manner using the DefaultWait class we can apply the wait on the other component also.