Skip to content

Commit bdf357c

Browse files
Ensure node has focus before setting value (#280)
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 dd6c46d commit bdf357c

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

lib/capybara/cuprite/javascripts/index.js

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

166166
let valueBefore = node.value;
167167

168+
node.focus();
168169
this.trigger(node, "focus");
169170
this.setValue(node, "");
170171

spec/features/driver_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,19 @@ def create_screenshot(file, *args)
15371537
end
15381538
end
15391539

1540+
context "input_fields" do
1541+
before { @session.visit("/cuprite/input_fields") }
1542+
1543+
it "focuses the element when filling in the value" do
1544+
input = @session.find(:css, "#text_field")
1545+
@session.fill_in "text_field", with: "2016-02-14"
1546+
1547+
expect(@session.find(:css, "#text_field").value).to eq("2016-02-14")
1548+
node = @session.driver.evaluate_script("document.activeElement")
1549+
expect(node).to eq input
1550+
end
1551+
end
1552+
15401553
context "evaluate_script" do
15411554
it "can return an element" do
15421555
@session.visit("/cuprite/send_keys")
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)