Access browser logs using selenium-webdriver 4 using ruby

43 Views Asked by At

I'm updating the gem selenium-webdriver from version 3 to 4.

These is the chrome driver setup to run javascript tests:

Capybara.register_driver :chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new(
    args: [
      'headless',
      'disable-gpu',
      'no-sandbox',
      '--window-size=2560,1440',
      '--disable-web-security',
      '--allow-running-insecure-content',
      "--enable-logging", # Enables logging
      "--log-level=0", # Enables all logging
      "--v=1" # Sets the verbosity level of logging
    ]
  )

  # browser_logging_path = "/tmp/chromedriver.log"
  # options.add_argument("--log-path=#{browser_logging_path}")


  # In Selenium 4, `add_option` is replaced by `add_preference` for setting browser-specific preferences.
  # However, for capabilities like loggingPrefs, you should use `add_option` with correct nesting under 'goog:loggingPrefs'.
  options.add_option('goog:loggingPrefs', { browser: 'ALL' })

  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

The issue I'm having is to access the browser logs if a test fail:

  config.after do |example|
    if example.exception && example.metadata[:js]
       browser_logs = page.driver.browser.logs.get(:browser)
       puts browser_logs
    end
  end

I'm trying to access the logs using one of these two methods, but none of them work:

page.driver.browser.logs.get(:browser)

or

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

Both of these calls give the error NoMethodError Exception: undefined method 'logs' for #<Selenium::WebDriver::Manager

How to access the browser logs after if tests fail?

1

There are 1 best solutions below

0
Rodrigo On

Solution is to use the new headless option:

  options = Selenium::WebDriver::Chrome::Options.new(
    args: [
      'headless=new',
      'disable-gpu',
      'no-sandbox',
      '--window-size=2560,1440',
      '--disable-web-security',
      '--allow-running-insecure-content',
      "--enable-logging", # Enables logging
      "--log-level=0", # Enables all logging
      "--v=1" # Sets the verbosity level of logging
    ]
  )