Skip to content

Commit

Permalink
Merge pull request #2439 from teamcapybara/selenium_4_alpha
Browse files Browse the repository at this point in the history
Update for selenium 4 latest alpha - Fix issue #2431
  • Loading branch information
twalpole authored Jan 20, 2021
2 parents bd17992 + 2bc7058 commit a760f17
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ matrix:
rvm: 2.7
env: HEADLESS=true
- gemfile: gemfiles/Gemfile.beta-versions
rvm: 2.6
rvm: 3.0
env: CAPYBARA_FF=true
addons:
firefox: latest-beta
Expand Down
11 changes: 6 additions & 5 deletions gemfiles/Gemfile.beta-versions
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ gem 'pkg-config' # needed by nokogiri
gem 'xpath', github: 'teamcapybara/xpath'
gem 'rack', github: 'rack/rack'
gem 'sinatra', github: 'sinatra/sinatra', branch: 'master'
gem 'rspec', github: 'rspec/rspec'
gem 'rspec-core', github: 'rspec/rspec-core'
gem 'rspec-support', github: 'rspec/rspec-support'
gem 'rspec-mocks', github: 'rspec/rspec-mocks'
gem 'rspec-expectations', github: 'rspec/rspec-expectations'
gem 'rspec', github: 'rspec/rspec', branch: 'main'
gem 'rspec-core', github: 'rspec/rspec-core', branch: 'main'
gem 'rspec-support', github: 'rspec/rspec-support', branch: 'main'
gem 'rspec-mocks', github: 'rspec/rspec-mocks', branch: 'main'
gem 'rspec-expectations', github: 'rspec/rspec-expectations', branch: 'main'
gem 'cucumber', github: 'cucumber/cucumber-ruby'
gem 'cucumber-core', github: 'cucumber/cucumber-ruby-core'
gem 'cucumber-wire', github: 'cucumber/cucumber-ruby-wire'
gem 'puma', github: 'puma/puma'
gem 'selenium-webdriver', '>= 4.0.0.alpha7'

40 changes: 32 additions & 8 deletions lib/capybara/registrations/drivers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,56 @@
end

Capybara.register_driver :selenium_headless do |app|
Capybara::Selenium::Driver.load_selenium
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
Capybara::Selenium::Driver.new(app, browser: :firefox, options: browser_options)

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)
end
end

Capybara.register_driver :selenium_chrome do |app|
Capybara::Selenium::Driver.load_selenium
version = Capybara::Selenium::Driver.load_selenium
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
opts.args << '--disable-site-isolation-trials'
if opts.respond_to?(:add_argument)
opts.add_argument('--disable-site-isolation-trials')
else
opts.args << '--disable-site-isolation-trials'
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, browser: :chrome, options: browser_options)
end

Capybara.register_driver :selenium_chrome_headless do |app|
Capybara::Selenium::Driver.load_selenium
version = Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
opts.args << '--headless'
if opts.respond_to?(:headless!)
opts.headless!
else
opts.args << '--headless'
end
opts.args << '--disable-gpu' if Gem.win_platform?
# Workaround https://bugs.chromium.org/p/chromedriver/issues/detail?id=2650&q=load&sort=-id&colspec=ID%20Status%20Pri%20Owner%20Summary
opts.args << '--disable-site-isolation-trials'
end
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)

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
end
19 changes: 16 additions & 3 deletions lib/capybara/selenium/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
attr_reader :app, :options

class << self
attr_reader :selenium_webdriver_version

def load_selenium
require 'selenium-webdriver'
require 'capybara/selenium/logger_suppressor'
Expand All @@ -32,15 +34,18 @@ def load_selenium
# Selenium::WebDriver::VERSION. Ideally we'd
# use the constant in all cases, but earlier versions
# of `selenium-webdriver` didn't provide the constant.
selenium_webdriver_version =
@selenium_webdriver_version =
if Gem.loaded_specs['selenium-webdriver']
Gem.loaded_specs['selenium-webdriver'].version
else
Gem::Version.new(Selenium::WebDriver::VERSION)
end
if selenium_webdriver_version < Gem::Version.new('3.5.0')

