diff --git a/.rubocop.yml b/.rubocop.yml index def0539c..9edc76e8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -62,6 +62,12 @@ Lint/UnusedMethodArgument: - 'lib/capybara/driver/base.rb' - 'lib/capybara/driver/node.rb' +Lint/RaiseException: + Enabled: true + +Lint/StructNewOverride: + Enabled: true + Layout/EndAlignment: EnforcedStyleAlignWith: variable diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index aa361ce6..9acb60e9 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -241,7 +241,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base def quit @browser&.quit - rescue Selenium::WebDriver::Error::SessionNotCreatedError, Errno::ECONNREFUSED # rubocop:disable Lint/SuppressedException + rescue Selenium::WebDriver::Error::SessionNotCreatedError, Errno::ECONNREFUSED # Browser must have already gone rescue Selenium::WebDriver::Error::UnknownError => e unless silenced_unknown_error_message?(e.message) # Most likely already gone @@ -293,7 +293,7 @@ private def clear_browser_state delete_all_cookies clear_storage - rescue *clear_browser_state_errors # rubocop:disable Lint/SuppressedException + rescue *clear_browser_state_errors # delete_all_cookies fails when we've previously gone # to about:blank, so we rescue this error and do nothing # instead. @@ -317,7 +317,7 @@ private def clear_storage clear_session_storage unless options[:clear_session_storage] == false clear_local_storage unless options[:clear_local_storage] == false - rescue Selenium::WebDriver::Error::JavascriptError # rubocop:disable Lint/SuppressedException + rescue Selenium::WebDriver::Error::JavascriptError # session/local storage may not be available if on non-http pages (e.g. about:blank) end @@ -353,7 +353,7 @@ private @browser.navigate.to(url) sleep 0.1 # slight wait for alert @browser.switch_to.alert.accept - rescue modal_error # rubocop:disable Lint/SuppressedException + rescue modal_error # alert now gone, should mean navigation happened end diff --git a/lib/capybara/selenium/driver_specializations/firefox_driver.rb b/lib/capybara/selenium/driver_specializations/firefox_driver.rb index c478858c..6c291626 100644 --- a/lib/capybara/selenium/driver_specializations/firefox_driver.rb +++ b/lib/capybara/selenium/driver_specializations/firefox_driver.rb @@ -46,7 +46,7 @@ module Capybara::Selenium::Driver::W3CFirefoxDriver begin # Firefox 68 hangs if we try to switch windows while a modal is visible browser.switch_to.alert&.dismiss - rescue Selenium::WebDriver::Error::NoSuchAlertError # rubocop:disable Lint/SuppressedException + rescue Selenium::WebDriver::Error::NoSuchAlertError # Swallow end end @@ -61,7 +61,7 @@ module Capybara::Selenium::Driver::W3CFirefoxDriver accept_modal :confirm, wait: 0.1 do super end - rescue Capybara::ModalNotFound # rubocop:disable Lint/SuppressedException + rescue Capybara::ModalNotFound # No modal was opened - page has refreshed - ignore end diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index 1451be98..a7a0c816 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -237,7 +237,7 @@ protected JS begin driver.execute_script(script, self) - rescue StandardError # rubocop:disable Lint/SuppressedException + rescue StandardError # Swallow error if scrollIntoView with options isn't supported end end @@ -281,7 +281,7 @@ private driver.execute_script 'arguments[0].select()', self unless clear == :none if rapid == true || (value.length > 30 && rapid != false) send_keys(value[0..3]) - driver.execute_script 'arguments[0].value += arguments[1]', self, value[4...-3] + driver.execute_script RAPID_SET_TEXT, self, value[4...-3] send_keys(value[-3..-1]) else send_keys(value) @@ -530,6 +530,16 @@ private })(arguments[0], arguments[1], arguments[2]) JS + RAPID_SET_TEXT = <<~'JS' + (function(el, val) { + var value = el.value + val; + if (el.maxLength != -1){ + value = value.slice(0, el.maxLength); + } + el.value = value; + })(arguments[0], arguments[1]) + JS + # SettableValue encapsulates time/date field formatting class SettableValue attr_reader :value diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb index 7e2f97e9..452e8412 100644 --- a/lib/capybara/spec/views/form.erb +++ b/lib/capybara/spec/views/form.erb @@ -143,6 +143,10 @@

+

+ +

+

diff --git a/spec/shared_selenium_node.rb b/spec/shared_selenium_node.rb index 872dfe1a..e606eb7b 100644 --- a/spec/shared_selenium_node.rb +++ b/spec/shared_selenium_node.rb @@ -35,6 +35,16 @@ RSpec.shared_examples 'Capybara::Node' do |session, _mode| end end + describe '#set' do + it 'respects maxlength when using rapid set' do + session.visit('/form') + inp = session.find(:css, '#long_length') + value = (0...50).map { |i| ((i % 26) + 65).chr }.join + inp.set(value, rapid: true) + expect(inp.value).to eq value[0...35] + end + end + describe '#visible?' do let(:bridge) do session.driver.browser.send(:bridge)