Skip to content

Instantly share code, notes, and snippets.

@zdennis
Created June 26, 2018 19:11
Show Gist options
  • Save zdennis/7ff21bbf663ee6a09984c64ac703bc3b to your computer and use it in GitHub Desktop.
Save zdennis/7ff21bbf663ee6a09984c64ac703bc3b to your computer and use it in GitHub Desktop.
Chromedriver browser logging for Capybara
# browser_logging.rb will print out the browser log from Chrome
# when running specs with "js: true". This is so we can easily debug
# issues where there are JS errors or other console warnings/error(s), etc.
#
# Output file is: Rails.root/log/browser.log
#
# Note: Nothing will be printed out for non-JS examples.
RSpec.configure do |config|
browser_log = File.new(Rails.root.join("log/browser.log").to_s, "w")
browser_log.sync = true
config.before(:each) do |example|
if self.class.metadata[:js]
description = "Example: #{example.full_description}"
browser_log.puts description, "-"*description.length
end
end
config.after(:each) do |example|
if self.class.metadata[:js]
browser_log.puts page.driver.browser.manage.logs.get(:browser)
browser_log.puts
end
end
config.after(:suite) do
browser_log.close
end
end
# Give us access to browser console logs, see spec/support/browser_logging.rb
logging_preferences = { browser: 'ALL' }
# This env var comes from chromedriver_linux64, used on the TravisCI
chrome_bin = ENV.fetch('CHROME_BIN', nil)
chrome_options = {}
chrome_options[:binary] = chrome_bin if chrome_bin
# Give us access to browser console logs, see spec/support/browser_logging.rb
logging_preferences = { browser: 'ALL' }
Capybara.register_driver :chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: chrome_options,
loggingPrefs: logging_preferences
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: capabilities
)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: chrome_options.merge(args: %w(headless disable-gpu)),
loggingPrefs: logging_preferences
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: capabilities
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment