Man must explore, and this is exploration at its greatest

Problems look mighty small from 150 miles up

Posted by Start Bootstrap on August 24, 2022

TimeOuts

An interface for managing timeout behavior for WebDriver instances.

                        
 /**
 * An interface for managing timeout behavior for WebDriver instances.
 */
interface Timeouts {
                         
  /**
   * @deprecated Use {@link #implicitlyWait(Duration)}
   *     

Specifies the amount of time the driver should wait when searching for an element if * it is not immediately present. *

When searching for a single element, the driver should poll the page until the element * has been found, or this timeout expires before throwing a {@link NoSuchElementException}. * When searching for multiple elements, the driver should poll the page until at least one * element has been found or this timeout has expired. *

Increasing the implicit wait timeout should be used judiciously as it will have an * adverse effect on test run time, especially when used with slower location strategies * like XPath. *

If the timeout is negative, not null, or greater than 2e16 - 1, an error code with * invalid argument will be returned. * @param time The amount of time to wait. * @param unit The unit of measure for {@code time}. * @return A self reference. */ @Deprecated Timeouts implicitlyWait(long time, TimeUnit unit); /** * Specifies the amount of time the driver should wait when searching for an element if it is * not immediately present. * *

When searching for a single element, the driver should poll the page until the element has * been found, or this timeout expires before throwing a {@link NoSuchElementException}. When * searching for multiple elements, the driver should poll the page until at least one element * has been found or this timeout has expired. * *

Increasing the implicit wait timeout should be used judiciously as it will have an adverse * effect on test run time, especially when used with slower location strategies like XPath. * *

If the timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid * argument will be returned. * * @param duration The duration to wait. * @return A self reference. */ default Timeouts implicitlyWait(Duration duration) { return implicitlyWait(duration.toMillis(), TimeUnit.MILLISECONDS); } /** * Gets the amount of time the driver should wait when searching for an element if it is not * immediately present. * * @return The amount of time the driver should wait when searching for an element. */ default Duration getImplicitWaitTimeout() { throw new UnsupportedCommandException(); } /** * @deprecated Use {@link #setScriptTimeout(Duration)} *

Sets the amount of time to wait for an asynchronous script to finish execution before * throwing an error. If the timeout is negative, not null, or greater than 2e16 - 1, an * error code with invalid argument will be returned. * @param time The timeout value. * @param unit The unit of time. * @return A self reference. * @see JavascriptExecutor#executeAsyncScript(String, Object...) */ @Deprecated Timeouts setScriptTimeout(long time, TimeUnit unit); /** * Sets the amount of time to wait for an asynchronous script to finish execution before * throwing an error. If the timeout is negative, not null, or greater than 2e16 - 1, an error * code with invalid argument will be returned. * * @param duration The timeout value. * @deprecated Use {@link #scriptTimeout(Duration)} * @return A self reference. * @see JavascriptExecutor#executeAsyncScript(String, Object...) */ @Deprecated default Timeouts setScriptTimeout(Duration duration) { return setScriptTimeout(duration.toMillis(), TimeUnit.MILLISECONDS); } /** * Sets the amount of time to wait for an asynchronous script to finish execution before * throwing an error. If the timeout is negative, not null, or greater than 2e16 - 1, an error * code with invalid argument will be returned. * * @param duration The timeout value. * @return A self reference. * @see JavascriptExecutor#executeAsyncScript(String, Object...) */ default Timeouts scriptTimeout(Duration duration) { return setScriptTimeout(duration); } /** * Gets the amount of time to wait for an asynchronous script to finish execution before * throwing an error. If the timeout is negative, not null, or greater than 2e16 - 1, an error * code with invalid argument will be returned. * * @return The amount of time to wait for an asynchronous script to finish execution. */ default Duration getScriptTimeout() { throw new UnsupportedCommandException(); } /** * @param time The timeout value. * @param unit The unit of time. * @return A Timeouts interface. * @deprecated Use {@link #pageLoadTimeout(Duration)} *

Sets the amount of time to wait for a page load to complete before throwing an error. * If the timeout is negative, not null, or greater than 2e16 - 1, an error code with * invalid argument will be returned. */ @Deprecated Timeouts pageLoadTimeout(long time, TimeUnit unit); /** * Sets the amount of time to wait for a page load to complete before throwing an error. If the * timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid argument * will be returned. * * @param duration The timeout value. * @return A Timeouts interface. */ default Timeouts pageLoadTimeout(Duration duration) { return pageLoadTimeout(duration.toMillis(), TimeUnit.MILLISECONDS); } /** * Gets the amount of time to wait for a page load to complete before throwing an error. If the * timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid argument * will be returned. * * @return The amount of time to wait for a page load to complete. */ default Duration getPageLoadTimeout() { throw new UnsupportedCommandException(); } }

