From 431482b751e5a65e9902eca1ece7abbd36646bee Mon Sep 17 00:00:00 2001 From: Darragh Buckley Date: Mon, 16 Apr 2018 12:05:25 -0700 Subject: [PATCH] Use `load_selenium` class method for requiring `selenium-webdriver`. (#2013) * Use `load_selenium` class method for requiring `selenium-webdriver`. This change relates to this issue: https://github.com/teamcapybara/capybara/issues/2011 `Capybara::Selenium::Driver#load_selenium` is bumped to a class method which is called either during `Selenium::Driver` instantiation or before Selenium is used. --- lib/capybara.rb | 1 + lib/capybara/selenium/driver.rb | 36 ++++++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/capybara.rb b/lib/capybara.rb index 3273470b..badb8766 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -494,6 +494,7 @@ Capybara.register_driver :selenium_chrome do |app| end Capybara.register_driver :selenium_chrome_headless do |app| + Capybara::Selenium::Driver.load_selenium browser_options = ::Selenium::WebDriver::Chrome::Options.new browser_options.args << '--headless' browser_options.args << '--disable-gpu' if Gem.win_platform? diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 76261d1c..f519f709 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -13,6 +13,23 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base attr_reader :app, :options + def self.load_selenium + require 'selenium-webdriver' + # Fix for selenium-webdriver 3.4.0 which misnamed these + unless defined?(::Selenium::WebDriver::Error::ElementNotInteractableError) + ::Selenium::WebDriver::Error.const_set('ElementNotInteractableError', Class.new(::Selenium::WebDriver::Error::WebDriverError)) + end + unless defined?(::Selenium::WebDriver::Error::ElementClickInterceptedError) + ::Selenium::WebDriver::Error.const_set('ElementClickInterceptedError', Class.new(::Selenium::WebDriver::Error::WebDriverError)) + end + rescue LoadError => e + if e.message =~ /selenium-webdriver/ + raise LoadError, "Capybara's selenium driver is unable to load `selenium-webdriver`, please install the gem and add `gem 'selenium-webdriver'` to your Gemfile if you are using bundler." + else + raise e + end + end + def browser unless @browser if firefox? @@ -38,7 +55,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base end def initialize(app, **options) - load_selenium + self.class.load_selenium @session = nil @app = app @browser = nil @@ -472,21 +489,4 @@ private arg end end - - def load_selenium - require 'selenium-webdriver' - # Fix for selenium-webdriver 3.4.0 which misnamed these - unless defined?(::Selenium::WebDriver::Error::ElementNotInteractableError) - ::Selenium::WebDriver::Error.const_set('ElementNotInteractableError', Class.new(::Selenium::WebDriver::Error::WebDriverError)) - end - unless defined?(::Selenium::WebDriver::Error::ElementClickInterceptedError) - ::Selenium::WebDriver::Error.const_set('ElementClickInterceptedError', Class.new(::Selenium::WebDriver::Error::WebDriverError)) - end - rescue LoadError => e - if e.message =~ /selenium-webdriver/ - raise LoadError, "Capybara's selenium driver is unable to load `selenium-webdriver`, please install the gem and add `gem 'selenium-webdriver'` to your Gemfile if you are using bundler." - else - raise e - end - end end