Skip to content

Commit d3336eb

Browse files
committed
Add support for week and month inputs
1 parent 08df7e7 commit d3336eb

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

lib/capybara/cuprite/javascripts/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Cuprite {
127127
this.trigger(node, "focus");
128128
this.setValue(node, "");
129129

130-
if (node.type == "number" || node.type == "date" || node.type == "range" || node.type == "time") {
130+
if (node.type == "number" || node.type == "date" || node.type == "range" || node.type == "time" || node.type == "month" || node.type == "week") {
131131
this.setValue(node, value);
132132
this.input(node);
133133
} else if (node.type == "datetime-local") {

lib/capybara/cuprite/node.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,17 @@ def set(value, options = {})
103103
command(:select_file, files)
104104
when "color"
105105
node.evaluate("this.setAttribute('value', '#{value}')")
106+
when 'date'
107+
value = value.to_date.iso8601 if !value.is_a?(String) && value.respond_to?(:to_date)
108+
command(:set, value.to_s)
106109
when "time"
107-
value = value.strftime("%H:%M") if value.is_a?(Time)
110+
value = value.to_time.strftime("%H:%M") if !value.is_a?(String) && value.respond_to?(:to_time)
111+
command(:set, value.to_s)
112+
when "month"
113+
value = value.to_date.strftime("%Y-%m") if !value.is_a?(String) && value.respond_to?(:to_date)
114+
command(:set, value.to_s)
115+
when "week"
116+
value = value.to_date.strftime("%G-W%V") if !value.is_a?(String) && value.respond_to?(:to_date)
108117
command(:set, value.to_s)
109118
else
110119
command(:set, value.to_s)

spec/features/session_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@
266266
expect(element.value).to eq("17:21")
267267
end
268268

269+
it "sets a value for a time input with a date object" do
270+
element = @session.find(:css, "#change_me_time")
271+
element.set(Date.new(2023, 9, 26))
272+
expect(element.value).to eq("00:00")
273+
end
274+
275+
it "sets a value for a time input with a datetime object" do
276+
element = @session.find(:css, "#change_me_time")
277+
element.set(DateTime.new(2023, 9, 26, 17, 21))
278+
expect(element.value).to eq("17:21")
279+
end
280+
269281
it "sets a value for a date input" do
270282
element = @session.find(:css, "#change_me_date")
271283
element.set("2023-09-26")
@@ -277,6 +289,67 @@
277289
element.set(Date.new(2023, 9, 26))
278290
expect(element.value).to eq("2023-09-26")
279291
end
292+
293+
it "sets a value for a date input with a time object" do
294+
element = @session.find(:css, "#change_me_date")
295+
element.set(Time.new(2023, 9, 26, 17, 21))
296+
expect(element.value).to eq("2023-09-26")
297+
end
298+
299+
it "sets a value for a date input with a datetime object" do
300+
element = @session.find(:css, "#change_me_date")
301+
element.set(DateTime.new(2023, 9, 26, 17, 21))
302+
expect(element.value).to eq("2023-09-26")
303+
end
304+
305+
it "sets a value for a month input" do
306+
element = @session.find(:css, "#change_me_month")
307+
element.set("2023-09")
308+
expect(element.value).to eq("2023-09")
309+
end
310+
311+
it "sets a value for a month input with a date object" do
312+
element = @session.find(:css, "#change_me_month")
313+
element.set(Date.new(2023, 9, 26))
314+
expect(element.value).to eq("2023-09")
315+
end
316+
317+
it "sets a value for a month input with a time object" do
318+
element = @session.find(:css, "#change_me_month")
319+
element.set(Time.new(2023, 9, 26, 17, 21))
320+
expect(element.value).to eq("2023-09")
321+
end
322+
323+
it "sets a value for a month input with a datetime object" do
324+
element = @session.find(:css, "#change_me_month")
325+
element.set(DateTime.new(2023, 9, 26, 17, 21))
326+
expect(element.value).to eq("2023-09")
327+
end
328+
329+
330+
it "sets a value for a week input" do
331+
element = @session.find(:css, "#change_me_week")
332+
element.set("2023-W39")
333+
expect(element.value).to eq("2023-W39")
334+
end
335+
336+
it "sets a value for a week input with a date object" do
337+
element = @session.find(:css, "#change_me_week")
338+
element.set(Date.new(2023, 9, 26))
339+
expect(element.value).to eq("2023-W39")
340+
end
341+
342+
it "sets a value for a week input with a time object" do
343+
element = @session.find(:css, "#change_me_week")
344+
element.set(Time.new(2023, 9, 26, 17, 21))
345+
expect(element.value).to eq("2023-W39")
346+
end
347+
348+
it "sets a value for a week input with a datetime object" do
349+
element = @session.find(:css, "#change_me_week")
350+
element.set(DateTime.new(2023, 9, 26, 17, 21))
351+
expect(element.value).to eq("2023-W39")
352+
end
280353
end
281354
end
282355

spec/support/views/time_inputs.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
<body>
1010
<p><input type="time" name="change_me_time" id="change_me_time"></p>
1111
<p><input type="date" name="change_me_date" id="change_me_date"></p>
12+
<p><input type="month" name="change_me_month" id="change_me_month"></p>
13+
<p><input type="week" name="change_me_month" id="change_me_week"></p>
1214
</body>
1315
</html>

0 commit comments

Comments
 (0)