Search code examples
rubytestingrspeccapybaraheadless

Debug in headless Chrome


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?


Solution

  • 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