0
# Console and Runtime Events
1
2
Runtime event monitoring for the Chrome DevTools Protocol v85, including console API calls, JavaScript exceptions, and error handling. This functionality allows you to capture browser console output and JavaScript runtime errors in real-time.
3
4
## Capabilities
5
6
### V85Events Class
7
8
The main class for runtime event handling extending the base Events functionality.
9
10
```java { .api }
11
/**
12
* Runtime event monitoring for Chrome DevTools v85
13
*/
14
public class V85Events extends Events<ConsoleAPICalled, ExceptionThrown> {
15
/**
16
* Creates a new V85Events instance
17
* @param devtools - DevTools session for sending commands
18
*/
19
public V85Events(DevTools devtools);
20
}
21
```
22
23
### Runtime Domain Control
24
25
Enable and disable the Runtime domain for event monitoring.
26
27
```java { .api }
28
/**
29
* Enables the Runtime domain for event monitoring
30
* @return Command to enable runtime domain
31
*/
32
protected Command<Void> enableRuntime();
33
34
/**
35
* Disables the Runtime domain
36
* @return Command to disable runtime domain
37
*/
38
protected Command<Void> disableRuntime();
39
```
40
41
### Console Event Monitoring
42
43
Monitor console API calls from the browser (console.log, console.error, etc.).
44
45
```java { .api }
46
/**
47
* Event fired when console API is called in the browser
48
* @return Event for console API calls
49
*/
50
protected Event<ConsoleAPICalled> consoleEvent();
51
52
/**
53
* Converts CDP console event to Selenium ConsoleEvent
54
* @param event - CDP console API called event
55
* @return Selenium ConsoleEvent with processed data
56
*/
57
protected ConsoleEvent toConsoleEvent(ConsoleAPICalled event);
58
```
59
60
**Usage Example:**
61
62
```java
63
import org.openqa.selenium.devtools.v85.V85Events;
64
import org.openqa.selenium.devtools.events.ConsoleEvent;
65
66
V85Events events = (V85Events) domains.events();
67
68
// Listen for console events using high-level API
69
events.addConsoleListener(event -> {
70
System.out.printf("[%s] %s: %s%n",
71
event.getTimestamp(),
72
event.getType(),
73
event.getMessages()
74
);
75
});
76
77
// Now when the browser executes:
78
// console.log("Hello from browser");
79
// console.error("An error occurred");
80
// You'll see the output in Java
81
```
82
83
### Exception Monitoring
84
85
Monitor JavaScript exceptions and runtime errors.
86
87
```java { .api }
88
/**
89
* Event fired when a JavaScript exception is thrown
90
* @return Event for JavaScript exceptions
91
*/
92
protected Event<ExceptionThrown> exceptionThrownEvent();
93
94
/**
95
* Converts CDP exception event to Selenium JavascriptException
96
* @param event - CDP exception thrown event
97
* @return JavascriptException with stack trace and error details
98
*/
99
protected JavascriptException toJsException(ExceptionThrown event);
100
```
101
102
**Usage Example:**
103
104
```java
105
// Listen for JavaScript exceptions using high-level API
106
events.addJavascriptExceptionListener(jsException -> {
107
System.err.println("JavaScript Exception: " + jsException.getMessage());
108
109
// Print stack trace
110
for (StackTraceElement element : jsException.getStackTrace()) {
111
System.err.println(" at " + element);
112
}
113
114
// Handle the exception (log, notify, etc.)
115
handleJavaScriptError(jsException);
116
});
117
```
118
119
### Complete Event Monitoring Setup
120
121
```java
122
import org.openqa.selenium.chrome.ChromeDriver;
123
import org.openqa.selenium.devtools.DevTools;
124
import org.openqa.selenium.devtools.v85.V85Domains;
125
126
ChromeDriver driver = new ChromeDriver();
127
DevTools devTools = driver.getDevTools();
128
devTools.createSession();
129
130
V85Domains domains = new V85Domains(devTools);
131
V85Events events = (V85Events) domains.events();
132
133
// Set up console monitoring using high-level API
134
events.addConsoleListener(event -> {
135
switch (event.getType()) {
136
case "log":
137
System.out.println("[LOG] " + event.getMessages());
138
break;
139
case "warn":
140
System.out.println("[WARN] " + event.getMessages());
141
break;
142
case "error":
143
System.err.println("[ERROR] " + event.getMessages());
144
break;
145
case "info":
146
System.out.println("[INFO] " + event.getMessages());
147
break;
148
case "debug":
149
System.out.println("[DEBUG] " + event.getMessages());
150
break;
151
}
152
});
153
154
// Set up exception monitoring using high-level API
155
events.addJavascriptExceptionListener(jsException -> {
156
System.err.println("Uncaught JavaScript Exception:");
157
System.err.println("Message: " + jsException.getMessage());
158
159
// Log to file or monitoring system
160
logJavaScriptError(jsException);
161
});
162
163
// Navigate to page - events will be captured automatically
164
driver.get("https://example.com");
165
166
// Execute some JavaScript that will generate events
167
driver.executeScript("console.log('Test log message');");
168
driver.executeScript("console.error('Test error message');");
169
driver.executeScript("throw new Error('Test exception');");
170
```
171
172
## CDP Model Classes
173
174
### ConsoleAPICalled
175
176
Event data for console API calls from the browser.
177
178
```java { .api }
179
/**
180
* Console API call event from Chrome DevTools
181
*/
182
public class ConsoleAPICalled {
183
/**
184
* Gets the type of console call (log, error, warn, etc.)
185
* @return Console call type
186
*/
187
public ConsoleApiType getType();
188
189
/**
190
* Gets the arguments passed to the console function
191
* @return List of remote objects representing the arguments
192
*/
193
public List<RemoteObject> getArgs();
194
195
/**
196
* Gets the timestamp when the console call occurred
197
* @return Timestamp of the call
198
*/
199
public Timestamp getTimestamp();
200
201
/**
202
* Gets the execution context ID where the call occurred
203
* @return Execution context ID
204
*/
205
public Optional<Integer> getExecutionContextId();
206
207
/**
208
* Gets the stack trace if available
209
* @return Stack trace information
210
*/
211
public Optional<StackTrace> getStackTrace();
212
}
213
214
/**
215
* Console API call types
216
*/
217
public enum ConsoleApiType {
218
LOG, DEBUG, INFO, ERROR, WARNING, DIR, DIRXML, TABLE, TRACE,
219
CLEAR, STARTGROUP, STARTGROUPCOLLAPSED, ENDGROUP, ASSERT,
220
PROFILE, PROFILEEND, COUNT, TIMEEND
221
}
222
```
223
224
### ExceptionThrown
225
226
Event data for JavaScript exceptions.
227
228
```java { .api }
229
/**
230
* JavaScript exception event from Chrome DevTools
231
*/
232
public class ExceptionThrown {
233
/**
234
* Gets the exception details
235
* @return Exception details with stack trace and error info
236
*/
237
public ExceptionDetails getExceptionDetails();
238
239
/**
240
* Gets the timestamp when the exception occurred
241
* @return Exception timestamp
242
*/
243
public Timestamp getTimestamp();
244
}
245
```
246
247
### ExceptionDetails
248
249
Detailed information about a JavaScript exception.
250
251
```java { .api }
252
/**
253
* Detailed exception information
254
*/
255
public class ExceptionDetails {
256
/**
257
* Gets the exception ID
258
* @return Exception identifier
259
*/
260
public Integer getExceptionId();
261
262
/**
263
* Gets the exception text/message
264
* @return Exception message
265
*/
266
public String getText();
267
268
/**
269
* Gets the line number where the exception occurred
270
* @return Line number
271
*/
272
public Integer getLineNumber();
273
274
/**
275
* Gets the column number where the exception occurred
276
* @return Column number
277
*/
278
public Integer getColumnNumber();
279
280
/**
281
* Gets the script ID where the exception occurred
282
* @return Script ID
283
*/
284
public Optional<String> getScriptId();
285
286
/**
287
* Gets the URL where the exception occurred
288
* @return URL of the script
289
*/
290
public Optional<String> getUrl();
291
292
/**
293
* Gets the stack trace for the exception
294
* @return Stack trace information
295
*/
296
public Optional<StackTrace> getStackTrace();
297
298
/**
299
* Gets the exception object details
300
* @return Remote object representing the exception
301
*/
302
public Optional<RemoteObject> getException();
303
}
304
```
305
306
### StackTrace
307
308
Stack trace information for exceptions and console calls.
309
310
```java { .api }
311
/**
312
* JavaScript stack trace information
313
*/
314
public class StackTrace {
315
/**
316
* Gets the description of the stack trace
317
* @return Stack trace description
318
*/
319
public Optional<String> getDescription();
320
321
/**
322
* Gets the call frames in the stack trace
323
* @return List of call frames from bottom to top
324
*/
325
public List<CallFrame> getCallFrames();
326
327
/**
328
* Gets the parent stack trace if available
329
* @return Parent stack trace for async operations
330
*/
331
public Optional<StackTrace> getParent();
332
}
333
334
/**
335
* Single call frame in a stack trace
336
*/
337
public class CallFrame {
338
/**
339
* Gets the function name
340
* @return Function name or empty string for anonymous functions
341
*/
342
public String getFunctionName();
343
344
/**
345
* Gets the script ID
346
* @return Script identifier
347
*/
348
public String getScriptId();
349
350
/**
351
* Gets the URL of the script
352
* @return Script URL
353
*/
354
public String getUrl();
355
356
/**
357
* Gets the line number (0-based)
358
* @return Line number in the script
359
*/
360
public Integer getLineNumber();
361
362
/**
363
* Gets the column number (0-based)
364
* @return Column number in the line
365
*/
366
public Integer getColumnNumber();
367
}
368
```
369
370
### RemoteObject
371
372
Represents a JavaScript object or value from the browser.
373
374
```java { .api }
375
/**
376
* Remote object representation from the browser
377
*/
378
public class RemoteObject {
379
/**
380
* Gets the object type
381
* @return Object type (object, function, undefined, string, number, boolean, symbol, bigint)
382
*/
383
public RemoteObjectType getType();
384
385
/**
386
* Gets the subtype for objects
387
* @return Object subtype (array, null, node, regexp, date, map, set, etc.)
388
*/
389
public Optional<RemoteObjectSubtype> getSubtype();
390
391
/**
392
* Gets the class name for objects
393
* @return Class name (e.g., "Object", "Array", "HTMLElement")
394
*/
395
public Optional<String> getClassName();
396
397
/**
398
* Gets the primitive value
399
* @return Value for primitive types
400
*/
401
public Optional<Object> getValue();
402
403
/**
404
* Gets the string description of the object
405
* @return Human-readable description
406
*/
407
public Optional<String> getDescription();
408
409
/**
410
* Gets the unique object ID for non-primitive values
411
* @return Object ID for referencing the object
412
*/
413
public Optional<String> getObjectId();
414
}
415
```
416
417
## Advanced Event Filtering
418
419
```java
420
// Filter console events by type and level
421
devTools.addListener(events.consoleEvent(), consoleCall -> {
422
ConsoleEvent event = events.toConsoleEvent(consoleCall);
423
424
// Only process errors and warnings
425
if (event.getType().equals("error") || event.getType().equals("warn")) {
426
logImportantMessage(event);
427
}
428
429
// Filter by message content
430
String message = event.getMessages().toString();
431
if (message.contains("API") || message.contains("network")) {
432
handleNetworkRelatedLog(event);
433
}
434
});
435
436
// Filter exceptions by location
437
devTools.addListener(events.exceptionThrownEvent(), exceptionEvent -> {
438
JavascriptException jsException = events.toJsException(exceptionEvent);
439
440
// Only handle exceptions from our application code
441
boolean isAppException = Arrays.stream(jsException.getStackTrace())
442
.anyMatch(frame -> frame.getFileName().contains("app.js") ||
443
frame.getFileName().contains("main.js"));
444
445
if (isAppException) {
446
handleApplicationError(jsException);
447
}
448
});
449
```