diff --git a/lib/capybara/selenium/nodes/chrome_node.rb b/lib/capybara/selenium/nodes/chrome_node.rb index 18d6e7a7..c0c865d5 100644 --- a/lib/capybara/selenium/nodes/chrome_node.rb +++ b/lib/capybara/selenium/nodes/chrome_node.rb @@ -13,6 +13,14 @@ class Capybara::Selenium::ChromeNode < Capybara::Selenium::Node end def set_file(value) # rubocop:disable Naming/AccessorMethodName + # In Chrome 75+ files are appended (due to WebDriver spec - why?) so we have to clear here if its multiple and already set + if browser_version >= 75.0 + driver.execute_script(<<~JS, self) + if (arguments[0].multiple && (arguments[0].files.length > 0)){ + arguments[0].value = null; + } + JS + end super(value) rescue *file_errors => e raise ArgumentError, "Selenium < 3.14 with remote Chrome doesn't support multiple file upload" if e.message.match?(/File not found : .+\n.+/m) @@ -58,4 +66,14 @@ private def bridge driver.browser.send(:bridge) end + + def w3c? + (defined?(Selenium::WebDriver::VERSION) && (Selenium::WebDriver::VERSION.to_f >= 4)) || + driver.browser.capabilities.is_a?(::Selenium::WebDriver::Remote::W3C::Capabilities) + end + + def browser_version + caps = driver.browser.capabilities + (caps[:browser_version] || caps[:version]).to_f + end end diff --git a/lib/capybara/selenium/nodes/firefox_node.rb b/lib/capybara/selenium/nodes/firefox_node.rb index b6803574..cfd11bbb 100644 --- a/lib/capybara/selenium/nodes/firefox_node.rb +++ b/lib/capybara/selenium/nodes/firefox_node.rb @@ -23,7 +23,11 @@ class Capybara::Selenium::FirefoxNode < Capybara::Selenium::Node def set_file(value) # rubocop:disable Naming/AccessorMethodName # By default files are appended so we have to clear here if its multiple and already set - native.clear if multiple? && driver.evaluate_script('arguments[0].files', self).any? + driver.execute_script(<<~JS, self) + if (arguments[0].multiple && (arguments[0].files.length > 0)){ + arguments[0].value = null; + } + JS return super if browser_version >= 62.0 # Workaround lack of support for multiple upload by uploading one at a time diff --git a/spec/selenium_spec_firefox.rb b/spec/selenium_spec_firefox.rb index 021b9b05..31b9f8f9 100644 --- a/spec/selenium_spec_firefox.rb +++ b/spec/selenium_spec_firefox.rb @@ -53,9 +53,6 @@ Capybara::SpecHelper.run_specs TestSessions::SeleniumFirefox, 'selenium', capyba pending "selenium-webdriver/geckodriver doesn't generate double click event" if firefox_lt?(59, @session) when 'Capybara::Session selenium #accept_prompt should accept the prompt with a blank response when there is a default' pending "Geckodriver doesn't set a blank response in FF < 63 - https://bugzilla.mozilla.org/show_bug.cgi?id=1486485" if firefox_lt?(63, @session) - when 'Capybara::Session selenium #attach_file with multipart form should fire change once for each set of files uploaded' - pending 'Gekcodriver appends files so we have to first call clear for multiple files which creates an extra change ' \ - 'if files are already set' when 'Capybara::Session selenium #attach_file with multipart form should fire change once when uploading multiple files from empty' pending "FF < 62 doesn't support setting all files at once" if firefox_lt?(62, @session) when 'Capybara::Session selenium #accept_confirm should work with nested modals' diff --git a/spec/selenium_spec_firefox_remote.rb b/spec/selenium_spec_firefox_remote.rb index bd4764bd..4c8c8a97 100644 --- a/spec/selenium_spec_firefox_remote.rb +++ b/spec/selenium_spec_firefox_remote.rb @@ -63,9 +63,6 @@ Capybara::SpecHelper.run_specs TestSessions::RemoteFirefox, FIREFOX_REMOTE_DRIVE skip "Firefox doesn't generate an event for shift+control+click" if firefox_gte?(62, @session) when 'Capybara::Session selenium_firefox_remote #accept_prompt should accept the prompt with a blank response when there is a default' pending "Geckodriver doesn't set a blank response in FF < 63 - https://bugzilla.mozilla.org/show_bug.cgi?id=1486485" if firefox_lt?(63, @session) - when 'Capybara::Session selenium_firefox_remote #attach_file with multipart form should fire change once for each set of files uploaded' - pending 'Gekcodriver appends files so we have to first call clear for multiple files which creates an extra change ' \ - 'if files are already set' when 'Capybara::Session selenium_firefox_remote #attach_file with multipart form should fire change once when uploading multiple files from empty' pending "FF < 62 doesn't support setting all files at once" if firefox_lt?(62, @session) when 'Capybara::Session selenium_firefox_remote #reset_session! removes ALL cookies'