Restore access to ChromeDriver 75 console logs

Problem

I recently wrote the following code to pull the console logs from Chrome:

caps = Selenium::WebDriver::Remote::Capabilities.chrome('loggingPrefs' => {browser: 'ALL'})
browser = Watir::Browser.new :chrome, desired_capabilities: caps

browser.execute_script('console.log("test");')

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

However, it started throwing an exception a couple of days ago:

#=> /selenium-webdriver-3.142.3/lib/selenium/webdriver/common/logs.rb:32:in `get': 
#=>   undefined method `log' for # (NoMethodError)

What is going on?

Answer

The problem stems from an upgrade to ChromeDriver 75.0.3770.8, where the release notes state “the most noticeable change is ChromeDriver now runs in W3C standard compliant mode by default.” This results in a different Selenium bridge being used:

  • ChromeDriver 74.0.3729.6 uses the Selenium::WebDriver::Remote::OSS::Bridge
  • ChromeDriver 75.0.3770.90 uses the Selenium::WebDriver::Remote::W3C::Bridge

From Selenium Issue 5127, this logging is not defined in W3C. No surprise then that the W3C::Bridge does not have have the logging methods that the OSS::Bridge had.

The ChromeDriver release notes also say, “renamed capability loggingPrefs to goog:loggingPrefs, as required by W3C standard.” This gives hope that the logging still exists; we just need to make it available. What if we just copy the OSS::Bridge functionality – particularly the #log method that is undefined and the associated command? I came up with the following patch:

module Selenium
  module WebDriver
    module Chrome
      module Bridge
        COMMANDS = remove_const(:COMMANDS).dup
        COMMANDS[:get_log] = [:post, 'session/:session_id/log']
        COMMANDS.freeze
        
        def log(type)
          data = execute :get_log, {}, {type: type.to_s}

          Array(data).map do |l|
            begin
              LogEntry.new l.fetch('level', 'UNKNOWN'), l.fetch('timestamp'), l.fetch('message')
            rescue KeyError
              next
            end
          end
        end
      end
    end
  end
end

To make this work, the capability also needs to be renamed from “loggingPrefs” to “goog:loggingPrefs”:

caps = Selenium::WebDriver::Remote::Capabilities.chrome('goog:loggingPrefs' => {browser: 'ALL'})
browser = Watir::Browser.new :chrome, desired_capabilities: caps

browser.execute_script('console.log("test");')

puts browser.driver.manage.logs.get(:browser)
#=> INFO 2019-06-13 21:48:03 -0400: console-ap

Not ideal to monkey patch, but good enough as a short-term fix. I’ll be keeping an eye on Issue 7270 to see if a better solution becomes available.

This entry was posted in Selenium-Webdriver, Watir. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s