From d0b9878aa0bb9fe543070d8543b41c996824c94b Mon Sep 17 00:00:00 2001 From: Chris Howlett Date: Mon, 17 Apr 2023 17:02:07 +0100 Subject: [PATCH 1/2] 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 rubycdp/cuprite#229 --- lib/capybara/cuprite/node.rb | 2 ++ spec/features/session_spec.rb | 12 ++++++++++++ spec/support/public/test.js | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index 849bb2a5..d14144d8 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -107,6 +107,8 @@ def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Met command(:select_file, files) when "color" node.evaluate("this.setAttribute('value', '#{value}')") + node.evaluate("this.dispatchEvent(new InputEvent('input'))") + node.evaluate("this.dispatchEvent(new Event('change', { bubbles: true }))") when "date" value = value.to_date.iso8601 if !value.is_a?(String) && value.respond_to?(:to_date) command(:set, value.to_s) diff --git a/spec/features/session_spec.rb b/spec/features/session_spec.rb index 02a39f46..d21f5c34 100644 --- a/spec/features/session_spec.rb +++ b/spec/features/session_spec.rb @@ -270,6 +270,18 @@ element.set("#ddeeff") expect(element.value).to eq("#ddeeff") end + + it "fires the change event for a color input" do + element = @session.find(:css, "#change_me_color") + element.set("#ddeeff") + expect(@session.find(:css, "#changes").text).to eq("#ddeeff") + end + + it "fires the input event for a color input" do + element = @session.find(:css, "#change_me_color") + element.set("#ddeeff") + expect(@session.find(:css, "#changes_on_input").text).to eq("#ddeeff") + end end # The time inputs are loading SVG icons as data: urls. diff --git a/spec/support/public/test.js b/spec/support/public/test.js index 7b11808e..6dc31950 100644 --- a/spec/support/public/test.js +++ b/spec/support/public/test.js @@ -34,6 +34,14 @@ $(function() { $("#changes_on_blur").text("Blur") }) + $("#change_me_color") + .change(function(event) { + $("#changes").text($(this).val()) + }) + .bind("input", function(event) { + $("#changes_on_input").text($(this).val()) + }) + $("#browser") .change(function(event) { $("#changes").text($(this).val()) From 42be0b71220963d3a230be9d335cfededddde839 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Mon, 24 Aug 2026 18:05:33 +0300 Subject: [PATCH 2/2] chore: add changelog entry for color input events fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e60828a8..8d3fe5e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Raise `ObsoleteNode` instead of silently acting on a node that got disconnected from the DOM between being found and being used [#239] - `Capybara::Cuprite::Node#send_keys` ignores empty or nil keys instead of raising, matching the Selenium and rack_test drivers [#313] - `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] +- `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] ### Removed