Skip to content

Commit

Permalink
Ensure node has focus before setting value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pythonandchips committed Oct 29, 2024
1 parent a58b22b commit b390fde
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/capybara/cuprite/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Cuprite {

let valueBefore = node.value;

node.focus();
this.trigger(node, "focus");
this.setValue(node, "");

Expand Down
13 changes: 13 additions & 0 deletions spec/features/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions spec/support/views/input_fields.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
</head>

<body>
<input type="text" id="text_field" name="text_field"/>
</body>
</html>

0 comments on commit b390fde

Please sign in to comment.