Skip to content

Instantly share code, notes, and snippets.

@snehaso
Forked from zdennis/browser_logging.rb
Created July 8, 2020 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snehaso/8176f87d81db90f30472f804767e274b to your computer and use it in GitHub Desktop.
Save snehaso/8176f87d81db90f30472f804767e274b 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