Timeouts Interface in Selenium Webdriver

Timouts interface manges all the waits of the driver instances, This is inner Interface of Webdriver Interface on other words Timouts interface is enclosed by Webdriver interface.

Timeouts interface has three abstract methods, which are :

  • implicitlyWait
  • setScriptTimeout
  • pageLoadTimeout

There is no implementation present for these methods in Timeouts interface, the browser classes(FirefoxDriver, ChromeDriver..) provides the implemetations for these methods because browser classes implements Webdriver Interface.

Learn about ScriptTimeoutException in Selenium

Page Load Timeout in Selenium Webdriver

Page load timeout in selenium requests/set the time limit for a page to load, If the page is not loaded within the given time frame selenium throws TimeOutException exception

We can set the page load timeout using pageLoadTimeout method present in Browser classes(FirefoxDriver, ChromeDriver..)

						driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

Page load timeout is useful when we perform performance test, or when we test execution in IE.

Page Load timeout is applicable only to driver.get() and driver.navigate().to() methods in selenium webdriver

Setting Negative time limit makes the selenium to wait for the page load infinitely

						// set page load time as infinite (by giving minus value)
						driver.manage().timeouts().pageLoadTimeout(-10, TimeUnit.SECONDS);

Note : Page load timeout is not applicable when user clicks a link to open a page.

Complete program for Page load timeout

						public class PageLoadTimeTest {
							public static void main(String[] args) {
								// set chrome driver exe path
								System.setProperty("webdriver.chrome.driver", "C:/~/chromedriver.exe");
								WebDriver driver = new ChromeDriver();
								// set the time of 30 seconds for page to complete the loading
								driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

Set Script Load timeout

setScriptTimeout sets the time limit for asynchronous script to finish execution, if process is not completed before the time limit selenium throws TimeoutException exception

The setScriptTimeout method affects only JavaScript code executed with executeAsyncScript and nothing else. In particular, executeScript is not affected by it.

So why do you want to set a timeout for executeAsyncScript?
    The default timeout for setScriptTimeout method is 0 (zero), if we donot set any time our executeAsyncScript method may fail because the javascript code may take more than zero seconds. So to avoid unnecessary failures we have to set the setScriptTimeout.

Run a simple javascript: (Do not set setScriptTimeout ) - Now this shall execute without throwing any issue.

						((JavascriptExecutor) driver).executeScript("alert('I am alert');");

Run a simple Assync Script: ( Do not set setScriptTimeout) - This shall fail with error - "Timed out waiting for async script result after 0ms"

						((JavascriptExecutor) driver).executeAsyncScript("document.getElementById('dummy')");

To resolve above issue add below given setScriptTimeout

						driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
						((JavascriptExecutor) driver).executeAsyncScript("document.getElementById('dummy')");

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech