Capabilities are options that are used to customize and configure a WebDriver session. These are settings that we can configure for the WebDriver to help in stabilized execution of the build. Many of the random failures that we encounter while running the build can be avoided using these options.
These instructions for the driver are generally given at the beginning of the session (browser options classes), before calling the driver. For remote driver sessions, a browser options instance would be required.
In this blog, we will be discussing more on the options for Chrome driver, which is one of the most frequently used WebDriver's. The WebDriver language APIs provides ways to pass capabilities to ChromeDriver and ChromeOptions is the class to manage options specific to ChromeDriver.
Let’s discuss some of the commonly used Chrome options here:
Option 1: setPageLoadStrategy
When navigating to a new page via URL, by default, WebDriver will hold off on completing a navigation method e.g., driver.navigate().get() until the document ready state is complete. This does not necessarily mean that the page has finished loading. Note also that this behavior does not apply to navigation, that is a result of clicking an element or submitting a form. Three types of page load strategies are available.
The page load strategy queries the document.readyState as described in the table below:
Strategy | Ready State | Notes |
normal | complete | Used by default, waits for all resources to download. Waits for the entire website to get loaded (HTML, CSS, JS, Images etc.). Random failures like Element not found exception are avoided. |
eager | interactive | DOM access is ready, but other resources like images may still be loading. Waits for the HTML content to get loaded, but not CSS, Images etc. |
none | Any | Does not block WebDriver at all |
The document.readyState property of a document describes the loading state of the current document.
If a page takes a long time to load because of downloading assets (e.g., images, CSS, JS) that aren’t important to the automation, you can change from the default parameter of normal to eager or none to speed up the session. This value applies to the entire session, so make sure that your waiting strategy is sufficient to minimize flakiness.
(i)Normal (default):
WebDriver waits until the load event fire is returned.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(chromeOptions);
(ii) Eager:
WebDriver waits until DOMContentLoaded event fire is returned.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
(iii) None:
WebDriver only waits until the initial page is downloaded.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(chromeOptions);
Option 2: setAcceptInsecureCerts
This capability checks whether an expired (or) invalid TLS Certificate is used while navigating during a session. An SSL/TLS certificate is a digital object that allows systems to verify the identity & subsequently establish an encrypted network connection to another system using the Secure Sockets Layer/Transport Layer Security (SSL/TLS) protocol. Certificates are used within a cryptographic system known as a public key infrastructure (PKI). PKI provides a way for one party to establish the identity of another party using certificates if they both trust a third-party - known as a certificate authority. SSL/TLS certificates thus act as digital identity cards to secure network communications, establish the identity of websites over the Internet as well as resources on private networks.
If the capability is set to false, an insecure certificate error will be returned as navigation encounters any domain certificate problems. If set to true, invalid certificate will be trusted by the browser.
All self-signed certificates will be trusted by this capability by default. Once set, acceptInsecureCerts capability will have an effect for the entire session.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(chromeOptions);
Option 3: setScriptTimeout
A WebDriver session is imposed with a certain session timeout interval, during which the user can control the behavior of executing scripts or retrieving information from the browser. ScriptTimeout specifies when to interrupt an executing script in a current browsing context. The default timeout 30,000 is imposed when a new session is created by WebDriver.
ChromeOptions chromeOptions = new ChromeOptions();
WebDriver driver = new ChromeDriver(chromeOptions);
Option 4: setPageLoadTimeout
Specifies the time interval in which web page needs to be loaded in a current browsing context. The default timeout 300,000 is imposed when a new session is created by WebDriver. If page load limits a given/default time frame, the script will be stopped byTimeoutException.
ChromeOptions chromeOptions = new ChromeOptions();
WebDriver driver = new ChromeDriver(chromeOptions);
Option 5: setImplicitWaitTimeout
This specifies the time to wait for the implicit element location strategy when locating elements. The default timeout 0 is imposed when a new session is created by WebDriver.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setImplicitWaitTimeout(Duration.ofSeconds(5));
WebDriver driver = new ChromeDriver(chromeOptions);
Option 6: Adds additional command line arguments to be used when starting Chrome.
Some of the commonly used arguments for ChromeOptions class
(i) start-maximized: Opens Chrome in maximize mode
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("start-maximized");
ChromeDriver driver = new ChromeDriver(chromeOptions);
(ii) incognito: Opens Chrome in incognito mode
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("--incognito ");
ChromeDriver driver = new ChromeDriver(chromeOptions);
(iii) headless: Opens Chrome in headless mode. A Headless browser runs in the background. You will not see the browser GUI or the operations been operated on it. Nevertheless, we would be able to see the output of the run in the console.
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("--headless");
ChromeDriver driver = new ChromeDriver(chromeOptions);
(iv) disable-extensions: Disables existing extensions on Chrome browser
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("--disable-extensions");
ChromeDriver driver = new ChromeDriver(chromeOptions);
(v) disable-popup-blocking: Disables pop-ups displayed on Chrome browser
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("--disable-popup-blocking");
ChromeDriver driver = new ChromeDriver(chromeOptions);
(vi) make-default-browser: Makes Chrome default browser
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("--make-default-browser");
ChromeDriver driver = new ChromeDriver(chromeOptions);
(vii) version: Prints chrome browser version
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("--version");
ChromeDriver driver = new ChromeDriver(chromeOptions);
(viii) disable-infobars: Prevents Chrome from displaying the notification
‘Chrome is being controlled by automated software’
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("disable-infobars");
ChromeDriver driver = new ChromeDriver(chromeOptions);
Conclusion
Here we have discussed how the Driver Capabilities (Browser Options) are an important part of automation testing. They provide a way to configure the browser session, set up the environment, and control how you want to execute the test. These capabilities allow testers to specify the exact browser version they want to use and many other features like page load strategy, timeouts etc. to help achieve stabilized and synchronized execution of the build by avoiding many of the random failures.
useful thank you