diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a8f97c..8f6e6b3 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 8d757c7..f472415 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 3f44300..01aec6a 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 5fe4eca..dae75cc 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 bdad2cc..05c1da0 100644 --- a/spec/support/views/send_keys.erb +++ b/spec/support/views/send_keys.erb @@ -10,6 +10,9 @@