Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support time inputs #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/capybara/cuprite/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def set(value, options = {})
command(:select_file, files)
when "color"
node.evaluate("this.setAttribute('value', '#{value}')")
when "time"
value = value.strftime("%H:%M") if value.is_a?(Time)
node.evaluate("this.setAttribute('value', '#{value}')")
Comment on lines +106 to +108
Copy link

@Slotos Slotos Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest changing lib/capybara/cuprite/javascripts/index.js to:

    } else if (node.type == "time") {
      this.setValue(node, value);
      this.input(node);
Suggested change
when "time"
value = value.strftime("%H:%M") if value.is_a?(Time)
node.evaluate("this.setAttribute('value', '#{value}')")
when "time"
value = value.strftime("%H:%M") if value.is_a?(Time)
command(:set, value.to_s)

This way, input event will be emitted correctly and value attribute won't be modified unnecessarily.

else
command(:set, value.to_s)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/features/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,30 @@
element.set("#ddeeff")
expect(element.value).to eq("#ddeeff")
end

it "sets a value for a time input" do
element = @session.find(:css, "#change_me_time")
element.set("17:21")
expect(element.value).to eq("17:21")
end

it "sets a value for a time input with a time object" do
element = @session.find(:css, "#change_me_time")
element.set(Time.new(2023, 9, 26, 17, 21))
expect(element.value).to eq("17:21")
end

it "sets a value for a date input" do
element = @session.find(:css, "#change_me_date")
element.set("2023-09-26")
expect(element.value).to eq("2023-09-26")
end

it "sets a value for a date input with a date object" do
element = @session.find(:css, "#change_me_date")
element.set(Date.new(2023, 9, 26))
expect(element.value).to eq("2023-09-26")
end
end

describe "Node#visible" do
Expand Down
2 changes: 2 additions & 0 deletions spec/support/views/with_js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
</p>
<p><input type="file" name="change_me_file" id="change_me_file"></p>
<p><input type="color" name="change_me_color" id="change_me_color"></p>
<p><input type="time" name="change_me_time" id="change_me_time"></p>
<p><input type="date" name="change_me_date" id="change_me_date"></p>
<p id="changes"></p>
<p id="changes_on_input"></p>
<p id="changes_on_keydown"></p>
Expand Down
Loading