Waiting for control happens most of the time while we start writing code in any automation tool, it can be either QTP, Visual Studio Test or our Selenium, our code behaves exactly what we say (here we write J
) it executes all the lines of code but our application under test (AUT) will not always cope up with the speed of code execution done in selenium.
We always need to wait for our web page to load or a web element to be present in the page. In order to do that, the logic is shown in the below flow chart
As you can see from the above flow chart ,we start our code from starting a timer and checking for the controls existence, if the control does not exist then we will wait for certain time specified in the timer, if the control exist then the code will terminate, the code will also terminate if the timer times out
Let’s write the same programmatically as shown below
WebDriverWait wait = new WebDriverWait(Driver,TimeSpan.FromSeconds(200));
bool myElement = wait.Until(d => (element.Displayed));
As you can see the above C#
code snippet, the WebDriverWait
class has a constructor which accepts web driver reference and a time in seconds/miniute.
And using the WebDriverWait
instance we can use Until
method (which accept a generic type) to check for its return type using the lambda expression.
This implementation in selenium is called as explicit wait and we have written the whole flow chart explanation in couple of lines.