Skip to content

Commit 88456ed

Browse files
authored
Extend Node#set with additional input types (#295)
1 parent cc3a3da commit 88456ed

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

lib/capybara/cuprite/node.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def value
8989
command(:value)
9090
end
9191

92-
def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
92+
def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
9393
warn "Options passed to Node#set but Cuprite doesn't currently support any - ignoring" unless options.empty?
9494

9595
if tag_name == "input"
@@ -115,6 +115,9 @@ def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Met
115115
when "week"
116116
value = value.to_date.strftime("%G-W%V") if !value.is_a?(String) && value.respond_to?(:to_date)
117117
command(:set, value.to_s)
118+
when "datetime-local"
119+
value = value.to_time.strftime("%Y-%m-%dT%H:%M") if !value.is_a?(String) && value.respond_to?(:to_time)
120+
command(:set, value.to_s)
118121
else
119122
command(:set, value.to_s)
120123
end

spec/features/driver_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,30 @@ def create_screenshot(file, *args)
14821482
expect(input.text).to eq("replacement text")
14831483
end
14841484

1485+
it "sets a date field" do
1486+
input = @session.find(:css, "#date-field")
1487+
input.set(Time.new(2000, 12, 31))
1488+
expect(input.value).to eq("2000-12-31")
1489+
end
1490+
1491+
it "sets a datetime-local field" do
1492+
input = @session.find(:css, "#datetime-local-field")
1493+
input.set(Time.new(2000, 12, 31, 17, 15))
1494+
expect(input.value).to eq(Time.new(2000, 12, 31, 17, 15).strftime("%Y-%m-%dT%H:%M"))
1495+
end
1496+
1497+
it "sets a range field" do
1498+
input = @session.find(:css, "#range-field")
1499+
input.set(42)
1500+
expect(input.value).to eq("42")
1501+
end
1502+
1503+
it "sets a color field" do
1504+
input = @session.find(:css, "#color-field")
1505+
input.set("#1270a4")
1506+
expect(input.value).to eq("#1270a4")
1507+
end
1508+
14851509
it "sets a content editable childs content" do
14861510
@session.visit("/with_js")
14871511
@session.find(:css, "#existing_content_editable_child").set("WYSIWYG")

spec/support/views/set.erb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
<!DOCTYPE html>
12
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
23
<head>
3-
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
4+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
5+
<title>Set tests</title>
46
</head>
57

68
<body>
79
<div id="empty_div" contenteditable="true"></div>
810
<div id="filled_div" contenteditable="true">Content</div>
11+
<form>
12+
<input id="date-field" type="date" name="date-field"><br>
13+
<input id="datetime-local-field" type="datetime-local" name="datetime-local-field"><br>
14+
<input id="range-field" type="range" min="0" max="100" name="range-field"><br>
15+
<input id="color-field" type="color" name="color-field">
16+
</form>
917
</body>
1018
</html>

0 commit comments

Comments
 (0)