To simplify this post, I want to ask you a basic question, how many times you open a web browser and the web elements are displayed in a different order? How many times this element are accessed with some delays? Well, I’m 100% sure that you can think about 1K examples that you can use to answer this two simple questions.
Now, think about your “Human” response to this situation, sometimes you just wait until the elements are loaded, sometimes you refresh the screen and sometimes you simply move to a different web page that you can use to accomplish the same results.
In the “Computer” world, where your expectations are built on a predefined value, you cannot trust such behavior where every delay can affect your test execution that leads to negative results.
To overcome this issue, the selenium framework allows the coder to overcome such scenarios with a basic “Wait” commands that help the machine to understand that the element will be available under a specific amount of time.
During this post I will describe the two types of “Wait” commands “Explicit” and “Implicit”.
Selenium Wait commands
Let’s talk about some technical issues, when you run your code without a “Wait” command the execution will fail in any case that there is a delay between the code execution and the element availability (The returned error is “ElementNotVisibleException”), this issue occurred based on two facts:
1. Selenium will search the element for one time only!
2. The default delay is 0.
Function 1: Navigate to a web page.
Function 3: Open any web element that still not available
Without the “Wait” command, the execution will fail every time that you run it, the solution is to add a “Wait” command that creates the bridge between the two functions.
Implicit wait, is used to acknowledge the WebDriver instance to wait a specific amount of time before throwing an exception and mark the test as in any case that he failed to find the element in the first try. Implicit wait.
Let’s explain this, by default the driver will try to find an element, if this element is not available the execution will failed and an exception will returned, the implicit wait will help you to overcome this issue because it’s allow you to determine a specific amount of time from the first try and until the time frame is ended, during this time frame there will no attempt to access the web element, but in the end the driver will try to access it again, in case of success the object is found and the tests can continue, and in any case of failure an exception is raised.
Few more issues:
- An implicit wait, can be set with different time frames (Hours, Minutes, Seconds Etc.).
- An “Implicit” wait, is set with timeframe of ‘0’.
- An implicit wait, is relevant to a specific WebDriver, from the moment that he created and until the Driver ends is Life cycle (and in simple words, from the moment that your browser is opened and until it’s closed).
Create a new WebDriver:
IwebDriver FXDriver = new FirefoxDriver();
Create an “Implicit” wait to this driver:
FXDriver.Manage ().timeouts ().ImplicityWait (TimeSpan.FromSeconds ((18));
The Explicit wait, is more restricted in a way that it’s not relevant to the entire Driver life cycle, the “Explicit” wait is used when you want to wait between a command that you execute and until you got a respond (in other words the code execution is stopped until the expected condition is met or the specified timeframe is ended).
Now, some of you which has some knowledge with Coding language, may think that you can achieve the same results with the “Sleep” command (Syntax: System.Threading.Thread.Sleep (No’ of milliseconds)) that allow you to stop the execution on every time that you want, well, if you think about it, stop it!. The sleep command will ALWAYS run no matter if the element is already available and the test can continue.
And for the big question…why you need to use an “Explicit” wait when you define the “Implicit” wait for the entire WebDriver Lifecycle? Well, the answer is very logic, when you define an “Implicit” wait you set a “Hard Coded” time that will set for each case of delay, but sometimes you have a specific cases where the element loading time may take much further than the time you set, therefore there is no sense to define a large time period in the Implicit code because the code execution will run for ages (just combine and create explicit waits in specific locations).
Create a new WebDriver:
IWebDriver FirefoxInstance = new FirefoxDriver();
Create an “Explicit” wait to this driver:
IWebElement ExpWait = (new WebDriverWait(FirefoxInstance,