1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Use Gem requirements to cleanup some workarounds

This commit is contained in:
Thomas Walpole 2021-01-20 11:37:49 -08:00
parent a760f1775e
commit 0ad261ad0a
7 changed files with 43 additions and 64 deletions

View file

@ -10,23 +10,20 @@ end
Capybara.register_driver :selenium_headless do |app|
version = Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Firefox::Options.new
if browser_options.respond_to?(:headless!)
browser_options.headless!
else
browser_options.args << '-headless'
end
if version >= Gem::Version.new('4.0.0.alpha6')
Capybara::Selenium::Driver.new(app, browser: :firefox, capabilities: browser_options)
else
Capybara::Selenium::Driver.new(app, browser: :firefox, options: browser_options)
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
browser_options = ::Selenium::WebDriver::Firefox::Options.new.tap do |opts|
if browser_options.respond_to?(:headless!)
opts.headless!
else
opts.args << '-headless'
end
end
Capybara::Selenium::Driver.new(app, **Hash[:browser => :firefox, options_key => browser_options])
end
Capybara.register_driver :selenium_chrome do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
# Workaround https://bugs.chromium.org/p/chromedriver/issues/detail?id=2650&q=load&sort=-id&colspec=ID%20Status%20Pri%20Owner%20Summary
if opts.respond_to?(:add_argument)
@ -36,15 +33,12 @@ Capybara.register_driver :selenium_chrome do |app|
end
end
if version >= Gem::Version.new('4.0.0.alpha6')
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: browser_options)
else
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end
Capybara::Selenium::Driver.new(app, **Hash[:browser => :firefox, options_key => browser_options])
end
Capybara.register_driver :selenium_chrome_headless do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
if opts.respond_to?(:headless!)
opts.headless!
@ -56,9 +50,5 @@ Capybara.register_driver :selenium_chrome_headless do |app|
opts.args << '--disable-site-isolation-trials'
end
if version >= Gem::Version.new('4.0.0.alpha6')
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: browser_options)
else
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end
Capybara::Selenium::Driver.new(app, **Hash[:browser => :firefox, options_key => browser_options])
end

View file

@ -32,7 +32,7 @@ Capybara.register_server :puma do |app, port, host, **options|
events = conf.options[:Silent] ? ::Puma::Events.strings : ::Puma::Events.stdio
puma_ver = Gem::Version.new(Puma::Const::PUMA_VERSION)
require_relative 'patches/puma_ssl' if (Gem::Version.new('4.0.0')...Gem::Version.new('4.1.0')).cover? puma_ver
require_relative 'patches/puma_ssl' if Gem::Requirement.new('>=4.0.0', '< 4.1.0').satisfied_by?(puma_ver)
events.log 'Capybara starting Puma...'
events.log "* Version #{Puma::Const::PUMA_VERSION} , codename: #{Puma::Const::CODE_NAME}"

View file

@ -12,6 +12,8 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
clear_session_storage: nil
}.freeze
SPECIAL_OPTIONS = %i[browser clear_local_storage clear_session_storage timeout native_displayed].freeze
CAPS_VERSION = Gem::Requirement.new('~> 4.0.0.alpha6')
attr_reader :app, :options
class << self
@ -41,7 +43,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
Gem::Version.new(Selenium::WebDriver::VERSION)
end
if @selenium_webdriver_version < Gem::Version.new('3.5.0')
unless Gem::Requirement.new('>= 3.5.0').satisfied_by? @selenium_webdriver_version
warn "Warning: You're using an unsupported version of selenium-webdriver, please upgrade."
end

View file

@ -118,15 +118,15 @@ private
end
def chromedriver_fixed_actions_key_state?
Gem::Version.new(chromedriver_version) >= Gem::Version.new('76.0.3809.68')
Gem::Requirement.new('>= 76.0.3809.68').satisfied_by?(chromedriver_version)
end
def chromedriver_supports_displayed_endpoint?
Gem::Version.new(chromedriver_version) >= Gem::Version.new('76.0.3809.25')
Gem::Requirement.new('>= 76.0.3809.25').satisfied_by?(chromedriver_version)
end
def chromedriver_version
capabilities['chrome']['chromedriverVersion'].split(' ')[0] # rubocop:disable Style/RedundantArgument
Gem::Version.new(capabilities['chrome']['chromedriverVersion'].split(' ')[0]) # rubocop:disable Style/RedundantArgument
end
def native_displayed?

View file

