Timeouts Interface in Selenium WebDriver
Timeouts interface manages all the waits of the driver
instances; this is inner Interface of WebDriver Interface on other words
Timeouts interface is enclosed by WebDriver interface.
Timeouts interface has three abstract methods, which are:
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 implementations for these
methods because browser classes implement WebDriver Interface.
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...)
We can set the page load timeout using pageLoadTimeout method present in Browser classes (FirefoxDriver, ChromeDriver...)
driver.manage().timeouts().pageLoadTimeout(30,
TimeUnit.SECONDS);
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);
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();
driver.get("https://google.com/");
//
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.
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 Async 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')");
No comments:
Post a Comment