Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Driver#send_keys and :focused filter #261

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Added

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

### 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
4 changes: 4 additions & 0 deletions lib/capybara/cuprite/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def close_window(target_id)
targets.delete(target_id) # page.close is async, delete target asap
end

def active_element
evaluate("document.activeElement")
end

def browser_error
evaluate("_cuprite.browserError()")
end
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 @@ -70,6 +71,10 @@ def frame_title
evaluate_script("document.title")
end

def active_element
Node.new(self, browser.active_element)
end

def find_xpath(selector)
find(:xpath, selector)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/features/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,30 @@ 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 "accessed the document.activeElement" do
input = @session.find_field("empty_input")
input.execute_script("this.focus()")

expect(@session.active_element).to eq(input)
end

it "sends keys to the active_element" 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