How to run selenium tests in headless mode

We can run selenium tests in headless mode with setting chrome options. In headless mode browser does not visible during execution and execution is faster compare to regular execution. We can run Selenium tests in headless mode by configuring Chrome options. In headless mode, the browser runs without a graphical user interface, meaning it is not visible during execution. This typically results in faster execution compared to running tests in normal (UI) mode.We can run selenium tests in headless mode with setting chrome options. In headless mode browser does not visible during execution and execution is faster compare to regular execution.

✅ Prerequisites:

  • Java installed (JDK 8 or later)
  • Selenium Java bindings (e.g., via Maven or manually added JARs)
  • ChromeDriver compatible with your Chrome version
  • Chrome browser installed

Following is the script to run the tests in headless mode

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) {
// Automatically setup driver
System.setProperty(“webdriver.driver.chromedriver”,”path of the chrome driver”);

    // Configure Chrome options for headless mode
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless=new"); // 'new' is preferred for latest Chrome
    options.addArguments("--disable-gpu"); // Recommended for Windows
    options.addArguments("--window-size=1920,1080");
    options.addArguments("--no-sandbox");
    options.addArguments("--disable-dev-shm-usage");

    // Initialize WebDriver with headless options
    WebDriver driver = new ChromeDriver(options);

    try {
        driver.get("https://example.com");
        System.out.println("Page title is: " + driver.getTitle());
    } finally {
        driver.quit();
    }
}

}

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , CodesToolbox
Scroll to Top
0
Would love your thoughts, please comment.x
()
x