Skip to content Skip to sidebar Skip to footer

Capture Browser Console Logs With Capybara

I need to capture the console logs (category: info) of a browser using Ruby & Capybara. Until now I have tried using driver.manage.logs.get(:browser) or (:client) but, using th

Solution 1:

Whether or not logs are available when using selenium depends on what browser you are using with Selenium. If you were using Firefox you'd be out of luck since it doesn't support the log retrieval API, however since you're using Chrome they are accessible. The issue you're having is that, by default, only WARN or ERROR level logs are captured. You can change this in the driver registration through the loggingPrefs capability

Capybara.register_driver :logging_selenium_chromedo|app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:{browser:'ALL'})
  browser_options = ::Selenium::WebDriver::Chrome::Options.new()
  # browser_options.args << '--some_option' # add whatever browser args and other options you need (--headless, etc)
  Capybara::Selenium::Driver.new(app, browser::chrome, options: browser_options, desired_capabilities: caps)
end

and then specify to use :logging_selenium_chrome as your driver

Capybara.javascript_driver = :logging_selenium_chrome # or however else you're specifying which driver to use

which should then allow you to get the logs in your tests with

page.driver.browser.manage.logs.get(:browser)

Solution 2:

Thomas Walpole answer is correct but it seems that nowadays if you are using chrome as your driver you should use

Selenium::WebDriver::Remote::Capabilities.chrome( "goog:loggingPrefs": { browser: 'ALL' } )

Notice goog:loggingPrefs instead of loggingPrefs only with this solution i was able to get console.log printed in the log.

Took me a while and got it from here https://intellipaat.com/community/5478/getting-console-log-output-from-chrome-with-selenium-python-api-bindings after several frustrating attempts.

Solution 3:

Not sure that this is what you want, but take a look at https://github.com/dbalatero/capybara-chromedriver-logger.

It helps me identify the problem with dynamic modules import(''). Works both locally and in Github Actions / Circle CI by displaying failed loads of assets (which i believe outputs as console.error).

Post a Comment for "Capture Browser Console Logs With Capybara"