Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
20 changes: 20 additions & 0 deletions lib/capybara/cuprite/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 3 additions & 6 deletions lib/capybara/cuprite/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
3 changes: 3 additions & 0 deletions spec/support/views/send_keys.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<textarea id="filled_textarea">Description</textarea>
<div id="empty_div" contenteditable="true"></div>
<div id="filled_div" contenteditable="true">Content</div>
<input id="filled_email" type="email" value="user@example">
<input id="checkbox" type="checkbox">
<a id="link" href="#" onclick="this.dataset.clicks = Number(this.dataset.clicks || 0) + 1; return false;">Link</a>

<form id="without_submit_button">
<input type="text">
Expand Down
Loading