Java bindings for Chrome DevTools Protocol version 101, enabling browser automation and debugging capabilities through CDP integration
—
The events domain provides real-time monitoring of browser console output and JavaScript runtime exceptions, essential for debugging and monitoring web application behavior during automation.
Main events handler that extends the base Events class with version-specific CDP implementations.
/**
* Handles console events and JavaScript exceptions for CDP version 101
* Extends Events with ConsoleAPICalled and ExceptionThrown event types
*/
public class V101Events extends Events<ConsoleAPICalled, ExceptionThrown> {
/**
* Creates a new events handler instance
* @param devtools DevTools instance for CDP communication
*/
public V101Events(DevTools devtools);
}Inherited Methods from Events Base Class:
/**
* Add a listener for console API calls (console.log, console.warn, etc.)
* @param listener Consumer that receives ConsoleEvent objects
*/
public void addConsoleListener(Consumer<ConsoleEvent> listener);
/**
* Add a listener for JavaScript exceptions and runtime errors
* @param listener Consumer that receives JavascriptException objects
*/
public void addJavascriptExceptionListener(Consumer<JavascriptException> listener);
/**
* Disable the events domain and clear all listeners
* Stops receiving console and exception events
*/
public void disable();Usage Examples:
import org.openqa.selenium.devtools.v101.V101Events;
import org.openqa.selenium.devtools.events.ConsoleEvent;
import org.openqa.selenium.JavascriptException;
// Create events handler
V101Events events = new V101Events(devTools);
// Listen for console messages
events.addConsoleListener(event -> {
System.out.println("Console " + event.getType() + ": " +
String.join(" ", event.getMessages()));
System.out.println("Timestamp: " + event.getTimestamp());
System.out.println("Arguments: " + event.getArgs());
});
// Listen for JavaScript exceptions
events.addJavascriptExceptionListener(exception -> {
System.err.println("JavaScript Error: " + exception.getMessage());
exception.printStackTrace();
});
// Navigate to page that will generate console output
driver.get("https://example.com");
// Execute JavaScript that will trigger events
driver.executeScript("console.log('Hello from browser'); console.warn('Warning message');");
driver.executeScript("throw new Error('Test exception');");
// Clean up when done
events.disable();Represents a browser console event (console.log, console.warn, console.error, etc.).
/**
* Represents a browser console API call event
* Contains the console message type, timestamp, and arguments
*/
public class ConsoleEvent {
/**
* Get the type of console call (log, warn, error, info, debug, etc.)
* @return Console call type as string
*/
public String getType();
/**
* Get the timestamp when the console event occurred
* @return Instant representing the event time
*/
public Instant getTimestamp();
/**
* Get the raw arguments passed to the console method
* @return List of objects representing console arguments
*/
public List<Object> getArgs();
/**
* Get formatted string messages from the console call
* @return List of formatted message strings
*/
public List<String> getMessages();
}Console Event Types:
"log" - console.log() calls"warn" - console.warn() calls"error" - console.error() calls"info" - console.info() calls"debug" - console.debug() calls"trace" - console.trace() calls"dir" - console.dir() calls"table" - console.table() callsUsage Example:
events.addConsoleListener(event -> {
switch (event.getType()) {
case "error":
System.err.println("❌ Error: " + String.join(" ", event.getMessages()));
break;
case "warn":
System.out.println("⚠️ Warning: " + String.join(" ", event.getMessages()));
break;
case "log":
case "info":
System.out.println("ℹ️ Info: " + String.join(" ", event.getMessages()));
break;
default:
System.out.println("📝 " + event.getType() + ": " + String.join(" ", event.getMessages()));
}
});Represents a JavaScript runtime exception that occurred in the browser.
/**
* Represents a JavaScript exception that occurred in the browser
* Extends RuntimeException with additional browser-specific information
*/
public class JavascriptException extends RuntimeException {
/**
* Creates a JavaScript exception with the specified message
* @param message Exception message from the browser
*/
public JavascriptException(String message);
/**
* Get the exception message
* @return Exception message string
*/
public String getMessage();
/**
* Get the stack trace elements from the browser
* @return Array of StackTraceElement objects representing the browser stack
*/
public StackTraceElement[] getStackTrace();
}Usage Example:
events.addJavascriptExceptionListener(exception -> {
System.err.println("JavaScript Exception occurred:");
System.err.println("Message: " + exception.getMessage());
System.err.println("Browser Stack Trace:");
for (StackTraceElement element : exception.getStackTrace()) {
System.err.println(" at " + element.getMethodName() +
" (" + element.getFileName() + ":" + element.getLineNumber() + ")");
}
});The underlying CDP protocol event types that are converted to the higher-level ConsoleEvent and JavascriptException objects:
/**
* CDP Runtime.consoleAPICalled event data
* Raw event data from the Chrome DevTools Protocol
*/
public class ConsoleAPICalled {
public ConsoleAPIType getType();
public List<RemoteObject> getArgs();
public Timestamp getTimestamp();
public Optional<ExecutionContextId> getExecutionContextId();
public Optional<StackTrace> getStackTrace();
}
/**
* CDP Runtime.exceptionThrown event data
* Raw event data from the Chrome DevTools Protocol
*/
public class ExceptionThrown {
public Timestamp getTimestamp();
public ExceptionDetails getExceptionDetails();
}
/**
* CDP exception details containing error information
*/
public class ExceptionDetails {
public Integer getExceptionId();
public String getText();
public Integer getLineNumber();
public Integer getColumnNumber();
public Optional<String> getUrl();
public Optional<StackTrace> getStackTrace();
public Optional<RemoteObject> getException();
}The V101Events class internally uses these CDP Runtime domain commands:
// From org.openqa.selenium.devtools.v101.runtime.Runtime
public static Command<Void> enable();
public static Command<Void> disable();
public static Event<ConsoleAPICalled> consoleAPICalled();
public static Event<ExceptionThrown> exceptionThrown();Internal Event Processing:
Runtime.consoleAPICalled and Runtime.exceptionThrown eventsError Handling:
// Robust event handling with error recovery
events.addConsoleListener(event -> {
try {
processConsoleEvent(event);
} catch (Exception e) {
System.err.println("Error processing console event: " + e.getMessage());
}
});
events.addJavascriptExceptionListener(exception -> {
try {
logJavaScriptError(exception);
// Could potentially recover or ignore certain types of JS errors
} catch (Exception e) {
System.err.println("Error processing JS exception: " + e.getMessage());
}
});Performance Considerations:
// Efficient filtering of console events
events.addConsoleListener(event -> {
// Only process error and warning events for performance
if ("error".equals(event.getType()) || "warn".equals(event.getType())) {
processImportantEvent(event);
}
});Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v101