@ -20,12 +20,9 @@ browser_options.add_preference(:download, default_directory: Capybara.save_path)
Capybara.register_driver :selenium_chrome do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
driver_options = { browser: :chrome, timeout: 30 }.tap do |opts|
if version >= Gem::Version.new('4.0.0.alpha6')
opts[:capabilities] = browser_options
else
opts[:options] = browser_options
end
opts[options_key] = browser_options
end
Capybara::Selenium::Driver.new(app, **driver_options).tap do |driver|
@ -36,12 +33,9 @@ end
Capybara.register_driver :selenium_chrome_not_clear_storage do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
chrome_options = { browser: :chrome, clear_local_storage: false, clear_session_storage: false }.tap do |opts|
if version >= Gem::Version.new('4.0.0.alpha6')
opts[:capabilities] = browser_options
else
opts[:options] = browser_options
end
opts[options_key] = browser_options
end
Capybara::Selenium::Driver.new(app, **chrome_options)
@ -49,12 +43,9 @@ end
Capybara.register_driver :selenium_chrome_not_clear_session_storage do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
chrome_options = { browser: :chrome, clear_session_storage: false }.tap do |opts|
if version >= Gem::Version.new('4.0.0.alpha6')
opts[:capabilities] = browser_options
else
opts[:options] = browser_options
end
opts[options_key] = browser_options
end
Capybara::Selenium::Driver.new(app, **chrome_options)
@ -62,25 +53,19 @@ end
Capybara.register_driver :selenium_chrome_not_clear_local_storage do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
chrome_options = { browser: :chrome, clear_local_storage: false }.tap do |opts|
if version >= Gem::Version.new('4.0.0.alpha6')
opts[:capabilities] = browser_options
else
opts[:options] = browser_options
end
opts[options_key] = browser_options
end
Capybara::Selenium::Driver.new(app, **chrome_options)
end
Capybara.register_driver :selenium_driver_subclass_with_chrome do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
subclass = Class.new(Capybara::Selenium::Driver)
chrome_options = { browser: :chrome, timeout: 30 }.tap do |opts|
if version >= Gem::Version.new('4.0.0.alpha6')
opts[:capabilities] = browser_options
else
opts[:options] = browser_options
end
opts[options_key] = browser_options
end
subclass.new(app, **chrome_options)
@ -203,7 +188,7 @@ RSpec.describe 'Capybara::Session with chrome' do
before { skip 'Only makes sense in W3C mode' if ENV['W3C'] == 'false' }
it 'does not error getting log types' do
skip if Gem::Version.new(session.driver.browser.capabilities['chrome']['chromedriverVersion'].split[0]) < Gem::Version.new('75.0.3770.90')
skip if Gem::Requirement.new('< 75.0.3770.90').satisfied_by? chromedriver_version
expect do
session.driver.browser.manage.logs.available_types
end.not_to raise_error
@ -215,4 +200,8 @@ RSpec.describe 'Capybara::Session with chrome' do
end.not_to raise_error
end
end
def chromedriver_version
Gem::Version.new(session.driver.browser.capabilities['chrome']['chromedriverVersion'].split[0])
end
end

View file

@ -81,7 +81,7 @@ RSpec.describe 'Capybara::Session with remote Chrome' do
before { skip 'Only makes sense in W3C mode' if ENV['W3C'] == 'false' }
it 'does not error when getting log types' do
skip if Gem::Version.new(session.driver.browser.capabilities['chrome']['chromedriverVersion'].split[0]) < Gem::Version.new('75.0.3770.90')
skip unless Gem::Requirement.new('>= 75.0.3770.90').satisfied_by? chromedriver_version
expect do
session.driver.browser.manage.logs.available_types
end.not_to raise_error
@ -93,4 +93,8 @@ RSpec.describe 'Capybara::Session with remote Chrome' do
end.not_to raise_error
end
end
def chromedriver_version
Gem::Version.new(session.driver.browser.capabilities['chrome']['chromedriverVersion'].split[0])
end
end

View file

@ -20,12 +20,9 @@ end
Capybara.register_driver :selenium_firefox do |app|
# ::Selenium::WebDriver.logger.level = "debug"
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
driver_options = { browser: :firefox, timeout: 31 }.tap do |opts|
if version >= Gem::Version.new('4.0.0.alpha6')
opts[:capabilities] = browser_options
else
opts[:options] = browser_options
end
opts[options_key] = browser_options
# Get a trace level log from geckodriver
# :driver_opts => { args: ['-vv'] }
end
@ -35,12 +32,9 @@ end
Capybara.register_driver :selenium_firefox_not_clear_storage do |app|
version = Capybara::Selenium::Driver.load_selenium
options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options
driver_options = { browser: :firefox, clear_local_storage: false, clear_session_storage: false }.tap do |opts|
if version >= Gem::Version.new('4.0.0.alpha6')
opts[:capabilities] = browser_options
else
opts[:options] = browser_options
end
opts[options_key] = browser_options
end
Capybara::Selenium::Driver.new(app, **driver_options)