Skip to content

Commit

Permalink
Support Node#active_element and :focused filter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
seanpdoyle committed Feb 29, 2024
1 parent 1e573a7 commit 405517d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### Added

- Support for `Driver#active_element`, `Driver#send_keys`, and the `:focused`
filter

### Changed
- `@window_size` attribute is moved from Ferrum, viewport size is still inherited [#253]
- Compatibility with latest Ferrum. Browser instance is not passed everywhere now [#254]
Expand Down
5 changes: 5 additions & 0 deletions lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Driver < Capybara::Driver::Base
delegate %i[restart quit status_code timeout timeout= current_url title body
window_handles close_window switch_to_window within_window window_handle
back forward refresh wait_for_reload viewport_size device_pixel_ratio] => :browser
delegate %i[send_keys] => :active_element
alias html body
alias current_window_handle window_handle
alias go_back back
Expand Down Expand Up @@ -82,6 +83,10 @@ def find(method, selector)
browser.find(method, selector).map { |native| Node.new(self, native) }
end

def active_element
evaluate_script("document.activeElement")
end

def click(x, y)
browser.mouse.click(x: x, y: y)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/features/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,23 @@ def create_screenshot(file, *args)
expect(input.text).to eq("hello")
end

it "supports the :focused filter" do
@session.find_field("empty_input").execute_script("this.focus()")

expect(@session).to have_field("empty_input", focused: true)
end

it "sends keys to the document.activeElement" do
@session.find_field("empty_input").execute_script("this.focus()")

expect(@session).to have_field("empty_input", focused: true)

@session.send_keys(:tab)

expect(@session).to have_field("empty_input", focused: false)
.and(have_field("filled_input", focused: true))
end

it "sends keys to filled contenteditable div" do
input = @session.find(:css, "#filled_div")

Expand Down

0 comments on commit 405517d

Please sign in to comment.