if @selenium_webdriver_version < Gem::Version.new('3.5.0')
warn "Warning: You're using an unsupported version of selenium-webdriver, please upgrade."
end

@selenium_webdriver_version
rescue LoadError => e
raise e unless e.message.include?('selenium-webdriver')

Expand All @@ -66,7 +71,15 @@ def browser
end
end
processed_options = options.reject { |key, _val| SPECIAL_OPTIONS.include?(key) }
@browser = Selenium::WebDriver.for(options[:browser], **processed_options)

@browser = if options[:browser] == :firefox &&
RUBY_VERSION >= '3.0' &&
Capybara::Selenium::Driver.selenium_webdriver_version <= Gem::Version.new('4.0.0.alpha1')
# selenium-webdriver 3.x doesn't correctly pass options through for Firefox with Ruby 3 so workaround that
Selenium::WebDriver::Firefox::Driver.new(**processed_options)
else
Selenium::WebDriver.for(options[:browser], processed_options)
end

specialize_driver
setup_exit_handler
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/selenium/logger_suppressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(*)
super
end

def deprecate(*)
def deprecate(*, **, &block)
super unless @suppress_for_capybara
end

Expand Down
66 changes: 49 additions & 17 deletions spec/selenium_spec_chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,71 @@
browser_options.add_preference(:download, default_directory: Capybara.save_path)

Capybara.register_driver :selenium_chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options, timeout: 30).tap do |driver|
version = Capybara::Selenium::Driver.load_selenium
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
end

Capybara::Selenium::Driver.new(app, **driver_options).tap do |driver|
# Set download dir for Chrome < 77
driver.browser.download_path = Capybara.save_path
end
end

Capybara.register_driver :selenium_chrome_not_clear_storage do |app|
chrome_options = {
browser: :chrome,
options: browser_options
}
Capybara::Selenium::Driver.new(app, **chrome_options.merge(clear_local_storage: false, clear_session_storage: false))
version = Capybara::Selenium::Driver.load_selenium
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
end

Capybara::Selenium::Driver.new(app, **chrome_options)
end

Capybara.register_driver :selenium_chrome_not_clear_session_storage do |app|
chrome_options = {
browser: :chrome,
options: browser_options
}
Capybara::Selenium::Driver.new(app, **chrome_options.merge(clear_session_storage: false))
version = Capybara::Selenium::Driver.load_selenium
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
end

Capybara::Selenium::Driver.new(app, **chrome_options)
end

Capybara.register_driver :selenium_chrome_not_clear_local_storage do |app|
chrome_options = {
browser: :chrome,
options: browser_options
}
Capybara::Selenium::Driver.new(app, **chrome_options.merge(clear_local_storage: false))
version = Capybara::Selenium::Driver.load_selenium
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
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
subclass = Class.new(Capybara::Selenium::Driver)
subclass.new(app, browser: :chrome, options: browser_options, timeout: 30)
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
end

subclass.new(app, **chrome_options)
end

module TestSessions
Expand Down
34 changes: 21 additions & 13 deletions spec/selenium_spec_firefox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,36 @@
profile['browser.download.dir'] = Capybara.save_path
profile['browser.download.folderList'] = 2
profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/csv'
profile['browser.startup.homepage'] = 'about:blank' # workaround bug in Selenium 4 alpha4-7
end

Capybara.register_driver :selenium_firefox do |app|
# ::Selenium::WebDriver.logger.level = "debug"
Capybara::Selenium::Driver.new(
app,
browser: :firefox,
options: browser_options,
timeout: 31
version = Capybara::Selenium::Driver.load_selenium
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
# Get a trace level log from geckodriver
# :driver_opts => { args: ['-vv'] }
)
end

Capybara::Selenium::Driver.new(app, **driver_options)
end

Capybara.register_driver :selenium_firefox_not_clear_storage do |app|
Capybara::Selenium::Driver.new(
app,
browser: :firefox,
clear_local_storage: false,
clear_session_storage: false,
options: browser_options
)
version = Capybara::Selenium::Driver.load_selenium
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
end

Capybara::Selenium::Driver.new(app, **driver_options)
end

module TestSessions
Expand Down

0 comments on commit a760f17

Please sign in to comment.