1

After switching from capybara_webkit to headless_chrome, I'm trying to find a way to replicate

Capybara::Webkit.configure do |config|
  config.debug = true
end

or

Capybara.javascript_driver = :webkit_debug

with the new driver. The goal is to be able to see the log of everything happening when running rspec my_spec.rb: for example, all GET requests.

Is there a way to achieve that?

fabdurso
  • 2,366
  • 5
  • 29
  • 55
  • 1
    The server in 'test' env writes logs into `test.log` by default. For printing logs to console read that: https://stackoverflow.com/questions/11770552/how-to-get-rails-logger-printing-to-the-console-stdout-when-running-rspec – Leo Jul 26 '18 at 11:44
  • Hey @Leo I'm not using Rails, so I suppose that this solution won't work, will it? – fabdurso Jul 26 '18 at 12:32
  • Yes, won't work. Also, in your app, you write some logs? Why doesn't that work for rspec? – Leo Jul 26 '18 at 12:38
  • The specs are independent from the app. – fabdurso Jul 26 '18 at 13:56

1 Answers1

2

There is no option when using Selenium like the capybara_webkit debug option that outputs the debug info in realtime, however you can get access to the Chrome logs and output those at the end of each test (or you could write a helper to output them whenever you call it of course).

First you'd need to configure your selenium driver for logging

Capybara.register_driver :logging_chrome do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(
    # customize this based on which and what level logs your prefer
    loggingPrefs: { 
      browser: 'ALL',
      driver: 'ALL',
      performance: 'ALL'
    }
  )
  browser_options = ::Selenium::WebDriver::Chrome::Options.new()
  browser_options.headless!
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: caps,
    options: browser_options
  )
end

Then you'd set up to use that driver

Capybara.javascript_driver = :logging_chrome # possibly default_driver = depending on your config

and then add an after block that gets the logs and displays them

after(:each) do
  # customize based on which type of logs you want displayed
  log_types = page.driver.browser.manage.logs.available_types
  log_types.each do |t|
     puts t.to_s + ": " + page.driver.browser.manage.logs.get(t).join("\n")
  end
end
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78