implicitlyWait Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException. When searching for multiple elements, the driver should poll the page until at least one element has been found or this timeout has expired. Increasing the implicit wait timeout should be used judiciously as it will have an adverse effect on test run time, especially when used with slower location strategies like XPath.

                            
import java.time.Duration;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Example1 {

    public static void main(String[] args) {
        
        WebDriver driver = new ChromeDriver();
        
        driver.get("https://www.selenium.dev/");
        
        System.out.println("Before: ");
        System.out.println(driver.manage().timeouts().getImplicitWaitTimeout());
        System.out.println(driver.manage().timeouts().getPageLoadTimeout());
        System.out.println(driver.manage().timeouts().getScriptTimeout());
        
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(15));
        driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(5));
        
        System.out.println("After: ");
        System.out.println(driver.manage().timeouts().getImplicitWaitTimeout());
        System.out.println(driver.manage().timeouts().getPageLoadTimeout());
        System.out.println(driver.manage().timeouts().getScriptTimeout());
        
        driver.quit();
    }

}
                            
                          

output:

                            
Before: 
PT0S
PT5M
PT30S
After: 
PT10S
PT15S
PT5S
                            
                          


Scripttimeout example:

                            
import java.time.Duration;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.ScriptTimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Example2 {

    public static void main(String[] args) {
        
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://www.selenium.dev/");
            
            driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(5));
            
            JavascriptExecutor jse = (JavascriptExecutor) driver;
            jse.executeAsyncScript("setTimeout(()=>{alert('Hello from alert');},6000)");
            
        }catch (ScriptTimeoutException e) {
            e.printStackTrace();
        }finally {
            driver.quit();
        }

    }

}
                            
                          


If you run the above program you will get following error:

Use this Link to test pageload timeout

org.openqa.selenium.ScriptTimeoutException: script timeout

                        
org.openqa.selenium.ScriptTimeoutException: script timeout
  (Session info: chrome=116.0.5845.140)
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.2.0-26-generic', java.version: '17.0.5'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [07c097c99a4b633c2e43bb0f35d0449a, executeAsyncScript {script=setTimeout(()=>{alert('Hello from alert');},6000), args=[]}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 116.0.5845.140, chrome: {chromedriverVersion: 116.0.5845.96 (1a3918166880..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:44065}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:cdp: ws://localhost:44065/devtoo..., se:cdpVersion: 116.0.5845.140, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 07c097c99a4b633c2e43bb0f35d0449a
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
	at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
	at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:52)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:191)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
	at org.openqa.selenium.remote.RemoteWebDriver.executeAsyncScript(RemoteWebDriver.java:471)
	at timeouts.Example2.main(Example2.java:21)
                        
                    

PageLoadTimeout Example:

                            
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Example3 {

    public static void main(String[] args) {
        
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://mcrsr.github.io/pageloadtimeout.html");
            driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(5));
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            driver.quit();
        }

    }

}
                            
                          

If you run the above program you will get the following error

org.openqa.selenium.TimeoutException: timeout: Timed out receiving message from renderer: 2.023