From b390fde8b119e78fa9958f3798a298d6980740db Mon Sep 17 00:00:00 2001 From: Colin Gemmell Date: Tue, 29 Oct 2024 14:16:11 +0000 Subject: [PATCH] Ensure node has focus before setting value Currently the javascript used to set a field value will trigger a "focus" event prior to updating a field however this does not set the input as focused so any use of `document.activeElement` will return the body element. To fix this a call to `node.focus()` has been added before the focus event to ensure `document.activeElement` will respond correctly. --- lib/capybara/cuprite/javascripts/index.js | 1 + spec/features/driver_spec.rb | 13 +++++++++++++ spec/support/views/input_fields.erb | 9 +++++++++ 3 files changed, 23 insertions(+) create mode 100644 spec/support/views/input_fields.erb diff --git a/lib/capybara/cuprite/javascripts/index.js b/lib/capybara/cuprite/javascripts/index.js index 81bc8db..edbb499 100644 --- a/lib/capybara/cuprite/javascripts/index.js +++ b/lib/capybara/cuprite/javascripts/index.js @@ -124,6 +124,7 @@ class Cuprite { let valueBefore = node.value; + node.focus(); this.trigger(node, "focus"); this.setValue(node, ""); diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index a4c6685..93da041 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -1534,6 +1534,19 @@ def create_screenshot(file, *args) end end + context "input_fields" do + before { @session.visit("/cuprite/input_fields") } + + it "focuses the element when filling in the value" do + input = @session.find(:css, "#text_field") + @session.fill_in "text_field", with: "2016-02-14" + + expect(@session.find(:css, "#text_field").value).to eq("2016-02-14") + node = @session.driver.evaluate_script("document.activeElement") + expect(node).to eq input + end + end + context "evaluate_script" do it "can return an element" do @session.visit("/cuprite/send_keys") diff --git a/spec/support/views/input_fields.erb b/spec/support/views/input_fields.erb new file mode 100644 index 0000000..0836725 --- /dev/null +++ b/spec/support/views/input_fields.erb @@ -0,0 +1,9 @@ + + + + + + + + +