Headless mode runs a browser without displaying its graphical user interface. Selenium uses it to execute browser tests without opening visible windows, which suits continuous integration environments without displays and avoids local focus interruptions. section 4.5.1
Use ChromeOptions to add the "--headless" argument when constructing a ChromeDriver. Remove the option during debugging to watch Selenium interact with the browser in real time. section 4.5.1
Listing 4.21 Selenium test setup
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class SeleniumTests {
@LocalServerPort int port;
static ChromeDriverService driverService;
static WebDriver driver;
private String baseUrl;
@BeforeAll
static void startChromeBrowser() throws Exception {
driverService = ChromeDriverService.createDefaultService(); #1
driverService.start(); #1
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(driverService, options); #2
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1)); #3
}
@AfterAll
static void stopChromeBrowser() {
driver.quit(); #4
driverService.stop(); #4
}
}
Shows the complete Selenium setup: creating the driver service, enabling headless Chrome, configuring an implicit wait, and shutting down both resources. section 4.5.1

