From 6146ccb3d0ff5f5bb5d659805a5193eab90d7028 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Wed, 25 Nov 2026 14:30:36 +0300 Subject: [PATCH] fix(node): focus instead of clicking an element send_keys types into send_keys clicked an element that didn't have focus, so a checkbox toggled before a space toggled it back, a link was followed, and the keys went where the click put the caret, the middle of a filled field. It now focuses the element and puts the caret at the end, pressing End for email and number inputs, which have no selection API. --- CHANGELOG.md | 2 ++ lib/capybara/cuprite/javascripts/index.js | 20 +++++++++++++++++++ lib/capybara/cuprite/node.rb | 9 +++------ spec/node_spec.rb | 24 +++++++++++++++++++++++ spec/support/views/send_keys.erb | 3 +++ 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a8f97c3..8f6e6b3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ [rubycdp/ferrum#716] ### Fixed +- `Node#send_keys` focuses an element without focus instead of clicking it, so a checkbox isn't toggled and a link isn't + followed, and the keys go to the end of a filled field instead of where the click put the caret - Setting a text field to a value ending in `\n` no longer fails now and then with `Ferrum::NoExecutionContextError` when the form submits and the page navigates away before the field refocuses - `response_headers` are looked up case-insensitively as with rack_test, `["Content-Type"]` returned `nil` diff --git a/lib/capybara/cuprite/javascripts/index.js b/lib/capybara/cuprite/javascripts/index.js index 8d757c79..f4724158 100644 --- a/lib/capybara/cuprite/javascripts/index.js +++ b/lib/capybara/cuprite/javascripts/index.js @@ -603,6 +603,26 @@ return document.activeElement === node; } + focusForTyping(node) { + if (this.containsSelection(node) || this.hasFocus(node)) return false; + + node.focus(); + + if (node.isContentEditable) { + let range = document.createRange(); + range.selectNodeContents(node); + range.collapse(false); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } else if (node.tagName == "INPUT" && ["email", "number"].includes(node.type)) { + return true; + } else if ((node.tagName == "INPUT" || node.tagName == "TEXTAREA") && node.selectionStart !== null) { + node.setSelectionRange(node.value.length, node.value.length); + } + + return false; + } + containsSelection(node) { let selectedNode = document.getSelection().focusNode; diff --git a/lib/capybara/cuprite/node.rb b/lib/capybara/cuprite/node.rb index 3f44300d..01aec6a6 100644 --- a/lib/capybara/cuprite/node.rb +++ b/lib/capybara/cuprite/node.rb @@ -231,12 +231,9 @@ def send_keys(*keys) return if keys.empty? command do - unless node.evaluate("_cuprite.containsSelection(this) || _cuprite.hasFocus(this)") - before_click("click") - node.click - end - - driver.browser.keyboard.type(keys) + keyboard = driver.browser.keyboard + keyboard.type(:end) if js(:focusForTyping) + keyboard.type(keys) end end diff --git a/spec/node_spec.rb b/spec/node_spec.rb index 5fe4eca7..dae75cc8 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -1205,6 +1205,30 @@ expect(input.text).to eq("Content appended") end + it "sends keys to the end of a filled email input" do + input = @session.find(:css, "#filled_email") + + input.native.send_keys(".com") + + expect(input.value).to eq("user@example.com") + end + + it "presses the keys on a checkbox without clicking it" do + checkbox = @session.find(:css, "#checkbox") + + checkbox.native.send_keys(:space) + + expect(checkbox).to be_checked + end + + it "does not click a link it sends keys to" do + link = @session.find(:css, "#link") + + link.native.send_keys(:shift) + + expect(link["data-clicks"]).to be_nil + end + it "keeps the focus across calls" do input = @session.find(:css, "#empty_div") diff --git a/spec/support/views/send_keys.erb b/spec/support/views/send_keys.erb index bdad2cc1..05c1da03 100644 --- a/spec/support/views/send_keys.erb +++ b/spec/support/views/send_keys.erb @@ -10,6 +10,9 @@
Content
+ + + Link