水豚测试中无法触发鼠标事件

发布于 2024-11-24 06:39:40 字数 119 浏览 1 评论 0原文

我正在使用 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

难如初 2024-12-01 06:39:41

此提交添加了悬停方法。它的优点是在元素上包含 CSS 悬停效果。

用法:

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

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

Usage:

find('#posts .post .comments .comment').hover
谈下烟灰 2024-12-01 06:39:41

我在让它发挥作用时也遇到了很多麻烦。网络上有很多相互矛盾的信息。我是这样做的:

设置: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_linkclick_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 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.

仄言 2024-12-01 06:39:41

对于可见性问题,有时更改窗口大小会有所帮助。我已经做到了
在spec_helper.rb中

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

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 
明月松间行 2024-12-01 06:39:41

以及使用 find('#element').hoverpage.execute_script "$('#element_2').trigger('mouseover');" 之一还必须将 js: true 传递给 describe 块才能打开 javascript(除非您已在配置中这样做)。这让我有点绊倒。

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.

嘦怹 2024-12-01 06:39:40

我选择使用 Capybara webkit,遗憾的是我不得不使用 jQuery 来执行 javascript:

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

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

page.execute_script('$(".ClassSelector").trigger("hover")')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文