Selenium DevTools
Selenium DevTools gives your test the same powers that a developer uses in the browser's inspection panel. Regular Selenium acts like a visitor who clicks and types. DevTools access acts like a mechanic who lifts the hood. The test can watch network traffic, read console messages, change the device size, and imitate slow internet. Selenium 4 supports this feature for Chrome and Edge through the Chrome DevTools Protocol, usually shortened to CDP.
Connection Diagram
Test Script | WebDriver commands (click, type) v ChromeDriver ------------------> Chrome Browser ^ ^ | DevTools commands (network, | | console, emulation) | +---------------------------------+
Opening a DevTools Session
Create a Chrome driver, ask for the DevTools object, and start a session. All later commands travel through that session.
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();The driver variable must use the ChromeDriver type, not the general WebDriver type. Only the browser-specific class exposes getDevTools().
Capturing Network Traffic
Every image, script, and data call crosses the network. A listener prints each request address and each response status. Testers use this view to find broken links and slow calls.
import org.openqa.selenium.devtools.v120.network.Network;
import java.util.Optional;
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.addListener(Network.responseReceived(), response -> {
System.out.println(response.getResponse().getStatus()
+ " " + response.getResponse().getUrl());
});
driver.get("https://example.com");The version number in the import, here v120, must match the browser's major version. Selenium ships several versions side by side.
200 https://example.com/ 200 https://example.com/style.css 404 https://example.com/missing-logo.png <-- broken image found
Reading Console Messages
Websites print errors into the browser console. A silent page may still hide script errors that users never see. A log listener collects those messages during a test run, and the test fails when an unexpected error appears.
import org.openqa.selenium.devtools.v120.log.Log;
devTools.send(Log.enable());
devTools.addListener(Log.entryAdded(), entry ->
System.out.println(entry.getLevel() + ": " + entry.getText()));Imitating a Mobile Screen
Device emulation changes the screen size and pixel density. The page responds as it would on a phone.
import org.openqa.selenium.devtools.v120.emulation.Emulation;
import java.util.Optional;
devTools.send(Emulation.setDeviceMetricsOverride(
390, 844, 3, true,
Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(),
Optional.empty()));
driver.get("https://example.com");The values 390 and 844 set width and height, and 3 sets the pixel ratio.
Simulating a Slow Network
Many users browse on weak connections. Network throttling slows the speed to check loading messages and timeouts.
Normal: page ready in 1.2 seconds Slow 3G: page ready in 9.8 seconds -> does the spinner appear? Offline: page fails -> does a friendly message appear?
Blocking Resources
A test can block images or advertisement scripts to speed up runs. Blocking proves that the page still works when a third-party file fails to load.
Other Selenium 4 Helpers
| Feature | Use |
|---|---|
| Network interceptor | Replace a server response with fake data |
| Geolocation override | Pretend the browser sits in another city |
| Basic authentication | Pass credentials without a pop-up |
| Performance metrics | Read memory and load timing |
Limits and Cautions
- CDP works on Chromium browsers only. Firefox uses a different protocol called BiDi.
- Version mismatches between imports and browser produce class-not-found errors.
- CDP commands may change between Chrome releases.
- Network capture adds overhead. Enable it only in tests that need it.
Key Points
- DevTools extends Selenium beyond clicking and typing.
- Use ChromeDriver or EdgeDriver to reach the DevTools object.
- Watch network and console data to catch hidden defects.
- Emulate devices and slow networks to test real-world conditions.
Everyday Use Cases
A shopper complains that a page feels slow. A tester records network traffic and finds a three-megabyte image that loads on every visit. A support team hears about a blank checkout page. A console listener reveals a script error that appears only on Safari-sized screens. A product owner asks how the site behaves in a café with weak Wi-Fi. Network throttling answers that question with evidence. Each case uses DevTools to see what the visitor feels but cannot describe.
Choosing Between Regular Selenium and DevTools
Regular commands remain the right tool for clicks, typing, and visible checks. DevTools fits behind-the-scenes questions: which calls the page makes, which errors it prints, and how it reacts to device and network changes. Keep both inside one test when a scenario needs a visible action plus a hidden check, such as clicking Pay and confirming that the payment call returns status 200.
