Log
This section contains the APIs related to logging.
Console logs
Listen to the console.log events and register callbacks to process the event.
void testListenToConsoleLog() throws ExecutionException, InterruptedException, TimeoutException {
try (LogInspector logInspector = new LogInspector(driver)) {
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();
logInspector.onConsoleEntry(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
driver.findElement(By.id("consoleLog")).click();
ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals("Hello, world!", logEntry.getText());
Assertions.assertEquals(1, logEntry.getArgs().size());
Assertions.assertEquals("console", logEntry.getType());
Assertions.assertEquals("log", logEntry.getMethod());
Assertions.assertNull(logEntry.getStackTrace());
}
}
it 'adds console message handler' do
driver.navigate.to 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'
log_entries = []
driver.script.add_console_message_handler { |log| log_entries << log }
driver.find_element(id: 'consoleLog').click
wait.until { log_entries.any? }
expect(log_entries.first&.text).to eq 'Hello, world!'
end it('test listen to console log', async function () {
let logEntry = null
const inspector = await LogInspector(driver)
await inspector.onConsoleEntry(function (log) {
logEntry = log
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
await driver.findElement({id: 'consoleLog'}).click()
assert.equal(logEntry.text, 'Hello, world!')
assert.equal(logEntry.realm, null)
assert.equal(logEntry.type, 'console')
assert.equal(logEntry.level, 'info')
assert.equal(logEntry.method, 'log')
assert.equal(logEntry.stackTrace, null)
assert.equal(logEntry.args.length, 1)
await inspector.close()
})def test_add_console_log_handler(driver):
driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
log_entries = []
driver.script.add_console_message_handler(log_entries.append)
driver.find_element(By.ID, "consoleLog").click()
WebDriverWait(driver, 5).until(lambda _: log_entries)
assert log_entries[0].text == "Hello, world!"JavaScript exceptions
Listen to the JS Exceptions and register callbacks to process the exception details.
void testListenToJavascriptErrorLog()
throws ExecutionException, InterruptedException, TimeoutException {
try (LogInspector logInspector = new LogInspector(driver)) {
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
logInspector.onJavaScriptException(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
driver.findElement(By.id("jsException")).click();
JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals("Error: Not working", logEntry.getText());
Assertions.assertEquals("javascript", logEntry.getType());
}
}
it 'adds JavaScript error handler' do
driver.navigate.to 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html'
log_entries = []
driver.script.add_javascript_error_handler { |error| log_entries << error }
driver.find_element(id: 'jsException').click
wait.until { log_entries.any? }
expect(log_entries.first&.text).to eq 'Error: Not working'
end it('test listen to javascript error log', async function () {
let logEntry = null
const inspector = await LogInspector(driver)
await inspector.onJavascriptException(function (log) {
logEntry = log
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
await driver.findElement({id: 'jsException'}).click()
assert.equal(logEntry.text, 'Error: Not working')
assert.equal(logEntry.type, 'javascript')
assert.equal(logEntry.level, 'error')
await inspector.close()
})def test_add_js_exception_handler(driver):
driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
log_entries = []
driver.script.add_javascript_error_handler(log_entries.append)
driver.find_element(By.ID, "jsException").click()
WebDriverWait(driver, 5).until(lambda _: log_entries)
assert log_entries[0].text == "Error: Not working"Listen to JS Logs
Listen to all JS logs at all levels and register callbacks to process the log.
void testListenToJavascriptLog()
throws ExecutionException, InterruptedException, TimeoutException {
try (LogInspector logInspector = new LogInspector(driver)) {
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
logInspector.onJavaScriptLog(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
driver.findElement(By.id("jsException")).click();
JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals("Error: Not working", logEntry.getText());
Assertions.assertEquals("javascript", logEntry.getType());
Assertions.assertEquals(LogLevel.ERROR, logEntry.getLevel());
}
}




