Chrome DevTools Logging Features

Logging features using CDP.

While Selenium 4 provides direct access to the Chrome DevTools Protocol, these methods will eventually be removed when WebDriver BiDi implemented.

Console Logs

30
32
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">dev.selenium.bidi.cdp</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline">

import static org.openqa.selenium.devtools.events.CdpEventTypes.consoleEvent; import dev.selenium.BaseTest; import java.time.Duration; import java.util.concurrent.CopyOnWriteArrayList; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.logging.HasLogEvents; import org.openqa.selenium.support.ui.WebDriverWait; public class LoggingTest extends BaseTest { @BeforeEach public void createSession() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void consoleLogs() { driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); CopyOnWriteArrayList<String> messages = new CopyOnWriteArrayList<>(); ((HasLogEvents) driver).onLogEvent(consoleEvent(e -> messages.add(e.getMessages().get(0)))); driver.findElement(By.id("consoleLog")).click(); driver.findElement(By.id("consoleError")).click(); wait.until(_d -> messages.size() > 1); Assertions.assertTrue(messages.contains("Hello, world!")); Assertions.assertTrue(messages.contains("I am console error")); } }

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/java/src/test/java/dev/selenium/bidi/cdp/LoggingTest.java#L31" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>
10
13
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">pytest</span>

from selenium.webdriver.common.bidi.console import Console from selenium.webdriver.common.by import By from selenium.webdriver.common.log import Log @pytest.mark.trio async def test_console_log(driver): driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html') async with driver.bidi_connection() as session: async with Log(driver, session).add_listener(Console.ALL) as messages: driver.find_element(by=By.ID, value='consoleLog').click() assert messages["message"] == "Hello, world!" @pytest.mark.trio async def test_js_error(driver): driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html') async with driver.bidi_connection() as session: async with Log(driver, session).add_js_error_listener() as messages: driver.find_element(by=By.ID, value='jsException').click() assert "Error: Not working" in messages.exception_details.exception.description

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/python/tests/bidi/cdp/test_logs.py#L11-12" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>