Unable to trigger mouse event in Capybara test
I've chosen to use Capybara webkit, and sadly I had to resort to executing javascript using jQuery:
page.execute_script('$(".ClassSelector").trigger("hover")')
is there any Alternative method for trigger in capybara?
page.execute_script(some_javascript)
, in particular some_javascript = "$(selector).trigger(event)"
. This will work on all drivers which are js-capable. Note that page.evaluate_script(some_javascript)
could work too, but it's not guaranteed and discouraged (use evaluate_script
only when script returns primitives).
I mean: execute client-side scripts which does what you want (trigger events).
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
What would prevent capybara/selenium from hovering over a visible element?
The problem you're running into is because the '.hoverme' element in your example at https://jsfiddle.net/pwo7zuL2/1/ has a size of 0x0 px (this is also why you couldn't click it). The size is 0x0 because it only contains absolute positioned elements which don't technically count when calculating the auto size of the parent. If instead of attempting to hover over the .hoverme element you hover over the visible absolute positioned child (which actually has size) of the element the hover will work correctly (which is what you are actually doing when you do it manually in your example).
find('#next').hover
Reliable click function in capybara with selenium
No, there is no alternative click function in Capybara with selenium (other than potentially executing JS via execute_script
). It's not likely the click isn't firing, it's more likely that it is firing at the "wrong" location due to animation on the page causing the calculated location of the click to be out of date by the time the click actually occurs. In that case disabling animation during testing can often help. If it isn't a wrong location issue, and you can create an example that exhibits the behavior, report it to either geckodriver or chromedriver and they will usually fix it pretty quickly (as long as you provide an example that replicates it).
Related Topics
What's The Impact of Eager_Load=True
More Efficient Ruby Way to Map Attribute in Array of Objects to Another Array
Update Rails to a Specific Version
Are There "Rules" for Ruby Syntactic Sugar
Ruby Group Hashes by Value of Key
How to Parse a HTML Table with Nokogiri
Ruby on Rails Active Record Return Value When Create Fails
How to Merge Two Hashes That Have Same Keys in Ruby
How Do Get a Random Datetime Rounded to Beginning of Hour in Rails
Instance Variables Inheritance
Undefined Method 'Reset' for Rdoc::Toplevel:Class When Installing a New Ruby Gem
Rspec Load Time Incredible Long on Os X
Ruby String Strip Defined Characters
How to Replicate Class_Inheritable_Accessor's Behavior in Rails 3.1