Test Automation with
Selenium 5 and Java
Devoxx Belgium 2024
10 October 2024
Boni García
Universidad Carlos III de Madrid, Spain
boni.garcia@uc3m.es
2
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
What is Selenium?
3
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
What is Selenium?
(for software people)
https://app.wooclap.com/EUHMZU
What is Selenium?
4
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
What is Selenium?
5
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
https://www.selenium.dev/about/
Selenium WebDriver Selenium IDE Selenium Grid
Library Plugin Server
What is Selenium?
Selenium WebDriver is the heart of the Selenium project and it is
often known as simply Selenium
6
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Selenium is a browser automation
library
- Selenium is NOT a testing framework
- Selenium is NOT a testing library
What’s the difference
between library and
framework?
Is it possible to do
“testing” with
Selenium?
What is NOT Selenium?
7
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
A library is a collection of code
that developers can call using an
API to solve a given problem
A framework is collection of libraries,
tools, and best practices that provides
a structure for developing software
Library vs. Framework
8
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Navigate to URLs
Locating web elements (DOM)
Impersonate user actions (keyboard, mouse)
Execute JavaScript
Make screenshots
Manage browser size, position, history,
Manage browser APIs like web storage, user media, …
What can I do with Selenium?
9
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
https://github.com/bonigarcia/selenium-webdriver-java
What can I do with Selenium?
10
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
// Create object to drive Chrome
WebDriver driver = new ChromeDriver();
// Navigate to a website (e.g., Wikipedia)
driver.get("https://en.wikipedia.org/");
// Type "software testing" in the search box
WebElement searchBox = driver.findElement(By.name("search"));
searchBox.sendKeys("software testing");
// Click on search button
WebElement searchButton = driver
.findElement(By.className("cdx-search-input__end-button"));
searchButton.click();
// Close browser
driver.quit();
Selenium History
11
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
Selenium
Core
WebDriver
Selenium
WebDriver
(Selenium 2)
Selenium
RC
Selenium 3 Selenium 4
W3C WebDriver
W3C BiDi
Current Selenium stable version: 4.25 (released on September 23)
https://www.selenium.dev/blog/2024/selenium-4-25-released/
Selenium History
12
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Next major release (Selenium 5) is currently in development
https://github.com/SeleniumHQ/selenium/milestone/16
Selenium History
13
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Selenium Manager
WebDriver BiDi
There is no official
date for Selenium 5
Testing with Selenium
In addition to the Selenium library, to create Selenium tests, we
typically use a unit testing framework
14
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Test
Setup
Verify
Teardown
Test verdict
initialize
interact
get outcome
Exercise System Under
Test (SUT)
assert
finalize
Testing with Selenium Build Tool
15
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.25.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.3</version>
<scope>test</scope>
</dependency>
<dependencies>
dependencies {
testImplementation("org.seleniumhq.selenium:selenium-java:4.25.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.11.1")
testImplementation("org.assertj:assertj-core:3.26.3")
}
Testing with Selenium Browsers
We need one or more browsers to be driven with Selenium
1. Local browser
2. Remote browser
16
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
WebDriver chrome = new ChromeDriver();
WebDriver firefox = new FirefoxDriver();
WebDriver edge = new EdgeDriver();
WebDriver safari = new SafariDriver();
ChromeOptions options = new ChromeOptions();
WebDriver driver = new RemoteWebDriver(
new URL("http://server:4444/"), options);
Testing with Selenium Browsers
17
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Browser
Driver
chromedriver
geckodriver
W3C
WebDriver Native
communication
msedgedriver
Web app
under test
Selenium test
HTTP(s)
safaridriver
Selenium users must manage (download,
setup, and maintain) these drivers
(JSON over
HTTP)
class ChromeManualTest {
WebDriver driver;
@BeforeAll
static void setupClass() {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
}
@BeforeEach
void setup() {
driver = new ChromeDriver();
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
String title = driver.getTitle();
assertThat(title).contains("Selenium WebDriver");
}
@AfterEach
void teardown() {
driver.quit();
}
}
https://github.com/bonigarcia/selenium-examples
Testing with Selenium Hello World (Selenium 3)
18
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Driver Management
19
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Modern web browsers are evergreen
chromedriver
126.0.6478.182
Chrome 126
chromedriver
126.0.6478.182
Chrome 127
chromedriver
126.0.6478.182
Chrome 128
this version of chromedriver only supports
Chrome version 126
Automated Driver Management
20
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Automated driver management and other
helper features for Selenium WebDriver in
Java
https://bonigarcia.dev/webdrivermanager/
class ChromeWdmTest {
WebDriver driver;
@BeforeAll
static void setupClass() {
WebDriverManager.chromedriver().setup();
}
@BeforeEach
void setup() {
driver = new ChromeDriver();
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
String title = driver.getTitle();
assertThat(title).contains("Selenium WebDriver");
}
@AfterEach
void teardown() {
driver.quit();
}
}
Testing with Selenium Hello World (Selenium 4)
21
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Automated Driver Management
22
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
https://www.selenium.dev/blog/2021/selenium-survey-results/
In 2021, the Selenium project surveyed its users:
Selenium users
wanted batteries
included
I joined the Selenium project as a committer in August 2022
It is a CLI (Command-Line Interface) tool
It has been developed in Rust
It is shipped in each Selenium release
Selenium Manager (beta)
Selenium Manager
23
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Selenium Manager automatically discovers, downloads, and
caches the drivers required by Selenium
Selenium Manager Automated Driver Management
24
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Chrome 128
chromedriver
128.0.6613.137
~/.cache/selenium
1. Browser version discovery
2. Driver version discovery
3. Driver download and cache
class ChromeBasicTest {
WebDriver driver;
@BeforeEach
void setup() {
driver = new ChromeDriver();
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
String title = driver.getTitle();
assertThat(title).contains("Selenium WebDriver");
}
@AfterEach
void teardown() {
driver.quit();
}
}
Selenium Manager Hello World (Selenium 5)
25
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Selenium Manager automatically discovers, downloads, and
caches the browsers driven with Selenium when these
browsers are not installed in the local system
Selenium Manager Automated Browser Management
26
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
*
*Requires admin permissions
Selenium Manager Automated Browser Management
27
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
class FirefoxBasicTest {
WebDriver driver;
@BeforeEach
void setup() {
driver = new FirefoxDriver();
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
String title = driver.getTitle();
assertThat(title).contains("Selenium WebDriver");
}
@AfterEach
void teardown() {
driver.quit();
}
}
If Firefox is not
available, Selenium
Manager will manage it
Selenium Manager Automated Browser Management
28
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
class ChromeVersionTest {
WebDriver driver;
@BeforeEach
void setup() {
ChromeOptions options = new ChromeOptions();
options.setBrowserVersion("beta");
driver = new ChromeDriver(options);
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
String title = driver.getTitle();
assertThat(title).contains("Selenium WebDriver");
}
@AfterEach
void teardown() {
driver.quit();
}
}
Specific browser versions
(including "beta", "dev",
or "nightly") are
supported
Another uses of Selenium Manager include:
Advanced configuration (with envs or global configuration file)
As a standalone CLI tool
https://www.selenium.dev/documentation/selenium_manager/
Selenium Manager Other Uses
29
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Selenium Manager (beta)
Is Selenium Manager a replacement for WebDriverManager?
For the use case of automated driver management, yes
WebDriverManager and Selenium Manager have different
features
Automated browser management is different in each project
WebDriverManager provides other additional features
Self-managed browsers in Docker containers
Monitoring features
Video recording
Selenium Manager & WebDriverManager
30
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
https://bonigarcia.dev/webdrivermanager/#webdrivermanager-and-selenium-manager
WebDriverManager Browser Management
31
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
class DockerChromeTest {
WebDriver driver;
WebDriverManager wdm;
@BeforeEach
void setupTest() {
wdm = WebDriverManager.chromedriver().browserInDocker();
driver = wdm.create();
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
assertThat(driver.getTitle()).contains("Selenium WebDriver");
}
@AfterEach
void teardown() {
wdm.quit();
}
}
WebDriverManager Browser Management
32
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
class DockerChromeRecordingTest {
WebDriver driver;
WebDriverManager wdm;
@BeforeEach
void setupTest() {
wdm = WebDriverManager.chromedriver().browserInDocker().enableRecording();
driver = wdm.create();
}
@Test
void test() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
assertThat(driver.getTitle()).contains("Selenium WebDriver");
}
@AfterEach
void teardown() {
wdm.quit();
}
}
Chrome DevTools is a set of web developer tools built into Chromium-
based browser (e.g., Chrome and Edge)
Chrome DevTools
33
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Inspect DOM, console,
network, sources,
performance, memory,
or security
The Chrome DevTools Protocol (CDP) is a set of APIs that allows us to
instrument, inspect, debug, and profile Chromium-based browsers
CDP is great for browser automation
Chrome DevTools Protocol
34
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Browser
Web app
under test
CDP HTTP(S)
Script using the
Puppeteer API
Puppeteer (a Node.js browser
automation library created by
Google) is based on CDP
Selenium supports CDP since version 4 to provide features not
available in the W3C WebDriver, such as
Network interception and monitoring
Device emulation
Performance metrics collection
Entire page screenshot
Security and authentication
Chrome DevTools Protocol and Selenium
35
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
https://github.com/bonigarcia/selenium-webdriver-java
See test examples
using CDP and
Selenium here
W3C WebDriver is evolving to a new specification called W3C
BiDi (BiDirectional)
Its goal is to combine the best of WebDriver and CDP
Browser
(JSON over
WebSocket)
W3C Bidi
BrowserDriver
W3C WebDriver
(JSON over HTTP)
WebDriver
Classic
WebDriver
BiDi
https://w3c.github.io/webdriver-bidi/
WebDriver BiDi
36
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
WebDriver BiDi features:
Bidirectional communication using a WebSocket (like CDP)
Event-driven architecture (e.g., for log gathering)
Support for modern web features (e.g., network interception)
WebDriver BiDi modules:
Browsing context
Actions
Scripting
Logging
Network
https://www.selenium.dev/documentation/webdriver/bidi/w3c/
WebDriver BiDi
37
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
@BeforeEach
void setup() {
ChromeOptions options = new ChromeOptions();
options.enableBiDi();
driver = new ChromeDriver(options);
}
Some of the WebDriver BiDi features are already available in
the latest versions of Selenium 4
@BeforeEach
void setup() {
FirefoxOptions options = new FirefoxOptions();
options.enableBiDi();
driver = new FirefoxDriver(options);
}
@BeforeEach
void setup() {
EdgeOptions options = new EdgeOptions();
options.enableBiDi();
driver = new EdgeDriver(options);
}
To use WebDriver BiDi in
Selenium, first we need to
enable it using browser options
WebDriver BiDi
38
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
@Test
void test() {
List<GenericLogEntry> logs = new ArrayList<>();
try (LogInspector logInspector = new LogInspector(driver)) {
logInspector.onGenericLog(logs::add);
logInspector.onConsoleEntry(logs::add);
logInspector.onJavaScriptException(logs::add);
}
driver.get(
"https://bonigarcia.dev/selenium-webdriver-java/console-logs.html");
new WebDriverWait(driver, Duration.ofSeconds(5))
.until(_d -> logs.size() > 3);
for (GenericLogEntry log :logs) {
System.out.println(log.getText());
}
}
WebDriver BiDi
39
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
https://github.com/bonigarcia/selenium-examples
https://www.selenium.dev/ecosystem/
Tools (e.g., testing frameworks) based on Selenium
WebDriver ecosystem
WebDriver BiDi ecosystem
The Selenium Ecosystem
40
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
Top-3 respondents will win a Selenium 20th anniversary t-shirt!
Quiz
41
Devoxx Belgium 2024 - Test Automation with Selenium 5 and Java
https://app.wooclap.com/JNZDZN
Test Automation with
Selenium 5 and Java
Thank you!
Boni García
Universidad Carlos III de Madrid, Spain
boni.garcia@uc3m.es
slides