How to emulate mouse hover with Capybara

前端 未结 4 1181
北荒
北荒 2020-12-13 06:27

Basically, what I\'m trying to do is click on a button that becomes visible when hovering another element (its parent).

I have tried to use trigger.(\'mouseove

相关标签:
4条回答
  • 2020-12-13 06:44

    Alex described the solution of such problems in his blog: check it out http://aokolish.me/blog/2012/01/22/testing-hover-events-with-capybara

    RSpec.configure do |config|
      # ...
      Capybara.javascript_driver = :webkit
    end
    
    page.find('#element').trigger(:mouseover)
    
    0 讨论(0)
  • 2020-12-13 07:06

    Capybara provides Element#hover method from version 2.1:

    find('.some_class').hover
    

    This method is implemented in Capybara::Selenium::Driver in almost the same way as in @AlexD's answer.

    Note that to use #hover in Selenium it's usually better to turn native events on:

    Capybara.register_driver :selenium do |app|
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile.native_events = true
      Capybara::Selenium::Driver.new(app, :browser => :firefox, profile: profile)
    end
    
    0 讨论(0)
  • 2020-12-13 07:08

    I found a way to simulate "mouse hover" using Capybara + the Selenium driver:

    module Capybara
      module Node
        class Element
          def hover
            @session.driver.browser.action.move_to(self.native).perform
          end
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-13 07:08

    Using Capybara + Selenium it is possible to use "hover" with this command:

    page.driver.browser.action.move_to(page.find('YourElement').native).perform
    
    0 讨论(0)
提交回复
热议问题