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 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 1e9289ea..54ce9e1a 100644 --- a/spec/support/views/set.erb +++ b/spec/support/views/set.erb @@ -1,10 +1,18 @@ + - + + Set tests
Content
+
+
+
+
+ +