Selenium 4 Headless Mode
Selenium 4 Headless Mode
Selenium 4 introduced an improved headless mode for browsers like Chrome and Firefox. Running tests in headless mode means executing them without launching a visible browser window. This is useful for faster test execution, running tests in a CI/CD pipeline, and saving system resources.
What is Headless Mode?
Headless mode allows browsers to run without a graphical user interface (GUI). It is especially useful for automating tests in environments where a visible browser is not required, such as remote servers or cloud-based testing.
ChromeOptions in Selenium
ChromeOptions is a class in Selenium that allows us to configure browser-specific settings. We use it to enable headless mode, set various startup arguments, and manage browser capabilities.
Using setHeadless(true) Method
In Selenium 4, the setHeadless(true)
method was deprecated. Instead, you should use the addArguments("--headless")
option to run Chrome in headless mode.
Deprecated
Earlier to execute your Selenium tests in headless mode, you had to utilize the ChromeOptions as shown below.
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
WebDriver driver = new ChromeDriver(options);
By using setHeadless() method, you will accomplish the same result. Finally, you must supply the options as a parameter when creating an instance of the ChromeDriver.
The traditional –headless, and since version 96, Chrome has a new headless mode that allows users to get the full browser functionality (even run extensions). Between versions 96 to 108 it was –headless=chrome, after version 109 –headless=new.
Usage from Chrome 96 onwards:
options.addArguments("--headless=chrome")
For Chrome 109 and above, the ‘–headless=new’ flag will now allow you to get the full functionality of Chrome in the new headless mode, and you can even run extensions in it. (For Chrome versions 96 through 108, use –headless=chrome)
Example: Running Selenium 4 in Headless Mode with Chrome
The following Java program demonstrates how to launch Chrome in headless mode and navigate to a website.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class HeadlessTest {
public static void main(String[] args) {
// Set up ChromeOptions
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless"); // Enable headless mode
options.addArguments("--disable-gpu"); // Recommended for better performance
// Initialize WebDriver
WebDriver driver = new ChromeDriver(options);
// Open a website
driver.get("https://www.example.com");
// Print the title
System.out.println("Page Title: " + driver.getTitle());
// Close the browser
driver.quit();
}
}
Benefits of Headless Mode
- Faster test execution as no GUI rendering is required.
- Useful for running tests in a CI/CD pipeline.
- Consumes fewer system resources.
- Allows execution on remote servers without a display.
Selenium’s headless mode allows running browser tests without a visible browser window by configuring the browser driver with the headless option. This approach enables faster and more efficient test execution as rendering the browser window is unnecessary.