Headless browser testing in Selenium using Java runs automation scripts without opening a visible browser window. It is mainly used for faster execution and efficient test runs in background environments such as CI/CD pipelines.
- Executes tests without launching a graphical user interface (GUI), improving speed
- Well-suited for CI/CD pipelines and server-based execution
- Reduces system resource usage during automated testing
Selenium Headless Browser Testing in Java – Step by Step
Headless browser testing in Selenium allows automated test execution without opening the browser UI, making testing faster and more resource-efficient.
Step 1: Setting up the Selenium in Java
First of all, download the Selenium WebDriver JARs from the official Selenium website. Now, open Eclipse, create a new Java project, and add the downloaded WebDriver JAR to the project's build path.
Step 2: Installing the Necessary Modules
In Selenium WebDriver for Java, the required classes and interfaces are already available in the Selenium JAR files. You just need to import them into your program.
- By: A class that is used to locate elements on a web page using various element identifiers such as ID, name, CSS selector, XPath, etc. is known as By.
- WebDriver: An interface that provides methods to automate web browsers and control web page interactions in Java is known as WebDriver.
- ChromeDriver: A WebDriver specifically designed to automate interactions with the Chrome web browser is known as ChromeDriver. It is necessary to install the same version of ChromeDriver as your Chrome, or else it will not work.
- ChromeOptions: A class that allows the user to customize and configure ChromeDriver's behavior is known as ChromeOptions.
- WebElement: An HTML element that provides methods to interact with WebDriver and manipulate its attributes and properties is known as WebElement.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;
Step 3: Defining Headless Browser Testing
In this step, we configure ChromeDriver options to modify the default behavior of the browser and enable headless mode, where the browser runs in the background without a visible UI.
- ChromeOptions: Used to customize browser settings such as enabling headless mode, disabling extensions, or setting preferences.
- addArguments(): A method used to pass command-line arguments to the browser to control its behavior. For headless testing, we pass "headless" as an argument.
// Import selenium libraries
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;
public class selenium2 {
public static void main(String[] args) {
// Set ChromeDriver path
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define ChromeDriver options
ChromeOptions options = new ChromeOptions();
// Enable headless mode
options.addArguments("--headless");
// Launch browser in headless mode
WebDriver driver = new ChromeDriver(options);
}
}
Step 4: Initiating the Web Driver and Navigate to the Webpage
In this step, we will define the path of the ChromeDriver and initiate the ChromeDriver using the ChromeDriver function with options as an argument, that defines headless browser testing. Further, we have opened the Geeks For Geeks website (link) using the get() function.
- get(): A function that is used to navigate to a specified URL in the web browser controlled by WebDriver is known as the get function.
// Import selenium libraries
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;
public class selenium2 {
public static void main(String[] args) {
// Set ChromeDriver path
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define Chrome options
ChromeOptions options = new ChromeOptions();
// Enable headless mode
options.addArguments("--headless");
// Initialize WebDriver with options
WebDriver driver = new ChromeDriver(options);
// Open website
driver.get("https://www.geeksforgeeks.org/");
// Close browser
driver.quit();
}
}
Step 5: Finding an Element
In this step, we locate a web element using Selenium’s findElement() method. The element is identified using By.linkText(), which matches the visible text of a hyperlink on the webpage. Selenium returns a WebElement, which can then be used to perform actions like click, send keys, or retrieve text.
- findElement(): Used to locate a single web element on the page.
- By.linkText(): Locates a hyperlink using its visible text.
// Import selenium libraries
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;
public class selenium2 {
public static void main(String[] args) {
// Set ChromeDriver path
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define Chrome options
ChromeOptions options = new ChromeOptions();
// Enable headless mode
options.addArguments("--headless");
// Optional: set window size for headless mode
options.addArguments("window-size=1920,1080");
// Initialize WebDriver
WebDriver driver = new ChromeDriver(options);
// Open website
driver.get("https://www.geeksforgeeks.org/");
// Find element using link text
WebElement link_text = driver.findElement(
By.linkText("Data Structures and Algorithms")
);
// Click the link (optional but recommended)
link_text.click();
// Close browser
driver.quit();
}
}
Step 6: Click on an Element
In this step, we perform a click action on a web element using Selenium’s click() method. Since the browser runs in headless mode, we cannot visually verify the action, so we print the page title using getTitle() to confirm navigation to the next page.
- click(): Simulates a mouse click on a web element such as a link or button.
- getTitle(): Returns the title of the current webpage, useful for validation in automation testing.
// Import selenium libraries
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebElement;
public class selenium2 {
public static void main(String[] args) {
// Set ChromeDriver path
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Chrome options
ChromeOptions options = new ChromeOptions();
// Enable headless mode
options.addArguments("--headless");
// Set window size for headless mode
options.addArguments("window-size=1920,1080");
// Initialize driver
WebDriver driver = new ChromeDriver(options);
// Open website
driver.get("https://www.geeksforgeeks.org/");
// Find element
WebElement link_text = driver.findElement(
By.linkText("Data Structures and Algorithms")
);
// Click element
link_text.click();
// Print page title
System.out.println("Page Title: " + driver.getTitle());
// Close browser
driver.quit();
}
}
Output: Learn Data Structures and Algorithms | DSA Tutorial - GeeksforGeeks
Advantages Headless Browser Testing
Headless browser testing improves speed, efficiency, and scalability in automated testing.
- Resource Efficiency: Headless testing consumes fewer system resources because no graphical user interface is rendered.
- Cross-platform Compatibility: Tests behave consistently across different operating systems and platforms.
- Enhanced Development Speed: Tests execute faster in headless mode, providing quicker feedback to developers.
- Scalability: Headless browsers support parallel test execution, improving scalability and overall testing efficiency.