add hover event to capybara

Seems capybara has implemented the hover function

check capybara/selenium/node.rb

1
2
3
4
5
6
7
8
9
10
class Capybara::Selenium::Node < Capybara::Driver::Node
...
def hover
driver.browser.action.move_to(native).perform
end
...
end

But it’s only a selenium implementation. Not all the browsers implement hover function,
Capybara has not apply the method.

check capybara/session.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
NODE_METHODS = [
:all, :first, :attach_file, :text, :check, :choose,
:click_link_or_button, :click_button, :click_link, :field_labeled,
:fill_in, :find, :find_button, :find_by_id, :find_field, :find_link,
:has_content?, :has_text?, :has_css?, :has_no_content?, :has_no_text?,
:has_no_css?, :has_no_xpath?, :resolve, :has_xpath?, :select, :uncheck,
:has_link?, :has_no_link?, :has_button?, :has_no_button?, :has_field?,
:has_no_field?, :has_checked_field?, :has_unchecked_field?,
:has_no_table?, :has_table?, :unselect, :has_select?, :has_no_select?,
:has_selector?, :has_no_selector?, :click_on, :has_no_checked_field?,
:has_no_unchecked_field?, :query, :assert_selector, :assert_no_selector
]
SESSION_METHODS = [
:body, :html, :source, :current_url, :current_host, :current_path,
:execute_script, :evaluate_script, :visit,
:within, :within_fieldset, :within_table, :within_frame, :within_window,
:save_page, :save_and_open_page, :save_screenshot,
:reset_session!, :response_headers, :status_code,
:title, :has_title?, :has_no_title?, :current_scope
]
DSL_METHODS = NODE_METHODS + SESSION_METHODS

You have two ways to use the hover function:

  1. convert element to selenium element and call hover

    element.native.hover

  1. Add the snippet below to hook.rb file under the support directory.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # Example:
    #
    # find("span.assign").hover
    #
    module Capybara
    module Node
    class Element
    def hover
    @session.driver.browser.action.move_to(self.native).perform
    end
    end
    end
    end
文章目录
|