Chrome DevTools Protocol version 115 bindings for Selenium Java WebDriver enabling programmatic browser debugging capabilities
—
Comprehensive event handling for console messages, JavaScript exceptions, and runtime events with idealized conversion and filtering capabilities through the v115Events class.
Handles runtime events including console API calls and JavaScript exceptions with automatic conversion to idealized event types.
/**
* Runtime events handler providing console and exception event processing
* Extends the idealized Events class with CDP v115 specific implementations
*/
public class v115Events extends Events<ConsoleAPICalled, ExceptionThrown> {
/**
* Creates events domain with DevTools connection
* @param devtools DevTools instance for CDP communication
*/
public v115Events(DevTools devtools);
/**
* Enable runtime domain for event collection
* @return Command to enable runtime domain
*/
protected Command<Void> enableRuntime();
/**
* Disable runtime domain and stop event collection
* @return Command to disable runtime domain
*/
protected Command<Void> disableRuntime();
/**
* Get console API called event stream
* @return Event stream for console API calls
*/
protected Event<ConsoleAPICalled> consoleEvent();
/**
* Get exception thrown event stream
* @return Event stream for JavaScript exceptions
*/
protected Event<ExceptionThrown> exceptionThrownEvent();
/**
* Convert CDP console event to idealized ConsoleEvent
* @param event CDP console API called event
* @return Idealized console event with converted timestamp and arguments
*/
protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event);
/**
* Convert CDP exception event to JavascriptException
* @param event CDP exception thrown event
* @return JavascriptException with stack trace and message
*/
protected JavascriptException toJsException(ExceptionThrown event);
}Add listeners for console messages with automatic event conversion and filtering.
/**
* Add console event listener (inherited from Events base class)
* @param listener Consumer to handle console events
*/
public void addConsoleListener(Consumer<ConsoleEvent> listener);Usage Examples:
import org.openqa.selenium.devtools.v115.v115Events;
import org.openqa.selenium.devtools.events.ConsoleEvent;
// Create events domain
v115Events events = new v115Events(devTools);
// Listen to all console events
events.addConsoleListener(event -> {
System.out.println("Console Event:");
System.out.println(" Type: " + event.getType());
System.out.println(" Timestamp: " + event.getTimestamp());
System.out.println(" Messages: " + String.join(", ", event.getMessages()));
System.out.println(" Args: " + event.getArgs().size() + " arguments");
});
// Navigate to trigger console events
driver.get("https://example.com");
driver.executeScript("console.log('Hello from JavaScript!');");
driver.executeScript("console.warn('Warning message');");
driver.executeScript("console.error('Error message');");Monitor and handle JavaScript runtime exceptions with detailed stack trace information.
/**
* Add JavaScript exception listener (inherited from Events base class)
* @param listener Consumer to handle JavaScript exceptions
*/
public void addJavascriptExceptionListener(Consumer<JavascriptException> listener);Usage Examples:
import org.openqa.selenium.JavascriptException;
// Listen to JavaScript exceptions
events.addJavascriptExceptionListener(exception -> {
System.err.println("JavaScript Exception: " + exception.getMessage());
// Print stack trace
for (StackTraceElement element : exception.getStackTrace()) {
System.err.println(" at " + element.getClassName() + "." +
element.getMethodName() +
"(" + element.getFileName() + ":" +
element.getLineNumber() + ")");
}
});
// Execute JavaScript that throws an exception
driver.executeScript("throw new Error('Something went wrong!');");Enable and disable runtime event collection as needed.
/**
* Disable event listening (inherited from Events base class)
* Stops runtime domain and clears all listeners
*/
public void disable();Usage Examples:
// Events are automatically enabled when listeners are added
events.addConsoleListener(event -> { /* handle */ });
// Manually disable when done
events.disable();The toConsoleEvent method performs comprehensive conversion from CDP events to idealized ConsoleEvent objects:
// CDP ConsoleAPICalled -> ConsoleEvent conversion includes:
// - Timestamp conversion from CDP Timestamp to Java Instant
// - Argument conversion from CDP RemoteObject to idealized RemoteObject
// - Type string preservation (log, warn, error, info, debug, etc.)
// - Message extraction and formattingThe toJsException method creates JavascriptException instances with full context:
// CDP ExceptionThrown -> JavascriptException conversion includes:
// - Message extraction from exception details or remote object description
// - Stack trace construction from CDP CallFrame information
// - Line number and URL preservation
// - Function name mapping when available
// - Fallback stack trace element creation for incomplete dataThe events system handles all standard console API types:
When CDP exception events lack stack trace information, the system creates fallback stack trace elements using available details:
// Fallback stack trace creation
StackTraceElement element = new StackTraceElement(
"unknown", // className
"unknown", // methodName
details.getUrl().orElse("unknown"), // fileName
details.getLineNumber() // lineNumber
);Console event timestamps are converted with error handling for malformed CDP timestamps:
// Safe timestamp conversion with current time fallback
private long fromCdpTimestamp(Timestamp timestamp) {
try {
return Long.parseLong(timestamp.toString());
} catch (NumberFormatException e) {
return System.currentTimeMillis();
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v115