Learn Selenium
上QQ阅读APP看书,第一时间看更新

Implicit wait time

Implicit wait time is used when you want to configure the WebDriver's wait time as a whole for the application under test. Imagine you have hosted a web application on a local server and on a remote server. Obviously, the time to load for a web page hosted on a local server would be less than the time for the same page hosted on a remote server, due to network latency. Now, if you want to execute your test cases against each of them, you may have to configure the wait time accordingly, such that your test case doesn't end up spending more time waiting for the page, or spend nowhere near enough time, and timeout. To handle these kinds of wait-time issues, WebDriver provides an option to set the implicit wait time for all of the operations that the driver does using the manage() method.

Let's see a code example of implicit wait time:

driver = new ChromeDriver();
driver.navigate().to("http://demo-store.seleniumacademy.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Let's analyze the following highlighted line of code:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Here, driver.manage().timeouts() returns the WebDriver.Timeouts interface, which declares a method named implicitlyWait, which is where you specify the amount of time the driver should wait when searching for a WebElement on a web page if it is not immediately present. Periodically, the WebDriver will poll for the WebElement on the web page, until the maximum wait time specified to the previous method is over. In the preceding code, 10 seconds is the maximum wait time your driver will wait for any WebElement to load on your browser. If it loads within this time period, WebDriver proceeds with the rest of the code; otherwise, it will throw NoSuchElementException.

Use this method when you want to specify a maximum wait time, which is generally common for most of the WebElements on your web application. The various factors that influence the performance of your page are network bandwidth, server configuration, and so on. Based on those conditions, as a developer of your WebDriver test cases, you have to arrive at a value for the maximum implicit wait time, such that your test cases don't take too long to execute, and, at the same time, don't timeout very frequently.