Log
This section contains the APIs related to logging.
Console logs
Listen to the console.log
events and register callbacks to process the event.
CompletableFuture<ConsoleLogEntry>future=newCompletableFuture<>();logInspector.onConsoleEntry(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("consoleLog")).click();ConsoleLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);
/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/LogTest.java
packagedev.selenium.bidirectional.webdriver_bidi;importdev.selenium.BaseTest;importjava.time.Duration;importjava.util.concurrent.*;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.bidi.module.LogInspector;importorg.openqa.selenium.bidi.log.ConsoleLogEntry;importorg.openqa.selenium.bidi.log.JavascriptLogEntry;importorg.openqa.selenium.bidi.log.LogLevel;importorg.openqa.selenium.bidi.log.StackTrace;importorg.openqa.selenium.firefox.FirefoxDriver;importorg.openqa.selenium.firefox.FirefoxOptions;importorg.openqa.selenium.support.ui.WebDriverWait;class LogTestextendsBaseTest{@BeforeEachpublicvoidsetup(){FirefoxOptionsoptions=newFirefoxOptions();options.setCapability("webSocketUrl",true);driver=newFirefoxDriver(options);}@TestvoidtestListenToConsoleLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<ConsoleLogEntry>future=newCompletableFuture<>();logInspector.onConsoleEntry(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("consoleLog")).click();ConsoleLogEntrylogEntry=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());}}@TestvoidtestListenToJavascriptLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptLog(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());Assertions.assertEquals(LogLevel.ERROR,logEntry.getLevel());}}@TestvoidtestListenToJavascriptErrorLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptException(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());}}@TestvoidtestRetrieveStacktraceForALog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptException(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("logWithStacktrace")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);StackTracestackTrace=logEntry.getStackTrace();Assertions.assertNotNull(stackTrace);Assertions.assertEquals(4,stackTrace.getCallFrames().size());}}@TestvoidtestListenToLogsWithMultipleConsumers()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>completableFuture1=newCompletableFuture<>();logInspector.onJavaScriptLog(completableFuture1::complete);CompletableFuture<JavascriptLogEntry>completableFuture2=newCompletableFuture<>();logInspector.onJavaScriptLog(completableFuture2::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=completableFuture1.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());logEntry=completableFuture2.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());}}}
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)
/examples/javascript/test/bidirectional/logInspector.spec.js
const assert = require("assert");
const firefox = require('selenium-webdriver/firefox');
const LogInspector = require('selenium-webdriver/bidi/logInspector');
const {Builder} = require("selenium-webdriver");
describe('Log Inspector', function () {
let driver
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
})
afterEach(async function () {
await driver.quit()
})
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()
})
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()
})
it('test retrieve stack trace for a 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()
const stackTrace = logEntry.stackTrace
assert.notEqual(stackTrace, null)
assert.equal(stackTrace.callFrames.length, 3)
await inspector.close()
})
it('test listen to logs with multiple consumers', async function () {
let logEntry1 = null
let logEntry2 = null
const inspector = await LogInspector(driver)
await inspector.onJavascriptException(function (log) {
logEntry1 = log
})
await inspector.onJavascriptException(function (log) {
logEntry2 = log
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
await driver.findElement({id: 'jsException'}).click()
assert.equal(logEntry1.text, 'Error: Not working')
assert.equal(logEntry1.type, 'javascript')
assert.equal(logEntry1.level, 'error')
assert.equal(logEntry2.text, 'Error: Not working')
assert.equal(logEntry2.type, 'javascript')
assert.equal(logEntry2.level, 'error')
await inspector.close()
})
})
JavaScript exceptions
Listen to the JS Exceptions and register callbacks to process the exception details.
logInspector.onJavaScriptException(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);
/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/LogTest.java
packagedev.selenium.bidirectional.webdriver_bidi;importdev.selenium.BaseTest;importjava.time.Duration;importjava.util.concurrent.*;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.bidi.module.LogInspector;importorg.openqa.selenium.bidi.log.ConsoleLogEntry;importorg.openqa.selenium.bidi.log.JavascriptLogEntry;importorg.openqa.selenium.bidi.log.LogLevel;importorg.openqa.selenium.bidi.log.StackTrace;importorg.openqa.selenium.firefox.FirefoxDriver;importorg.openqa.selenium.firefox.FirefoxOptions;importorg.openqa.selenium.support.ui.WebDriverWait;class LogTestextendsBaseTest{@BeforeEachpublicvoidsetup(){FirefoxOptionsoptions=newFirefoxOptions();options.setCapability("webSocketUrl",true);driver=newFirefoxDriver(options);}@TestvoidtestListenToConsoleLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<ConsoleLogEntry>future=newCompletableFuture<>();logInspector.onConsoleEntry(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("consoleLog")).click();ConsoleLogEntrylogEntry=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());}}@TestvoidtestListenToJavascriptLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptLog(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());Assertions.assertEquals(LogLevel.ERROR,logEntry.getLevel());}}@TestvoidtestListenToJavascriptErrorLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptException(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());}}@TestvoidtestRetrieveStacktraceForALog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptException(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("logWithStacktrace")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);StackTracestackTrace=logEntry.getStackTrace();Assertions.assertNotNull(stackTrace);Assertions.assertEquals(4,stackTrace.getCallFrames().size());}}@TestvoidtestListenToLogsWithMultipleConsumers()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>completableFuture1=newCompletableFuture<>();logInspector.onJavaScriptLog(completableFuture1::complete);CompletableFuture<JavascriptLogEntry>completableFuture2=newCompletableFuture<>();logInspector.onJavaScriptLog(completableFuture2::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=completableFuture1.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());logEntry=completableFuture2.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());}}}
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')
/examples/javascript/test/bidirectional/logInspector.spec.js
const assert = require("assert");
const firefox = require('selenium-webdriver/firefox');
const LogInspector = require('selenium-webdriver/bidi/logInspector');
const {Builder} = require("selenium-webdriver");
describe('Log Inspector', function () {
let driver
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
})
afterEach(async function () {
await driver.quit()
})
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()
})
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()
})
it('test retrieve stack trace for a 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()
const stackTrace = logEntry.stackTrace
assert.notEqual(stackTrace, null)
assert.equal(stackTrace.callFrames.length, 3)
await inspector.close()
})
it('test listen to logs with multiple consumers', async function () {
let logEntry1 = null
let logEntry2 = null
const inspector = await LogInspector(driver)
await inspector.onJavascriptException(function (log) {
logEntry1 = log
})
await inspector.onJavascriptException(function (log) {
logEntry2 = log
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
await driver.findElement({id: 'jsException'}).click()
assert.equal(logEntry1.text, 'Error: Not working')
assert.equal(logEntry1.type, 'javascript')
assert.equal(logEntry1.level, 'error')
assert.equal(logEntry2.text, 'Error: Not working')
assert.equal(logEntry2.type, 'javascript')
assert.equal(logEntry2.level, 'error')
await inspector.close()
})
})
Listen to JS Logs
Listen to all JS logs at all levels and register callbacks to process the log.
logInspector.onJavaScriptLog(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);
/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/LogTest.java
packagedev.selenium.bidirectional.webdriver_bidi;importdev.selenium.BaseTest;importjava.time.Duration;importjava.util.concurrent.*;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.bidi.module.LogInspector;importorg.openqa.selenium.bidi.log.ConsoleLogEntry;importorg.openqa.selenium.bidi.log.JavascriptLogEntry;importorg.openqa.selenium.bidi.log.LogLevel;importorg.openqa.selenium.bidi.log.StackTrace;importorg.openqa.selenium.firefox.FirefoxDriver;importorg.openqa.selenium.firefox.FirefoxOptions;importorg.openqa.selenium.support.ui.WebDriverWait;class LogTestextendsBaseTest{@BeforeEachpublicvoidsetup(){FirefoxOptionsoptions=newFirefoxOptions();options.setCapability("webSocketUrl",true);driver=newFirefoxDriver(options);}@TestvoidtestListenToConsoleLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<ConsoleLogEntry>future=newCompletableFuture<>();logInspector.onConsoleEntry(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("consoleLog")).click();ConsoleLogEntrylogEntry=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());}}@TestvoidtestListenToJavascriptLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptLog(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());Assertions.assertEquals(LogLevel.ERROR,logEntry.getLevel());}}@TestvoidtestListenToJavascriptErrorLog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptException(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());}}@TestvoidtestRetrieveStacktraceForALog()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>future=newCompletableFuture<>();logInspector.onJavaScriptException(future::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("logWithStacktrace")).click();JavascriptLogEntrylogEntry=future.get(5,TimeUnit.SECONDS);StackTracestackTrace=logEntry.getStackTrace();Assertions.assertNotNull(stackTrace);Assertions.assertEquals(4,stackTrace.getCallFrames().size());}}@TestvoidtestListenToLogsWithMultipleConsumers()throwsExecutionException,InterruptedException,TimeoutException{try(LogInspectorlogInspector=newLogInspector(driver)){CompletableFuture<JavascriptLogEntry>completableFuture1=newCompletableFuture<>();logInspector.onJavaScriptLog(completableFuture1::complete);CompletableFuture<JavascriptLogEntry>completableFuture2=newCompletableFuture<>();logInspector.onJavaScriptLog(completableFuture2::complete);driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");driver.findElement(By.id("jsException")).click();JavascriptLogEntrylogEntry=completableFuture1.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());logEntry=completableFuture2.get(5,TimeUnit.SECONDS);Assertions.assertEquals("Error: Not working",logEntry.getText());Assertions.assertEquals("javascript",logEntry.getType());}}}
Last modified December 16, 2024: [java] Remove a wrong code example and update code lines (#2104) (6b3cccc0e32)