Selenium4: Chrome DevTools API
In Selenium 4, there is an Interface for Chrome DevTools API that lets you play with DevTools.
Here is a list of few things that we can achieve by using DevTools API with Selenium:
- Use Console capabilities
- Emulate network conditions
- Perform security operations
- Get performance and Metrics of our Browser/Network
The complete API can be found here: https://chromedevtools.github.io/devtools-protocol/
Showing below how we can listen to Chrome Console logs and close the browser using the “Devtools” interface.
public static void chromeDevTools() throws InterruptedException, IOException {
try {
chromeDevTools = driver.getDevTools();
chromeDevTools.createSession();
message = “Hi everyone, this is Dheeraj.”; driver.get(“https://testersdigest.blogspot.com”); // execute Script to write console message
driver.executeScript(“console.log(‘“ + message + “‘);”);
}
catch (Exception e) {
e.printStackTrace(); }
finally {
// Close browser gracefully.
chromeDevTools.send(new Command<>(“Browser.close”, ImmutableMap.of()));
}