Access browser console messages from a Capybara test
A very handy API I’ve stumbled across today:
page.driver.browser.manage.logs.get(:browser)
This returns an array of Selenium::LogEntry instances which have the expected properties - level
, timestamp
, and message
. You can use standard Enumerable methods to filter the log entries down to the information relevant to you.
For example, all error messages:
page.driver.browser.manage.logs.get(:browser).select { |le| le.level == "SEVERE" }.map(&:message)
=> ["http://127.0.0.1:40539/madeup - Failed to load resource: net::ERR_CONNECTION_REFUSED", ...]