Friday, July 29, 2016

WebDriver DefaultWait in C#

WebDriver DefaultWait in C#


DefaultWait (FluentWait in Java) utilizes DefaultWait class of the C# Webdriver bindings. WebDriverWait class inherits from the DefaultWait class in C#.

Here, in this post, we will look at a simple example of DefaultWait.

In this example we are using two files "button.html" and "styles.css". When we double click on "button.html" to open it, it opens in the browser and after 10 seconds a button named "proceed" is displayed on it.

Our button.html file:


Our styles.css file:


Put both the files in one folder. When you open the button.html file in a browser (I used Firefox), below screen is shown.


Then after 10 seconds "proceed" button will be visible. This is because we have set the timer to 10000 milliseconds or 10 seconds in our button.html file.


Lets run a small example using above files. In this example we keep waiting until an element is displayed.


Lets go through the above code:
Create a new instance of Firefox driver. Navigate to the desired URL. Find the element. We are finding it using Id. Create wait using DefaultWait. Here element is the element on which wait is to be applied. We are using two properties of DefaultWait here, namely timeout and polling interval. Timeout is how long to wait for the evaluated condition to be true. The default timeout is 500 milliseconds. PollingInterval is how often the condition should be evaluated. The default timeout is 500 milliseconds.
Next there is a function "waiting" which takes input parameter as IWebElement and outputs a boolean value. As soon as an element is displayed on screen, "true" is returned and the function ends. We are calling the function on line “wait.Until(waiting)”. The function ends when true is returned i.e. the function keeps running as long as false is being returned.

Output of the above test can be seen below. Test was run on Visual Studio.

As you can see that “proceed” button is displayed after 10,000 milliseconds and polling is done after every 2500 milliseconds, hence, in our output, text “still waiting” is shown 4 times (10000/4).