Back to Challenges
Catch the JavaScript Error the Test Missed
advanced Python 15 minA checkout test passes while the page throws an uncaught TypeError. Use WebDriver BiDi to make the test fail on JavaScript errors.
The Scenario
A recent release broke the analytics call that fires after a coupon is applied. The total still updates, so the test passes, but every real user’s browser logs an uncaught TypeError and the analytics team has lost a week of data.
Your Task
Enable WebDriver BiDi and make the test (and, through a fixture, every test) fail when the page throws an uncaught JavaScript error, with the error text in the failure message.
What Good Looks Like
- BiDi enabled via options, not by hand-rolling a WebSocket.
- A reusable fixture that collects errors and asserts after the test body.
- The assertion message lists the errors so triage does not need a rerun.
Your task
A checkout test passes while the page throws an uncaught TypeError. Use WebDriver BiDi to make the test fail on JavaScript errors.
Problem Code
@pytest.fixture
def driver():
d = webdriver.Chrome()
yield d
d.quit()
def test_apply_coupon(driver):
driver.get("https://app.example.com/checkout")
driver.find_element(By.NAME, "promoCode").send_keys("SAVE10")
driver.find_element(By.ID, "apply").click()
WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element((By.CSS_SELECTOR, "[data-testid='order-total']"), "$89.00"))
# Passes. Meanwhile the console shows:
# Uncaught TypeError: Cannot read properties of undefined (reading 'analytics')
Need Help?
Solution
Try to solve the challenge yourself before revealing the solution.
Lessons that cover this
- WebDriver BiDi Enable the bidirectional protocol and use it for console log capture, JavaScript error detection, basic authentication, DOM mutation events, preload scripts and emulation, with browser support and migration guidance as of Selenium 4.48.
- Browser and Driver Logs Turn on the logs that explain failures: browser console output via BiDi, chromedriver and geckodriver logs, Selenium's own client logging, network failures, and how to collect them per test.