From ebcaa38d3aa3f3d4ce1cc953c9d10781cdc85d44 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 21 Jan 2020 13:55:34 +0000 Subject: [PATCH] Set range inputs via JS in selenium driver The slider can't be controlled so set it via JS like color inputs. --- History.md | 2 ++ lib/capybara/selenium/node.rb | 6 ++++++ lib/capybara/spec/views/form.erb | 5 +++++ spec/shared_selenium_session.rb | 12 ++++++++++++ 4 files changed, 25 insertions(+) diff --git a/History.md b/History.md index d29918fc..6e1c804b 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,8 @@ # Version 3.30.1 Release date: unreleased +* Support setting `` elements with the selenium driver + * Fix Ruby 2.7 deprecation notices around keyword arguments. I have tried to do this without any breaking changes, but due to the nature of the 2.7 changes and some selector types accepting Hashes as locators there are a lot of edge cases. If you find any broken cases please report diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index fdef20a2..905cdf31 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -78,6 +78,8 @@ class Capybara::Selenium::Node < Capybara::Driver::Node set_datetime_local(value) when 'color' set_color(value) + when 'range' + set_range(value) else set_text(value, **options) end @@ -296,6 +298,10 @@ private update_value_js(value) end + def set_range(value) # rubocop:disable Naming/AccessorMethodName + update_value_js(value) + end + def update_value_js(value) driver.execute_script(<<-JS, self, value) if (arguments[0].readOnly) { return }; diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb index 5ba961fb..521472b3 100644 --- a/lib/capybara/spec/views/form.erb +++ b/lib/capybara/spec/views/form.erb @@ -62,6 +62,11 @@

+

+ + +

+

diff --git a/spec/shared_selenium_session.rb b/spec/shared_selenium_session.rb index af822883..248df414 100644 --- a/spec/shared_selenium_session.rb +++ b/spec/shared_selenium_session.rb @@ -205,6 +205,18 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode| end end + describe '#fill_in with input[type="range"]' do + before do + session.visit('/form') + end + + it 'should set the range slider correctly' do + session.fill_in('form_age', with: 51) + session.click_button('awesome') + expect(Integer(extract_results(session)['age'])).to eq 51 + end + end + describe '#path' do it 'returns xpath' do # this is here because it is testing for an XPath that is specific to the algorithm used in the selenium driver