Skip to content

Commit b390fde

Browse files
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.
1 parent a58b22b commit b390fde

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/capybara/cuprite/javascripts/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class Cuprite {
124124

125125
let valueBefore = node.value;
126126

127+
node.focus();
127128
this.trigger(node, "focus");
128129
this.setValue(node, "");
129130

spec/features/driver_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,19 @@ def create_screenshot(file, *args)
15341534
end
15351535
end
15361536

1537+
context "input_fields" do
1538+
before { @session.visit("/cuprite/input_fields") }
1539+
1540+
it "focuses the element when filling in the value" do
1541+
input = @session.find(:css, "#text_field")
1542+
@session.fill_in "text_field", with: "2016-02-14"
1543+
1544+
expect(@session.find(:css, "#text_field").value).to eq("2016-02-14")
1545+
node = @session.driver.evaluate_script("document.activeElement")
1546+
expect(node).to eq input
1547+
end
1548+
end
1549+
15371550
context "evaluate_script" do
15381551
it "can return an element" do
15391552
@session.visit("/cuprite/send_keys")

spec/support/views/input_fields.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
2+
<head>
3+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
4+
</head>
5+
6+
<body>
7+
<input type="text" id="text_field" name="text_field"/>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)