Implicit Wait in Selenium WebDriver
Implicit wait in Selenium WebDriver introduction:
Implicit wait in WebDriver has solved many issues that occurs due to intensive use of Ajax in any webpage. Ajax intends loading time of each element on webpage to vary.Due to different loading time, it become cumbersome to find all element on web page as soon as web page opens and this causes your script to fail.
Implicit wait specifies the amount of time the driver should wait when searching for an element if it is not immediately present. So to search any element on webpage driver will poll the page till the time it does not found element or time out expires and then it throws exception like “NoSuchElementException”.
Watch video to know more about facts of Implicit Wait
Few important points about Implicit wait:
1- Implicit wait is called global wait because once it is declared it stay with WebDriver instance for its life time.
2- It comes in to action, as soon as it sees any driver.findElement/driver.findElements statement in code. it means it will wait for all the statements made to find WebElement on webpage.
3- Default wait is “0” that can be custom as per our need depending upon network latency.
4- Implicit wait should be avoided with slow locator like xpath due to its adverse impact on script run time.
Before moving ahead why not we see code snippet taken from WebDriver api which have all the code of TimeOut interface
interface Timeouts { / * * @param time The amount of time to wait. * @param unit The unit of measure for {@code time}. * @return A self reference. */ Timeouts implicitlyWait(long time, TimeUnit unit); * * @param time The timeout value. * @param unit The unit of time. * @return A self reference. * @see JavascriptExecutor#executeAsyncScript(String, Object...) */ Timeouts setScriptTimeout(long time, TimeUnit unit); /** * * @param time The timeout value. * @param unit The unit of time. * @return A Timeouts interface. */ Timeouts pageLoadTimeout(long time, TimeUnit unit); } Source : WebDriver Code
As we can see in above code that implicitlyWait take two parameters, First parameter takes the time for which driver instance have to wait and second instance is an enum TimeUnit which have constants like Seconds, Minutes, Hours, Days,milliSeconds, micoSeconds and nanoSeconds.
So if someone has taken 5 as time to wait and TimeUnit.SECONDS then it means driver instance will wait for next 5 seconds before throwing exceptions if element is not found on webpage.
code snippet used for implicitlyWait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
above line of code should be used just after driver instance is initialise.
So lets take one example of implicit wait.
Scenario:
1- Open bing website
2- Enter Selenium Tutorials string in search text field
3- Once string will be entered in textfield, some ajax drop down will appear and in this first choice should be selected.
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class IMplicitWaitInWebDriver { @Test public void test() { WebDriver driver = new FirefoxDriver(); driver.get("http://bing.com"); driver.findElement(By.name("q")).sendKeys("Selenium Tutorials"); // Implicit wait : is global wait for webelement driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.findElement(By.xpath("//*[@id='sa_2']/div/div")).click(); } }
Description of above code:
above code will open one firefox instance and will open bing.com and after opening bing.com it will enter Selenium Tutorials string in search box but as soon as we will enter string ajax dropdown will not appear on screen and for this we need to wait for 2-3 seconds so for safer side, have used 5 second in code and after 5 second elapsed code will perform the action of selecting the first dropdown option.
This is a fantastic blog from which people can learn a lot. It is very informative and is explained in…
Thanks for sharing such knowledgeable Content on Automation testing, to know how to enhance the performance of ERP's like Oracle…
Thanks.. this list is much needed.
This one is also good to use. Thanks for sharing this.. Might be we can also add it in list…
How about youtube-dl?