Željko Filipin's blog.
Home Blog Tags Now License

View on GitHub
21 August 2012

Catch JavaScript Errors on the Page with Watir or Selenium

by Željko Filipin

Javascript Error

Seven months ago (tempus fugit) a client asked if I could create a smoke test that would visit all pages of the application and report if there were any JavaScript errors. Since we already had a smoke test that visited all pages and took screenshots, I thought it would be easy to do. All I had to do was to figure out how to catch JavaScript errors with Watir. Turns out Selenium does not have API for checking for JavaScript errors on the page yet. How are Selenium and Watir connected? Watir WebDriver (part of Watir project) uses WebDriver (part of Selenium project) to drive browsers.

I have found a solution (with some help at Google Groups and Stack Overflow), but it was neither simple nor elegant. Then Jari Bakken pointed me to his Gist.

require 'selenium-webdriver'
require 'pp'

profile =
profile.add_extension 'JSErrorCollector.xpi' #

driver = Selenium::WebDriver.for :firefox, :profile => profile

# do stuff

errors = driver.execute_script("return window.JSErrorCollector_errors.pump()")
pp errors
tags: code - ruby