简体   繁体   中英

Enable/view console.log messages in headless Chrome

I have a Rails 5.2.3 app running headless Chrome test using Capybara, Selenium, and chromedriver. My tests run just fine, but I can't seem to enable or view any messages output to console.log . Here is my setup:

Gemfile

ruby '2.6.5'

gem 'rails', '~> 5.2.3'

group :test do
  gem 'rspec-rails'
  gem 'capybara'

  gem 'selenium-webdriver'
  gem 'webdrivers'
end

spec_helper.rb

Capybara.register_driver :headless_chrome do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome loggingPrefs: {
    browser: 'ALL', client: 'ALL', driver: 'ALL', server: 'ALL'
  }

  opts = Selenium::WebDriver::Chrome::Options.new
  chrome_args = %w(--headless --window-size=1920,1080 --no-sandbox --disable-dev-shm-usage --enable-logging)
  chrome_args.each {|a| opts.add_argument a}

  Capybara::Selenium::Driver.new app, browser: :chrome, options: opts, desired_capabilities: caps
end

Then inside any of my tests, if I try to inspect the logs (and I know I am triggering console.log messages in my code that is run), I don't see anything:

page.driver.browser.manage.logs.get(:browser) # => []

So, what am I doing incorrectly? I assume I'm missing some configuration/logging setting when setting up the :headless_chrome driver, but what is it? Any help would be greatly appreciated.

When Chrome changed to w3c mode by default (v75) it changed loggingPrefs to goog:loggingPrefs to be spec compliant. Try setting goog:loggingPrefs instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM