From cd9052876f6a411527829fc77312292223599eca Mon Sep 17 00:00:00 2001 From: "nicholas a. evans" Date: Mon, 24 Jan 2022 14:59:01 -0500 Subject: [PATCH] compatibility with selenium-webdriver 4.x --- .ruby-version | 2 +- .travis.yml | 9 +- Gemfile.lock | 94 ++++++++++--------- capybara-chromedriver-logger.gemspec | 6 +- lib/capybara/chromedriver/logger.rb | 39 +++++++- lib/capybara/chromedriver/logger/collector.rb | 24 +++-- spec/support/capybara.rb | 16 ++-- 7 files changed, 123 insertions(+), 67 deletions(-) diff --git a/.ruby-version b/.ruby-version index 57cf282..fd2a018 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.6.5 +3.1.0 diff --git a/.travis.yml b/.travis.yml index 77acf60..85e561d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,5 +3,10 @@ language: ruby addons: chrome: stable rvm: - - 2.6.5 -before_install: gem install bundler -v 1.16.0 + - 2.6.9 + - 2.7.5 + - 3.0.3 + - 3.1.0 +before_install: + gem update --system 3.3.5 + gem install bundler -v 2.3.5 diff --git a/Gemfile.lock b/Gemfile.lock index cfcf84f..177b3f6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,60 +8,66 @@ PATH GEM remote: https://rubygems.org/ specs: - addressable (2.7.0) + addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) - capybara (3.31.0) + capybara (3.36.0) addressable + matrix mini_mime (>= 0.1.3) nokogiri (~> 1.8) rack (>= 1.6.0) rack-test (>= 0.6.3) - regexp_parser (~> 1.5) + regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) - childprocess (0.8.0) - ffi (~> 1.0, >= 1.0.11) - coderay (1.1.2) + childprocess (4.1.0) + coderay (1.1.3) colorize (0.8.1) - diff-lcs (1.3) - ffi (1.12.2) - gem-release (2.0.1) - method_source (0.9.0) - mini_mime (1.0.2) - mini_portile2 (2.4.0) - nokogiri (1.10.8) - mini_portile2 (~> 2.4.0) - pry (0.11.3) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - public_suffix (4.0.3) - rack (2.0.8) + diff-lcs (1.5.0) + gem-release (2.2.2) + matrix (0.4.2) + method_source (1.0.0) + mini_mime (1.1.2) + mini_portile2 (2.7.1) + nokogiri (1.13.1) + mini_portile2 (~> 2.7.0) + racc (~> 1.4) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (4.0.6) + racc (1.6.0) + rack (2.2.3) rack-test (1.1.0) rack (>= 1.0, < 3) - rake (13.0.1) - regexp_parser (1.7.0) - rspec (3.7.0) - rspec-core (~> 3.7.0) - rspec-expectations (~> 3.7.0) - rspec-mocks (~> 3.7.0) - rspec-core (3.7.1) - rspec-support (~> 3.7.0) - rspec-expectations (3.7.0) + rake (13.0.6) + regexp_parser (2.2.0) + rexml (3.2.5) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-mocks (3.7.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-support (3.7.1) - rubyzip (1.2.1) - selenium-webdriver (3.9.0) - childprocess (~> 0.5) - rubyzip (~> 1.2) - stub_server (0.2.1) + rspec-support (~> 3.10.0) + rspec-support (3.10.3) + rubyzip (2.3.2) + selenium-webdriver (4.1.0) + childprocess (>= 0.5, < 5.0) + rexml (~> 3.2, >= 3.2.5) + rubyzip (>= 1.2.2) + stub_server (0.6.0) rack - webdrivers (4.1.2) + webrick + webdrivers (5.0.0) nokogiri (~> 1.6) - rubyzip (~> 1.0) - selenium-webdriver (>= 3.0, < 4.0) + rubyzip (>= 1.3.0) + selenium-webdriver (~> 4.0) + webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) @@ -69,15 +75,15 @@ PLATFORMS ruby DEPENDENCIES - bundler (~> 1.16) + bundler (~> 2.3) capybara-chromedriver-logger! gem-release pry rake (~> 13.0) rspec (~> 3.0) - selenium-webdriver + selenium-webdriver (~> 4.1) stub_server - webdrivers (~> 4.0) + webdrivers (~> 5.0) BUNDLED WITH - 1.17.3 + 2.3.5 diff --git a/capybara-chromedriver-logger.gemspec b/capybara-chromedriver-logger.gemspec index 48b3949..e55cedb 100644 --- a/capybara-chromedriver-logger.gemspec +++ b/capybara-chromedriver-logger.gemspec @@ -32,12 +32,12 @@ Gem::Specification.new do |spec| spec.add_dependency "capybara" spec.add_dependency "colorize" - spec.add_development_dependency "bundler", "~> 1.16" + spec.add_development_dependency "bundler", "~> 2.3" spec.add_development_dependency "gem-release" spec.add_development_dependency "pry" spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "rspec", "~> 3.0" - spec.add_development_dependency "selenium-webdriver" + spec.add_development_dependency "selenium-webdriver", "~> 4.1" spec.add_development_dependency "stub_server" - spec.add_development_dependency 'webdrivers', '~> 4.0' + spec.add_development_dependency 'webdrivers', '~> 5.0' end diff --git a/lib/capybara/chromedriver/logger.rb b/lib/capybara/chromedriver/logger.rb index 7f9ff22..4f649bf 100644 --- a/lib/capybara/chromedriver/logger.rb +++ b/lib/capybara/chromedriver/logger.rb @@ -12,7 +12,35 @@ module Chromedriver module Logger extend self - def build_capabilities(loggingPrefs: { browser: 'ALL' }, **options) + # == compatibility with selenium-webdriver 4.x + # + # Selenium::WebDriver's API changed significantly between 3.x and 4.x. + # + # This method delegates to build_capabilities_3 or build_capabilities_4. + def build_capabilities(logging_prefs: { browser: 'ALL' }, **options) + # backwards compatibility with camelcase + logging_prefs = options.delete(:loggingPrefs) || logging_prefs + + if using_selenium_webdriver_4_or_higher? + # is there a better way to detect Selenium::WebDriver compatibility? + build_capabilities_4(logging_prefs: logging_prefs, **options) + else + build_capabilities_3(loggingPrefs: logging_prefs, **options) + end + end + + # For selenium-webdriver >= 4.0. Capabilities will be created using + # Selenium::WebDriver::Options.chrome and should be passed to + # Capybara::Selenium::Driver.new(..., capabilities: capabilities) + def build_capabilities_4(logging_prefs: { browser: 'ALL' }, **options) + ::Selenium::WebDriver::Options.chrome(**options, + logging_prefs: logging_prefs) + end + + # For selenium-webdriver < 4.0. Capabilities will be created using + # Selenium::WebDriver::Remote::Capabilities.chrome and should be passed to + # Capybara::Selenium::Driver.new(..., desired_capabilities: capabilities) + def build_capabilities_3(loggingPrefs: { browser: 'ALL' }, **options) options[:chromeOptions] ||= {} if options[:chromeOptions][:w3c] @@ -60,6 +88,15 @@ def raise_js_errors? def raise_js_errors=(value) @raise_js_errors = value end + + # always returns true if Gem::Version isn't defined + def using_selenium_webdriver_4_or_higher? + return true unless defined?(Gem::Version) + return false unless defined?(::Selenium::WebDriver::VERSION) + Gem::Version.new(::Selenium::WebDriver::VERSION) >= + Gem::Version.new("4.0.0.0.a") + end + end end end diff --git a/lib/capybara/chromedriver/logger/collector.rb b/lib/capybara/chromedriver/logger/collector.rb index bdfe8a5..99b8d8f 100644 --- a/lib/capybara/chromedriver/logger/collector.rb +++ b/lib/capybara/chromedriver/logger/collector.rb @@ -32,11 +32,11 @@ def raise_errors_if_needed! def flush_logs! browser_logs.each do |log| message = Message.new(log) - + next if should_filter?(message) errors << message if message.error? - + log_destination.puts message.to_s end end @@ -50,12 +50,20 @@ def browser_logs end def logs(type) - Capybara - .current_session - .driver.browser - .manage - .logs - .get(type) + if Logger.using_selenium_webdriver_4_or_higher? + Capybara + .current_session + .driver.browser + .logs + .get(type) + else + Capybara + .current_session + .driver.browser + .manage + .logs + .get(type) + end end def should_filter?(message) diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index 5d8faab..4427b15 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -2,9 +2,6 @@ require 'capybara/rspec' Capybara.register_driver :selenium do |app| - client = Selenium::WebDriver::Remote::Http::Default.new - client.read_timeout = 240 - args = %w[ disable-default-apps disable-extensions @@ -21,16 +18,19 @@ ] capabilities = Capybara::Chromedriver::Logger.build_capabilities( - chromeOptions: { - args: args - } + args: args ) + if ENV["VERBOSE_SELENIUM_WEBDRIVER"] == "1" + capabilities.add_argument "--verbose" + Selenium::WebDriver.logger.level = :debug + end + Capybara::Selenium::Driver.new( app, browser: :chrome, - desired_capabilities: capabilities, - http_client: client + capabilities: capabilities, + timeout: 240 ) end