Implicit Waits in Selenium | Selenium WebDriver Tutorial
Implicit Waits:
Implicit waits tell to the WebDriver to wait for certain amount of time before it throws an exception. Once we set the time, WebDriver will wait for the element based on the time we set before it throws an exception. The default setting is 0 (zero). We need to set some wait time to make WebDriver to wait for the required time.
Note: Implicit Wait is in place for the entire time the browser is open. Time taken to search all the elements are based on the time fixed for the implicit wait.
Must Read: WebDriverWait in Selenium
Syntax:
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
Implicit Wait time is applied to all the elements in the script.
Implicit wait will accept 2 parameters, the first parameter will accept the time as an integer value and the second parameter will accept the time measurement in terms of SECONDS, MINUTES, MILISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.
Must Read: FluentWait in Selenium
Test script with an explanation – Implicit Waits in Selenium:
Find the sample script (using Java) mentioned below. Execute it to see the functionality of Implicit Wait in Selenium.
package waits; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class ImplicitWaits { public static void main(String[] args) { //To create a new instance of Firefox Driver WebDriver driver = new FirefoxDriver(); //Implicit Wait - Here the specified Implicit Wait time frame is 15 seconds. //It waits 15 seconds of time frame for the element to load. //It throws an exception, if the element is not loaded within the specified time frame driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); //To open a website "Software Testing Material" driver.get("https://www.softwaretestingmaterial.com"); //To maximize the browser window driver.manage().window().maximize(); //To close the browser driver.close(); } }
There are some instances when a particular element takes more time (eg. 1 min) to load. In such cases setting a huge time to Implicit wait makes the browser to wait for the same time for every element. To avoid this, we need to implement Explicit Waits.
If you are not regular reader of my blog then I highly recommend you to sign up for the free email newsletter using the below link.
Subscribe and get a free eBook and regular updates from SoftwareTestingMaterial.com