Skip to content

Commit 41dbd80

Browse files
asilanoroute
andauthored
Fire input and change event on set for color input (#230)
* Fire input and change event on set for color input Since a color input has to have its value set directly via javascript (presumably because of the difficulty of interacting with the browser's colour picker), the `change` and `input` events don't automatically happen. This PR fires them manually. Fixes #229 * chore: add changelog entry for color input events fix --------- Co-authored-by: Dmitry Vorotilin <d.vorotilin@gmail.com>
1 parent bf5d1c5 commit 41dbd80

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Raise `ObsoleteNode` instead of silently acting on a node that got disconnected from the DOM between being found and being used [#239]
1313
- `Capybara::Cuprite::Node#send_keys` ignores empty or nil keys instead of raising, matching the Selenium and rack_test drivers [#313]
1414
- `switch_to_window` sends `Target.activateTarget` so Chrome reactivates the window's renderer immediately instead of leaving it backgrounded, which could stall the next input command for several seconds [#321]
15+
- `Node#set` fires `input` and `change` events for a `color` input, since its value can only be set via JavaScript and never gets them naturally [#229]
1516

1617
### Removed
1718

lib/capybara/cuprite/node.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Met
107107
command(:select_file, files)
108108
when "color"
109109
node.evaluate("this.setAttribute('value', '#{value}')")
110+
node.evaluate("this.dispatchEvent(new InputEvent('input'))")
111+
node.evaluate("this.dispatchEvent(new Event('change', { bubbles: true }))")
110112
when "date"
111113
value = value.to_date.iso8601 if !value.is_a?(String) && value.respond_to?(:to_date)
112114
command(:set, value.to_s)

spec/features/session_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,18 @@
270270
element.set("#ddeeff")
271271
expect(element.value).to eq("#ddeeff")
272272
end
273+
274+
it "fires the change event for a color input" do
275+
element = @session.find(:css, "#change_me_color")
276+
element.set("#ddeeff")
277+
expect(@session.find(:css, "#changes").text).to eq("#ddeeff")
278+
end
279+
280+
it "fires the input event for a color input" do
281+
element = @session.find(:css, "#change_me_color")
282+
element.set("#ddeeff")
283+
expect(@session.find(:css, "#changes_on_input").text).to eq("#ddeeff")
284+
end
273285
end
274286

275287
# The time inputs are loading SVG icons as data: urls.

spec/support/public/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ $(function() {
3434
$("#changes_on_blur").text("Blur")
3535
})
3636

37+
$("#change_me_color")
38+
.change(function(event) {
39+
$("#changes").text($(this).val())
40+
})
41+
.bind("input", function(event) {
42+
$("#changes_on_input").text($(this).val())
43+
})
44+
3745
$("#browser")
3846
.change(function(event) {
3947
$("#changes").text($(this).val())

0 commit comments

Comments
 (0)