diff --git a/CHANGELOG.md b/CHANGELOG.md index a9b50e8..67d6785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/lib/capybara/cuprite/driver.rb b/lib/capybara/cuprite/driver.rb index eff7689..15f3df9 100644 --- a/lib/capybara/cuprite/driver.rb +++ b/lib/capybara/cuprite/driver.rb @@ -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 @@ -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 diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index dce0c42..256ada8 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -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")