Skip to content

Commit 3203b8c

Browse files
DanielHeathduncan-bayne
authored andcommitted
Implement date input types based on the capybara selenium driver
1 parent 4fa8126 commit 3203b8c

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

lib/capybara/cuprite/node.rb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,28 @@ def set(value, options = {})
101101
when "file"
102102
files = value.respond_to?(:to_ary) ? value.to_ary.map(&:to_s) : value.to_s
103103
command(:select_file, files)
104-
when "color"
105-
node.evaluate("this.setAttribute('value', '#{value}')")
104+
when 'date'
105+
if value.respond_to?(:to_time)
106+
set_value_js(value.to_date.iso8601)
107+
else
108+
command(:set, value.to_s)
109+
end
110+
when 'time'
111+
if value.respond_to?(:to_time) && !value.is_a?(String)
112+
set_value_js(value.to_time.strftime('%H:%M'))
113+
else
114+
command(:set, value.to_s)
115+
end
116+
when 'datetime-local'
117+
if value.respond_to?(:to_time) && !value.is_a?(String)
118+
set_value_js(value.to_time.strftime('%Y-%m-%dT%H:%M'))
119+
else
120+
command(:set, value.to_s)
121+
end
122+
when 'range'
123+
set_value_js(value)
124+
when 'color'
125+
set_value_js(value)
106126
else
107127
command(:set, value.to_s)
108128
end
@@ -114,6 +134,23 @@ def set(value, options = {})
114134
end
115135
end
116136

137+
# Copied from MIT licensed capybara project
138+
# revision 0be79d6
139+
# path lib/capybara/selenium/node.rb
140+
def set_value_js(value)
141+
driver.execute_script(<<-JS, self, value)
142+
if (arguments[0].readOnly) { return };
143+
if (document.activeElement !== arguments[0]){
144+
arguments[0].focus();
145+
}
146+
if (arguments[0].value != arguments[1]) {
147+
arguments[0].value = arguments[1]
148+
arguments[0].dispatchEvent(new InputEvent('input'));
149+
arguments[0].dispatchEvent(new Event('change', { bubbles: true }));
150+
}
151+
JS
152+
end
153+
117154
def select_option
118155
command(:select, true)
119156
end

0 commit comments

Comments
 (0)