Capture Browser Console Logs with Capybara

Is there a way to print javascript console.errors to the terminal with Rspec/Capybara/Selenium?

I don't know if this will be of any help, but you could try switching over to thoughtbot's capybara-webkit driver. It's an alternative to Selenium that's headless, meaning it doesn't open a browser to run the tests. When I run my tests using this driver (in an RSpec+Capybara setup), all Javascript errors get printed inline with my RSpec output.

I've never tried switching from Selenium to capybara-webkit, so I don't know how feasible this is on an existing project. If you're not doing anything really fancy with Selenium, the transition might be pretty smooth. However, if you depend on being able to watch the tests running in the browser, or have some other specific need for Selenium, then my answer unfortunately won't be of much use.

You can find capybara-webkit here: https://github.com/thoughtbot/capybara-webkit

Getting it installed might be a pain, since you'll need the Qt4 library. If you don't already have Qt4 on your system, the build process can take a long time. For me, it was well worth the trouble. I much prefer capybara-webkit to any other solution I've tried.

How to Print Browser console logs using Cucumber Ruby Automation?

Since you're using selenium you can try

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

Note: I've never tried it with PhantomJS as the browser

Enable/view console.log messages in headless Chrome

When Chrome changed to w3c mode by default (v75) it changed loggingPrefs to goog:loggingPrefs to be spec compliant. Try setting goog:loggingPrefs instead.

Get chromes console log via Ruby WebDriver

Apologies for late response.

I originally achieved it by adding the following to Webdriver;

module Selenium
module WebDriver
class Options

#
# Returns the available logs for this webDriver instance
#
def available_log_types
@bridge.getAvailableLogTypes
end

#
# Returns the requested log
#
# @param type [String] The required log type
#
# @return [Array] An array of log entries
#
def get_log(type)
@bridge.getLog(type)
end

end
end
end

When "required" this resulted in the following being supported;

driver.manage.get_log(:browser)

However, Version 2.38 of the selenium ruby gem exposes the logging API (although experimental).

http://selenium.googlecode.com/git/rb/CHANGES

https://code.google.com/p/selenium/wiki/Logging

Therefore, from 2.38 onwards the following should work WITHOUT the above extension;

driver.manage.logs.get :browser

How to enable ChromeDriver logging in Ruby Capybara with Selenium?

You have already added logging prefs in capabilities.

Also, you can create a method to store the logs where ever you need. Here is the method I use to write logs on a specific folder you need. You can call this method in after hooks. Also, you can store as artifacts if you want to see in any CI/CD application

 def capture_browser_logs
errors = Capybara.page.driver.browser.manage.logs.get(:browser)
.select { |e| e.level == "SEVERE" && e.message.present? }
.map(&:message)
.to_a
return if errors.none?
message = errors.join("\n\n")

# writes console errors to a log file
log_file_path = Rails.root.join("tmp", "smoke_tests", "console_logs", "js_errors.log")
FileUtils.mkdir_p(log_file_path.dirname) unless File.directory?(log_file_path.dirname)

logging_destination = if ENV["RAILS_LOG_TO_STDOUT"].present? && ENV["RAILS_LOG_TO_STDOUT"].to_s == "true"
STDOUT
else
log_file_path
end

logger = Logger.new(logging_destination)
logger.error(message)
end


Related Topics



Leave a reply



Submit