diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 63e109ea..7602f3a3 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -430,6 +430,10 @@ private ::Capybara::Selenium::Node.new(self, native_node, initial_cache) end + def bridge + browser.send(:bridge) + end + def specialize_driver browser_type = browser.browser Capybara::Selenium::Driver.specializations.select { |k, _v| k === browser_type }.each_value do |specialization| # rubocop:disable Style/CaseEquality diff --git a/lib/capybara/selenium/driver_specializations/chrome_driver.rb b/lib/capybara/selenium/driver_specializations/chrome_driver.rb index 99ffa25e..535ff92f 100644 --- a/lib/capybara/selenium/driver_specializations/chrome_driver.rb +++ b/lib/capybara/selenium/driver_specializations/chrome_driver.rb @@ -108,10 +108,6 @@ private ::Capybara::Selenium::ChromeNode.new(self, native_node, initial_cache) end - def bridge - browser.send(:bridge) - end - def chromedriver_version @chromedriver_version ||= begin caps = browser.capabilities diff --git a/lib/capybara/selenium/driver_specializations/edge_driver.rb b/lib/capybara/selenium/driver_specializations/edge_driver.rb index a6289440..3422e6d1 100644 --- a/lib/capybara/selenium/driver_specializations/edge_driver.rb +++ b/lib/capybara/selenium/driver_specializations/edge_driver.rb @@ -108,10 +108,6 @@ private ::Capybara::Selenium::EdgeNode.new(self, native_node, initial_cache) end - def bridge - browser.send(:bridge) - end - def edgedriver_version @edgedriver_version ||= begin caps = browser.capabilities diff --git a/lib/capybara/selenium/driver_specializations/firefox_driver.rb b/lib/capybara/selenium/driver_specializations/firefox_driver.rb index 2cb77ddb..53e6ac58 100644 --- a/lib/capybara/selenium/driver_specializations/firefox_driver.rb +++ b/lib/capybara/selenium/driver_specializations/firefox_driver.rb @@ -13,12 +13,6 @@ module Capybara::Selenium::Driver::FirefoxDriver (defined?(Selenium::WebDriver::VERSION) && (Selenium::WebDriver::VERSION.to_f >= 4)) || driver.browser.capabilities.is_a?(::Selenium::WebDriver::Remote::W3C::Capabilities) end - -private - - def bridge - browser.send(:bridge) - end end module Capybara::Selenium::Driver::W3CFirefoxDriver diff --git a/lib/capybara/selenium/driver_specializations/safari_driver.rb b/lib/capybara/selenium/driver_specializations/safari_driver.rb index f68a1e14..e085a07d 100644 --- a/lib/capybara/selenium/driver_specializations/safari_driver.rb +++ b/lib/capybara/selenium/driver_specializations/safari_driver.rb @@ -18,10 +18,6 @@ private def build_node(native_node, initial_cache = {}) ::Capybara::Selenium::SafariNode.new(self, native_node, initial_cache) end - - def bridge - browser.send(:bridge) - end end Capybara::Selenium::Driver.register_specialization(/^(safari|Safari_Technology_Preview)$/, diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index b2b40be8..f7c7a2bb 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -299,11 +299,31 @@ private end def set_file(value) # rubocop:disable Naming/AccessorMethodName - path_names = value.to_s.empty? ? [] : value - file_names = Array(path_names).map do |pn| - Pathname.new(pn).absolute? ? pn : File.expand_path(pn) - end.join("\n") - native.send_keys(file_names) + with_file_detector do + path_names = value.to_s.empty? ? [] : value + file_names = Array(path_names).map do |pn| + Pathname.new(pn).absolute? ? pn : File.expand_path(pn) + end.join("\n") + native.send_keys(file_names) + end + end + + def with_file_detector + if driver.options[:browser] == :remote && + bridge.respond_to?(:file_detector) && + bridge.file_detector.nil? + begin + bridge.file_detector = lambda do |(fn, *)| + str = fn.to_s + str if File.exist?(str) + end + yield + ensure + bridge.file_detector = nil + end + else + yield + end end def set_content_editable(value) # rubocop:disable Naming/AccessorMethodName @@ -359,6 +379,10 @@ private driver.browser end + def bridge + browser.send(:bridge) + end + def browser_action browser.action end diff --git a/lib/capybara/selenium/nodes/chrome_node.rb b/lib/capybara/selenium/nodes/chrome_node.rb index ac869258..0ba91dfd 100644 --- a/lib/capybara/selenium/nodes/chrome_node.rb +++ b/lib/capybara/selenium/nodes/chrome_node.rb @@ -84,10 +84,6 @@ private end end - def bridge - driver.browser.send(:bridge) - end - def w3c? (defined?(Selenium::WebDriver::VERSION) && (Selenium::WebDriver::VERSION.to_f >= 4)) || capabilities.is_a?(::Selenium::WebDriver::Remote::W3C::Capabilities) diff --git a/lib/capybara/selenium/nodes/edge_node.rb b/lib/capybara/selenium/nodes/edge_node.rb index 6fbf5cbf..8bded239 100644 --- a/lib/capybara/selenium/nodes/edge_node.rb +++ b/lib/capybara/selenium/nodes/edge_node.rb @@ -69,10 +69,6 @@ private end end - def bridge - driver.browser.send(:bridge) - end - def browser_version @browser_version ||= begin caps = driver.browser.capabilities diff --git a/lib/capybara/selenium/nodes/firefox_node.rb b/lib/capybara/selenium/nodes/firefox_node.rb index 113ce73f..07fcbffb 100644 --- a/lib/capybara/selenium/nodes/firefox_node.rb +++ b/lib/capybara/selenium/nodes/firefox_node.rb @@ -115,10 +115,6 @@ private actions end - def bridge - driver.browser.send(:bridge) - end - def upload(local_file) return nil unless local_file raise ArgumentError, "You may only upload files: #{local_file.inspect}" unless File.file?(local_file) diff --git a/lib/capybara/selenium/nodes/safari_node.rb b/lib/capybara/selenium/nodes/safari_node.rb index 745aa54e..4305164a 100644 --- a/lib/capybara/selenium/nodes/safari_node.rb +++ b/lib/capybara/selenium/nodes/safari_node.rb @@ -90,10 +90,6 @@ class Capybara::Selenium::SafariNode < Capybara::Selenium::Node private - def bridge - driver.browser.send(:bridge) - end - def _send_keys(keys, actions = browser_action, down_keys = ModifierKeysStack.new) case keys when *MODIFIER_KEYS diff --git a/spec/selenium_spec_chrome_remote.rb b/spec/selenium_spec_chrome_remote.rb index 79e5f940..afcaf53b 100644 --- a/spec/selenium_spec_chrome_remote.rb +++ b/spec/selenium_spec_chrome_remote.rb @@ -54,12 +54,6 @@ module TestSessions Chrome = Capybara::Session.new(CHROME_REMOTE_DRIVER, TestApp) end -TestSessions::Chrome.driver.browser.file_detector = lambda do |args| - # args => ["/path/to/file"] - str = args.first.to_s - str if File.exist?(str) -end - skipped_tests = %i[response_headers status_code trigger download] Capybara::SpecHelper.run_specs TestSessions::Chrome, CHROME_REMOTE_DRIVER.to_s, capybara_skip: skipped_tests do |example| diff --git a/spec/selenium_spec_firefox_remote.rb b/spec/selenium_spec_firefox_remote.rb index 9a77fba7..571654c3 100644 --- a/spec/selenium_spec_firefox_remote.rb +++ b/spec/selenium_spec_firefox_remote.rb @@ -50,12 +50,6 @@ module TestSessions RemoteFirefox = Capybara::Session.new(FIREFOX_REMOTE_DRIVER, TestApp) end -TestSessions::RemoteFirefox.driver.browser.file_detector = lambda do |args| - # args => ["/path/to/file"] - str = args.first.to_s - str if File.exist?(str) -end - skipped_tests = %i[response_headers status_code trigger download] Capybara::SpecHelper.run_specs TestSessions::RemoteFirefox, FIREFOX_REMOTE_DRIVER.to_s, capybara_skip: skipped_tests do |example| diff --git a/spec/selenium_spec_ie.rb b/spec/selenium_spec_ie.rb index 0ad42396..4a73d92f 100644 --- a/spec/selenium_spec_ie.rb +++ b/spec/selenium_spec_ie.rb @@ -39,12 +39,7 @@ Capybara.register_driver :selenium_ie do |app| Capybara::Selenium::Driver.new(app, browser: :remote, options: options, - url: url).tap do |driver| - driver.browser.file_detector = lambda do |args| - str = args.first.to_s - str if File.exist?(str) - end - end + url: url) else Capybara::Selenium::Driver.new( app,