Configure browser only when using Selenium

Since #39179, `selenium/webdriver` is required only when using the
`:selenium` driver.  However, `ActionDispatch::SystemTesting::Browser`
references `::Selenium` as soon as it is instantiated, and thus raises
an error when using a non-`:selenium` driver.

This commit ensures that `ActionDispatch::SystemTesting::Browser` is
instantiated only when using the `:selenium` driver.
This commit is contained in:
Jonathan Hefner 2021-07-01 15:36:32 -05:00
parent f263530bf7
commit 000969928d
2 changed files with 7 additions and 9 deletions

View File

@ -5,13 +5,13 @@ module ActionDispatch
class Driver # :nodoc:
def initialize(name, **options, &capabilities)
@name = name
@browser = Browser.new(options[:using])
@screen_size = options[:screen_size]
@options = options[:options] || {}
@capabilities = capabilities
if name == :selenium
require "selenium/webdriver"
@browser = Browser.new(options[:using])
@browser.preload
end
end
@ -28,7 +28,7 @@ module ActionDispatch
end
def register
@browser.configure(&@capabilities)
@browser&.configure(&@capabilities)
Capybara.register_driver @name do |app|
case @name

View File

@ -148,13 +148,11 @@ class DriverTest < ActiveSupport::TestCase
::Selenium::WebDriver::Chrome::Service.driver_path = original_driver_path
end
test "does not preload if used driver is not :selenium" do
assert_not_called_on_instance_of(ActionDispatch::SystemTesting::Browser, :preload) do
ActionDispatch::SystemTesting::Driver.new(:rack_test, using: :chrome)
end
test "does not configure browser if driver is not :selenium" do
# sanity check
assert ActionDispatch::SystemTesting::Driver.new(:selenium).instance_variable_get(:@browser)
assert_not_called_on_instance_of(ActionDispatch::SystemTesting::Browser, :preload) do
ActionDispatch::SystemTesting::Driver.new(:poltergeist)
end
assert_nil ActionDispatch::SystemTesting::Driver.new(:rack_test).instance_variable_get(:@browser)
assert_nil ActionDispatch::SystemTesting::Driver.new(:poltergeist).instance_variable_get(:@browser)
end
end