水豚测试中无法触发鼠标事件
我正在使用 Capybara 1.0.0,并且我的页面中有一个链接,当鼠标悬停在该块上时该链接可见。所以我想在测试中触发鼠标悬停,以便我可以单击该隐藏链接。 我用谷歌搜索,但找不到适合我的解决方案。你们能帮我解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
此提交添加了悬停方法。它的优点是在元素上包含 CSS 悬停效果。
用法:
This commit added the hover method. It has the advantage of including css hover effects on elements.
Usage:
我在让它发挥作用时也遇到了很多麻烦。网络上有很多相互矛盾的信息。我是这样做的:
设置: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')"
这将导致之前隐藏的链接出现(借助 jQuery 的悬停功能),然后
have_link
和click_link
就可以访问这些链接。注意:您不想想要编写
page.evaluate_script
,因为这不起作用。编辑:嗯,我刚刚意识到@kelly-sutton 的解决方案与我的相同。所以我可以确认这一点。
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
andclick_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.
对于可见性问题,有时更改窗口大小会有所帮助。我已经做到了
在spec_helper.rb中
For visibility problems sometimes it helps to change window size for poltergeist. I've done it
in spec_helper.rb
以及使用
find('#element').hover
或page.execute_script "$('#element_2').trigger('mouseover');"
之一还必须将js: true
传递给describe
块才能打开 javascript(除非您已在配置中这样做)。这让我有点绊倒。As well as using either
find('#element').hover
orpage.execute_script "$('#element_2').trigger('mouseover');"
one must also passjs: true
to thedescribe
block in order to turn on javascript (unless you've done that in your configuration). This tripped me up for a bit.我选择使用 Capybara webkit,遗憾的是我不得不使用 jQuery 来执行 javascript:
I've chosen to use Capybara webkit, and sadly I had to resort to executing javascript using jQuery:
这个博客有答案:
http://aokolish.me /blog/2012/01/22/testing-hover-events-with-capybara
但这不适用于 selenium 驱动程序:
http://rubydoc.info/github/jnicklas/capybara/master /Capybara/Node/Element#trigger-instance_method
This blog has the answer:
http://aokolish.me/blog/2012/01/22/testing-hover-events-with-capybara
This doesn't work with the selenium driver though:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#trigger-instance_method