kxhitiz
kxhitiz

Reputation: 1949

Unable to trigger mouse event in Capybara test

I am using Capybara 1.0.0, and I have a link in my page which gets visible when mouse hover over that block. So I want to trigger mouse over in test so that I can click that hidden link. I googled it, but couldn't find the solution that suits me. Can you guys help me with this?

Upvotes: 22

Views: 18676

Answers (6)

Jeremy Eaton
Jeremy Eaton

Reputation: 148

As well as using either find('#element').hover or page.execute_script "$('#element_2').trigger('mouseover');" one must also pass js: true to the describe block in order to turn on javascript (unless you've done that in your configuration). This tripped me up for a bit.

Upvotes: 0

Artur79
Artur79

Reputation: 13647

For visibility problems sometimes it helps to change window size for poltergeist. I've done it in spec_helper.rb

  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app, window_size: [1280, 600])
  end 

Upvotes: 0

Harm de Wit
Harm de Wit

Reputation: 2210

This commit added the hover method. It has the advantage of including css hover effects on elements.

Usage:

find('#posts .post .comments .comment').hover

Upvotes: 10

bovender
bovender

Reputation: 1960

I had a lot of trouble getting this to work as well. There's a lot of conflicting information on the web. Here's how I did it:

Setting: rails 3.2.1, capybara 1.1.2, rspec 2.8.0, selenium-webdriver 2.20.0

page.execute_script "$('tr.user-#{user.id}').trigger('mouseover')"

This will cause the previously hidden links to appear (by virtue of jQuery's hover function), which are then accessible for have_link and click_link.

Note: you do not want to write page.evaluate_script as this won't work.


Edit: Well I just realized that @kelly-sutton's solution is the same as mine. So I can confirm this.

Upvotes: 1

Red
Red

Reputation: 139

This blog has the answer:

http://aokolish.me/blog/2012/01/22/testing-hover-events-with-capybara

page.find('#element').trigger(:mouseover)

This doesn't work with the selenium driver though:

http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#trigger-instance_method

Upvotes: 13

Kelly Sutton
Kelly Sutton

Reputation: 591

I've chosen to use Capybara webkit, and sadly I had to resort to executing javascript using jQuery:

page.execute_script('$(".ClassSelector").trigger("hover")')

Upvotes: 14

Related Questions