Java bindings for Chrome DevTools Protocol version 102, providing programmatic access to Chrome browser debugging and automation capabilities
—
Handles runtime events like console API calls, JavaScript exceptions, and provides conversion utilities for Selenium's event system. This domain enables monitoring of browser runtime behavior and console activity.
Main class for handling runtime events and console operations. Extends the idealized Events interface to provide v102-specific implementations for console monitoring and exception handling.
/**
* Handles runtime events like console API calls and exceptions
*/
public class V102Events extends Events<ConsoleAPICalled, ExceptionThrown> {
/**
* Creates a new V102Events instance with the specified DevTools connection
* @param devtools DevTools connection instance (required)
*/
public V102Events(DevTools devtools);
/**
* Enables the Runtime domain for receiving events
* @return Command to enable runtime domain
*/
protected Command<Void> enableRuntime();
/**
* Disables the Runtime domain to stop receiving events
* @return Command to disable runtime domain
*/
protected Command<Void> disableRuntime();
/**
* Returns the console API called event for monitoring console activity
* @return Event for console API calls
*/
protected Event<ConsoleAPICalled> consoleEvent();
/**
* Returns the exception thrown event for monitoring JavaScript exceptions
* @return Event for exception notifications
*/
protected Event<ExceptionThrown> exceptionThrownEvent();
/**
* Converts CDP console event to Selenium console event
* @param event CDP console API called event
* @return Selenium ConsoleEvent with converted data
*/
protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event);
/**
* Converts CDP exception event to Selenium JavaScript exception
* @param event CDP exception thrown event
* @return JavascriptException with stack trace and message
*/
protected JavascriptException toJsException(ExceptionThrown event);
}Usage Examples:
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v102.V102Domains;
import org.openqa.selenium.devtools.events.ConsoleEvent;
import org.openqa.selenium.JavascriptException;
// Setup
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();
V102Domains domains = new V102Domains(devTools);
// Enable events domain
domains.events().enable();
// Listen for console events
devTools.addListener(domains.events().consoleEvent(), (consoleEvent) -> {
System.out.println("Console: " + consoleEvent.getType() + " - " + consoleEvent.getMessages());
});
// Listen for JavaScript exceptions
devTools.addListener(domains.events().exceptionThrownEvent(), (exceptionEvent) -> {
JavascriptException jsException = domains.events().toJsException(exceptionEvent);
System.out.println("JS Exception: " + jsException.getMessage());
});
// Navigate to page that will generate console output
driver.get("https://example.com");
// Execute JavaScript that logs to console
driver.executeScript("console.log('Hello from JavaScript');");
driver.executeScript("console.error('This is an error');");
// Cleanup
domains.events().disable();The V102Events class provides sophisticated conversion methods that transform low-level CDP events into Selenium's standardized event format.
/**
* Converts CDP console event to Selenium console event
* Handles timestamp conversion and argument processing
* @param event CDP ConsoleAPICalled event with raw data
* @return ConsoleEvent with processed arguments and timestamp
*/
protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event);The conversion process:
/**
* Converts CDP exception event to Selenium JavaScript exception
* Builds stack trace from CDP exception details
* @param event CDP ExceptionThrown event
* @return JavascriptException with message and stack trace
*/
protected JavascriptException toJsException(ExceptionThrown event);The conversion process:
The V102Events class interacts with several generated CDP protocol classes:
// Generated CDP runtime classes (available at runtime)
class Runtime {
static Command<Void> enable();
static Command<Void> disable();
static Event<ConsoleAPICalled> consoleAPICalled();
static Event<ExceptionThrown> exceptionThrown();
}
// Console event data
class ConsoleAPICalled {
String getType();
Timestamp getTimestamp();
List<RemoteObject> getArgs();
}
// Exception event data
class ExceptionThrown {
ExceptionDetails getExceptionDetails();
}
class ExceptionDetails {
String getText();
Optional<String> getUrl();
int getLineNumber();
Optional<StackTrace> getStackTrace();
Optional<RemoteObject> getException();
}
class StackTrace {
List<CallFrame> getCallFrames();
}
// Timestamp representation
class Timestamp {
JsonElement toJson();
}// Selenium event classes
class ConsoleEvent {
ConsoleEvent(String type, Instant timestamp, List<Object> messages);
String getType();
Instant getTimestamp();
List<Object> getMessages();
}
class JavascriptException extends RuntimeException {
JavascriptException(String message);
void setStackTrace(StackTraceElement[] stackTrace);
}
// Remote object representation
class RemoteObject {
RemoteObject(String type, Object value);
String getType();
Object getValue();
}// Monitor all console activity
devTools.addListener(domains.events().consoleEvent(), (event) -> {
ConsoleEvent consoleEvent = domains.events().toConsoleEvent(event);
switch (consoleEvent.getType()) {
case "log":
System.out.println("LOG: " + consoleEvent.getMessages());
break;
case "error":
System.err.println("ERROR: " + consoleEvent.getMessages());
break;
case "warn":
System.out.println("WARN: " + consoleEvent.getMessages());
break;
}
});// Monitor JavaScript exceptions
devTools.addListener(domains.events().exceptionThrownEvent(), (event) -> {
JavascriptException exception = domains.events().toJsException(event);
System.err.println("JavaScript Exception: " + exception.getMessage());
exception.printStackTrace();
});Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v102