From 343ed98a3b2f912cea22250576eca6433979f50d Mon Sep 17 00:00:00 2001 From: Daniel Heath Date: Fri, 21 Mar 2025 12:35:00 +1100 Subject: [PATCH 1/6] Implement date input types based on the capybara selenium driver --- lib/capybara/cuprite/node.rb | 41 ++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index d4f12395..fc645428 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -101,8 +101,28 @@ def set(value, options = {}) when "file" files = value.respond_to?(:to_ary) ? value.to_ary.map(&:to_s) : value.to_s command(:select_file, files) - when "color" - node.evaluate("this.setAttribute('value', '#{value}')") + when 'date' + if value.respond_to?(:to_time) + set_value_js(value.to_date.iso8601) + else + command(:set, value.to_s) + end + when 'time' + if value.respond_to?(:to_time) && !value.is_a?(String) + set_value_js(value.to_time.strftime('%H:%M')) + else + command(:set, value.to_s) + end + when 'datetime-local' + if value.respond_to?(:to_time) && !value.is_a?(String) + set_value_js(value.to_time.strftime('%Y-%m-%dT%H:%M')) + else + command(:set, value.to_s) + end + when 'range' + set_value_js(value) + when 'color' + set_value_js(value) else command(:set, value.to_s) end @@ -114,6 +134,23 @@ def set(value, options = {}) end end + # Copied from MIT licensed capybara project + # revision 0be79d6 + # path lib/capybara/selenium/node.rb + def set_value_js(value) + driver.execute_script(<<-JS, self, value) + if (arguments[0].readOnly) { return }; + if (document.activeElement !== arguments[0]){ + arguments[0].focus(); + } + if (arguments[0].value != arguments[1]) { + arguments[0].value = arguments[1] + arguments[0].dispatchEvent(new InputEvent('input')); + arguments[0].dispatchEvent(new Event('change', { bubbles: true })); + } + JS + end + def select_option command(:select, true) end From 6e4da4de1bb6a610a6473bf15c83e962aee8a725 Mon Sep 17 00:00:00 2001 From: Duncan Bayne Date: Wed, 26 Mar 2025 09:26:55 +1100 Subject: [PATCH 2/6] Make fixture form valid HTML --- spec/support/views/set.erb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/support/views/set.erb b/spec/support/views/set.erb index 1e9289ea..d58e674d 100644 --- a/spec/support/views/set.erb +++ b/spec/support/views/set.erb @@ -1,6 +1,8 @@ + - + + Set tests From c7a2a7631d79f02a879461d75b2eb306e4f2d5cb Mon Sep 17 00:00:00 2001 From: Duncan Bayne Date: Wed, 26 Mar 2025 09:47:24 +1100 Subject: [PATCH 3/6] Add specs for additional set types --- spec/features/driver_spec.rb | 24 ++++++++++++++++++++++++ spec/support/views/set.erb | 6 ++++++ 2 files changed, 30 insertions(+) diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index 4896324c..1d830301 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -1468,6 +1468,30 @@ def create_screenshot(file, *args) expect(input.text).to eq("replacement text") end + it "sets a date field" do + input = @session.find(:css, "#date-field") + input.set(Time.new(2000, 12, 31)) + expect(input.value).to eq("2000-12-31") + end + + it "sets a datetime-local field" do + input = @session.find(:css, "#datetime-local-field") + input.set(Time.new(2000, 12, 31, 17, 15)) + expect(input.value).to eq(Time.new(2000, 12, 31, 17, 15).strftime("%Y-%m-%dT%H:%M")) + end + + it "sets a range field" do + input = @session.find(:css, "#range-field") + input.set(42) + expect(input.value).to eq("42") + end + + it "sets a color field" do + input = @session.find(:css, "#color-field") + input.set("#1270a4") + expect(input.value).to eq("#1270a4") + end + it "sets a content editable childs content" do @session.visit("/with_js") @session.find(:css, "#existing_content_editable_child").set("WYSIWYG") diff --git a/spec/support/views/set.erb b/spec/support/views/set.erb index d58e674d..54ce9e1a 100644 --- a/spec/support/views/set.erb +++ b/spec/support/views/set.erb @@ -8,5 +8,11 @@
Content
+
+
+
+
+ +
From 7174e462a320fa90e5f71cc818d71d7f93cd58ea Mon Sep 17 00:00:00 2001 From: Duncan Bayne Date: Mon, 4 May 2026 13:21:48 +1000 Subject: [PATCH 4/6] Remove dead code --- lib/capybara/cuprite/node.rb | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index c0408bea..df10a98f 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -131,23 +131,6 @@ def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Met end end - # Copied from MIT licensed capybara project - # revision 0be79d6 - # path lib/capybara/selenium/node.rb - def set_value_js(value) - driver.execute_script(<<-JS, self, value) - if (arguments[0].readOnly) { return }; - if (document.activeElement !== arguments[0]){ - arguments[0].focus(); - } - if (arguments[0].value != arguments[1]) { - arguments[0].value = arguments[1] - arguments[0].dispatchEvent(new InputEvent('input')); - arguments[0].dispatchEvent(new Event('change', { bubbles: true })); - } - JS - end - def select_option command(:select, true) end From 9a9a9c032193858481c3570f46af762f89e742f4 Mon Sep 17 00:00:00 2001 From: Duncan Bayne Date: Mon, 4 May 2026 13:22:05 +1000 Subject: [PATCH 5/6] Remove redundant when clause --- lib/capybara/cuprite/node.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index df10a98f..c71a4d5d 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -116,9 +116,7 @@ def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Met value = value.to_date.strftime("%G-W%V") if !value.is_a?(String) && value.respond_to?(:to_date) command(:set, value.to_s) when "datetime-local" - value = value.to_time.strftime('%Y-%m-%dT%H:%M') if !value.is_a?(String) && value.respond_to?(:to_time) - command(:set, value.to_s) - when "range" + value = value.to_time.strftime("%Y-%m-%dT%H:%M") if !value.is_a?(String) && value.respond_to?(:to_time) command(:set, value.to_s) else command(:set, value.to_s) From d41d77e4e67ae18eeada13bd5688bf8e0582b71b Mon Sep 17 00:00:00 2001 From: Duncan Bayne Date: Mon, 4 May 2026 13:22:20 +1000 Subject: [PATCH 6/6] Ignore additional complexity metric --- lib/capybara/cuprite/node.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index c71a4d5d..45c58eb1 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -89,7 +89,7 @@ def value command(:value) end - def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize warn "Options passed to Node#set but Cuprite doesn't currently support any - ignoring" unless options.empty? if tag_name == "input"