Capturing JavaScript Log in browser using LogInspector of Selenium 4 BiDi

I made and published a GitHub repository:


Problem to solve

Using Katalon Studio, I want to open browser, navigate to a web page. The JavaScript in the page will write some messages in the console. I want to capture the console messages from browser into the Test Case script. How to do it?

Solution

Katalon Studio v10.x runs upon Selenium 4. Selenium 4 features BiDirectional functionalities which includes LogInspector. The LogInspector enables you to capture the console messages.

Description

Environment

  • macOS: 15.7.2
  • Katalon Studio Free: 10.4.2
  • Chrome browser: 143.0.7499.147
  • ChromeDriver: 143.0.7499.169
  • seleniumVersion: 4.34.0

I tested this using Katalon Studio V10.4.2. I do not know if it works on previous versions.

No additional external library is needed.

target HTML page

I made a target HTML:

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>Hello</title>
</head>
<body>
    <header></header>
    <main>
    <p id="main">Hello</p>
    </main>
    <footer></footer>
    <script><!--
    document.addEventListener('DOMContentLoaded', function() {
        let now = new Date();
        console.log('now: ' + now);
    });
    --></script>
</body>
</html>

When the page is loaded, the JavaScript will write a string message like now: Mon Dec 22 2025 19:09:59 GMT+0900 (日本標準時) into the console.

I want my Katalon Test Case script to be able to capture this message string.

ChromeDriver option “webSocketUrl” to be true

In order to utilize the BiDirectional features of Selenium 4, you need to set ChromeOption webSocketUrl to be true.
In Katalon Studio, Project > Settings > Desired Capabilities > WebUI > Chrome, do like this:

Test Case script

I wrote Test Cases/CapturingConsoleLog:

import java.nio.file.Path
import java.nio.file.Paths

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.module.LogInspector;

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path pageFile = projectDir.resolve('./page.html')
String pageUrl = pageFile.toFile().toURI().toURL().toExternalForm()

WebUI.openBrowser('')
WebDriver driver = DriverFactory.getWebDriver()

StringBuilder sb = new StringBuilder()

LogInspector logInspector = new LogInspector(driver)
logInspector.onConsoleEntry({ consoleLogEntry ->
	sb.append(consoleLogEntry.getType() + " " + "[" + consoleLogEntry.getLevel() + "] " + consoleLogEntry.getText())
	sb.append("\n")
})
WebUI.navigateToUrl(pageUrl)
WebUI.closeBrowser()

print sb.toString()

Please find how I utilized the LogInspector class here.

Result

When I ran the Test Case, I got the following output in the Console tab of Katalon Studio.

2025-12-22 21:01:50.507 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/CapturingConsoleLog
2025-12-22 21:01:51.720 DEBUG testcase.CapturingConsoleLog             - 1: projectDir = Paths.get(getProjectDir())
2025-12-22 21:01:51.770 DEBUG testcase.CapturingConsoleLog             - 2: pageFile = projectDir.resolve("./page.html")
2025-12-22 21:01:51.852 DEBUG testcase.CapturingConsoleLog             - 3: pageUrl = toURL().toExternalForm()
2025-12-22 21:01:51.926 DEBUG testcase.CapturingConsoleLog             - 4: openBrowser("")
2025-12-22 21:01:52.321 INFO  c.k.k.core.webui.driver.DriverFactory    - Starting 'Chrome' driver
2025-12-22 21:01:52.329 INFO  c.k.k.c.w.util.WebDriverPropertyUtil     - User set preference: ['prefs', '{webSocketUrl=true}']
2025-12-22 21:01:52.392 INFO  c.k.k.core.webui.driver.DriverFactory    - Action delay is set to 0 milliseconds
...
2025-12-22 21:02:04.070 INFO  c.k.k.core.webui.driver.DriverFactory    - sessionId = a930d33b24393f0aabc8f96a14471c38
2025-12-22 21:02:04.072 INFO  c.k.k.core.webui.driver.DriverFactory    - browser = Chrome 143.0.7499.170
2025-12-22 21:02:04.074 INFO  c.k.k.core.webui.driver.DriverFactory    - platform = Mac OS X
2025-12-22 21:02:04.076 INFO  c.k.k.core.webui.driver.DriverFactory    - seleniumVersion = 4.34.0
2025-12-22 21:02:04.110 INFO  c.k.k.core.webui.driver.DriverFactory    - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password=********, proxyServerAddress=, proxyServerPort=0, executionList="", isApplyToDesiredCapabilities=true }
2025-12-22 21:02:04.250 DEBUG testcase.CapturingConsoleLog             - 5: driver = getWebDriver()
2025-12-22 21:02:04.271 DEBUG testcase.CapturingConsoleLog             - 6: sb = new java.lang.StringBuilder()
2025-12-22 21:02:04.328 DEBUG testcase.CapturingConsoleLog             - 7: logInspector = new org.openqa.selenium.bidi.module.LogInspector(driver)
2025-12-22 21:02:04.383 DEBUG testcase.CapturingConsoleLog             - 8: logInspector.onConsoleEntry({ java.lang.Object consoleLogEntry -> ... })
2025-12-22 21:02:04.489 DEBUG testcase.CapturingConsoleLog             - 9: navigateToUrl(pageUrl)
2025-12-22 21:02:05.058 DEBUG testcase.CapturingConsoleLog             - 10: closeBrowser()
2025-12-22 21:02:05.244 DEBUG testcase.CapturingConsoleLog             - 11: print(sb.toString())
console [info] content script: Katalon Waiter v.2 is up and running !
console [info] now: Mon Dec 22 2025 21:02:05 GMT+0900 (日本標準時)
2025-12-22 21:02:05.260 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/CapturingConsoleLog

As you see, I could capture the messsage “now: Mon Dec 22 2025 21:02:05 GMT+0900 (日本標準時)”.

Conclusion

Using the LogInspector, your Katalon test case script can easily capture the messages in the JavaScript console.

2 Likes

hi @kazurayam

thanks for sharing this
good example of using Selenium 4 BiDi LogInspector in Katalon to capture JavaScript console logs
the setup with webSocketUrl in ChromeOptions is very clear and helpful

just curious have you tried this with other browsers like Firefox, or only Chrome so far?
also, did you consider filtering specific log levels to reduce noise?

really appreciate you sharing this solution

Only Chome. But I believe you can do the same with Firefox.

No, I didn’t. But it would be easy to implement any custom processings in the Consumer passed as the 1st argument of

logInspector.onConsoleEntry({logentry ->
    if (logentry.getType() == 'console') {
        ...
    } else {
        ...
    }
})

I just wanted to see if the Selenium4 BiDi features are usable in Katalon Studio v10.x. This study concluded positive. I’ve got contented. I am not going to do any further study.

3 Likes