How to emulate mouse hover with Capybara How to emulate mouse hover with Capybara ruby-on-rails ruby-on-rails

How to emulate mouse hover with Capybara


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


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 = :webkitendpage.find('#element').trigger(:mouseover)


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  endend