Selenium provides APIs and tooling for automating a real web browser and testing web applications through browser interactions. It works with WebDriver, which exposes a standard HTTP interface for sending commands to browser-specific drivers and reading browser state. Unlike HtmlUnit, Selenium handles modern JavaScript well, but requires a network-accessible test application, a running web server, and a full browser. section 4.5 section 4.6
A Spring Boot Selenium test commonly starts the application with @SpringBootTest(webEnvironment = RANDOM_PORT), creates a browser driver in @BeforeAll, and shuts it down in @AfterAll. Reusing one browser avoids startup cost but shares cookies and other local state across tests; delete cookies explicitly when isolation requires it. ChromeDriverService is replaceable with services for Firefox, Edge, or Safari, while Selenium can discover matching browser and driver executables or accept explicit paths. section 4.5.1
Testing an auto-complete field
Navigate to the application using the random port, locate the city search field with By.id("citySearch"), and send "jak" with sendKeys(). Locate the result container, collect child elements with a CSS selector, and assert the displayed cities and their order. An implicit wait lets Selenium retry element lookup for up to one second while the JavaScript-driven results appear. section 4.5.2
Listing 4.20 Selenium testing dependencies
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
</dependency>
Use the dependency declaration as the minimal Maven starting point for adding Selenium to test scope. section 4.5
Prefer Selenium when HTML or JavaScript behavior needs a real browser, especially for JavaScript-heavy applications. Use lighter mock-stack or client-based approaches for most API tests, and reserve Selenium for the browser capabilities those approaches cannot represent. section 4.6


