Back to All Scenarios
Scenario 85 of 100
CI/CD
Beginner
Automating Web Browsers in Headless Execution Mode
πScenario Overview
Automating Web Browsers in Headless Execution Mode
Key Takeaways & Cheat Sheet
- βSet headless browser options to execute without visual GUIs
- βConfigure explicit window viewport dimensions to match desktop resolutions
- βDisable GPU and shared memory settings to prevent crashes
- βCapture failure screenshots to troubleshoot headless execution states
Short Direct Answer
Headless mode executes the browser in the background without rendering a visual GUI, saving memory and speeding up execution in CI pipelines. To configure this, add `--headless=new` to your browser options, disable GPU acceleration to prevent crashes, and set an explicit viewport size (like 1920x1080) to keep elements from collapsing.
β οΈ Senior Warning (Red Flag)
Avoid using legacy headless mode arguments (like --headless). Always use Chrome's modern headless engine (--headless=new) to ensure page behaviors match standard UI runs.
π‘ STAR Deep Dive Explanation & Pro Tip
Chrome's --headless=new mode uses the actual browser rendering engine, ensuring layouts and element states match standard desktop execution.
SeleniumAutomation.java
Selenium 4 + Javaimport org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
// β
Configuring Headless chrome for CI pipelines
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new"); // Use modern headless engine
options.addArguments("--window-size=1920,1080"); // Force desktop resolution
options.addArguments("--disable-gpu"); // Prevent graphics driver overhead
options.addArguments("--no-sandbox"); // Required in Linux Docker setups
WebDriver driver = new ChromeDriver(options);