Test Automation - Capturing Console Logs and JavaScript Errors with Selenium WebDriver BiDi in Python
Introduction
WebDriver BiDi (Bidirectional) protocol represents a significant evolution of WebDriver Classic, seamlessly integrating powerful capabilities from the Chrome DevTools Protocol (CDP). This architectural enhancement establishes true bidirectional communication between the test client and browser, unlocking advanced capabilities for logging, network interception, and performance monitoring directly from test scripts.
While Java enjoys a more comprehensive BiDi implementation with robust support across features, Python's implementation is still in active development, with certain advanced capabilities like network interception and DOM mutation handling not yet matching the maturity found in Java.
The solution presented in this article is exemplified in my Selenium Python example project, which was developed in collaboration with Elias Shourosh.
Benefits of Using BiDi for Logging
Console messages in a browser encompass all outputs generated through console.log, console.error, console.warn, console.info, and similar methods. These messages act as intentional breadcrumbs left by developers to trace the application's execution flow. When a web test fails, these console messages provide crucial context about:
JavaScript errors capture unhandled exceptions, syntax errors, and runtime issues during page execution. Unlike console messages, these errors are often unintentional and represent actual failures in the application code. Common JavaScript errors include:
When a web test fails, having comprehensive logs of both console messages and JavaScript errors provides invaluable diagnostic information. Without these logs, QA engineers often face the frustrating "works on my machine" scenario, making it difficult to resolve intermittent failures that can’t be reproduced locally.
BiDi logging creates a detailed audit trail of the application's behavior during test execution, significantly reducing debugging time and allowing teams to pinpoint the exact conditions that led to test failures.
Implementing the Solution
The solution code can be found here.
Using BiDi, we can intercept these logs in real time:
Event handlers in this context are functions that automatically execute when specific browser events occur, such as console outputs or JavaScript exceptions. They enable automated log collection without modifying the tested application’s code.
If a test fails and there are console logs or JavaScript errors captured during execution, these logs will be attached to Allure reports, helping in quicker diagnostics. If no logs are present, no attachments will be made.
Conclusion
By leveraging BiDi's event-driven architecture, testers can capture critical diagnostic information without relying on external tools. This capability transforms the debugging experience, converting mysterious test failures into traceable, reproducible issues with clear error paths and contextual information.
As the WebDriver BiDi protocol continues to mature, we can expect even more powerful capabilities to emerge, further enhancing the testing experience. For teams working with Python automation frameworks, adopting these BiDi logging techniques represents an immediate opportunity to improve test reliability and significantly reduce time spent troubleshooting failed tests.
The solution presented is more than just a technical implementation—it's a step toward more observable, maintainable test automation that aligns with modern development practices. By embracing these advanced logging capabilities, teams can build more resilient test suites that provide genuine confidence in application quality.
Happy testing!