Skip to content

Commit c1c2e42

Browse files
authored
Support Node#active_element and :focused filter (#261)
As of [teamcapybara/capybara#2489][] (April 14, 2021), Capybara supports calling `#send_keys` on the page (instead of a particular element). Similarly, there is a global `:focused` filter that compares the node to the [Document.activeElement][]. These are both powered by a [Driver#active_element][] method in drivers that support JavaScript. This commit integrates `Capybara::Cuprite::Driver` to support both. [teamcapybara/capybara#2489]: teamcapybara/capybara#2489 [Driver#active_element]: https://github.com/teamcapybara/capybara/blob/c0cbf4024c1abd48b0c22c2930e7b05af58ab284/lib/capybara/driver/base.rb#L62-L68 [Document.activeElement]: https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement
1 parent 1e573a7 commit c1c2e42

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Added
44

5+
- Support for `Driver#send_keys`, the `:focused` filter, and `Driver#active_element` [#261]
6+
57
### Changed
68
- `@window_size` attribute is moved from Ferrum, viewport size is still inherited [#253]
79
- Compatibility with latest Ferrum. Browser instance is not passed everywhere now [#254]

lib/capybara/cuprite/browser.rb

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ def close_window(target_id)
127127
targets.delete(target_id) # page.close is async, delete target asap
128128
end
129129

130+
def active_element
131+
evaluate("document.activeElement")
132+
end
133+
130134
def browser_error
131135
evaluate("_cuprite.browserError()")
132136
end

lib/capybara/cuprite/driver.rb

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Driver < Capybara::Driver::Base
1515
delegate %i[restart quit status_code timeout timeout= current_url title body
1616
window_handles close_window switch_to_window within_window window_handle
1717
back forward refresh wait_for_reload viewport_size device_pixel_ratio] => :browser
18+
delegate %i[send_keys] => :active_element
1819
alias html body
1920
alias current_window_handle window_handle
2021
alias go_back back
@@ -70,6 +71,10 @@ def frame_title
7071
evaluate_script("document.title")
7172
end
7273

74+
def active_element
75+
Node.new(self, browser.active_element)
76+
end
77+
7378
def find_xpath(selector)
7479
find(:xpath, selector)
7580
end

spec/features/driver_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,30 @@ def create_screenshot(file, *args)
13031303
expect(input.text).to eq("hello")
13041304
end
13051305

1306+
it "supports the :focused filter" do
1307+
@session.find_field("empty_input").execute_script("this.focus()")
1308+
1309+
expect(@session).to have_field("empty_input", focused: true)
1310+
end
1311+
1312+
it "accessed the document.activeElement" do
1313+
input = @session.find_field("empty_input")
1314+
input.execute_script("this.focus()")
1315+
1316+
expect(@session.active_element).to eq(input)
1317+
end
1318+
1319+
it "sends keys to the active_element" do
1320+
@session.find_field("empty_input").execute_script("this.focus()")
1321+
1322+
expect(@session).to have_field("empty_input", focused: true)
1323+
1324+
@session.send_keys(:tab)
1325+
1326+
expect(@session).to have_field("empty_input", focused: false)
1327+
.and(have_field("filled_input", focused: true))
1328+
end
1329+
13061330
it "sends keys to filled contenteditable div" do
13071331
input = @session.find(:css, "#filled_div")
13081332

0 commit comments

Comments
 (0)