Capybara需要睡觉吗? [英] Does Capybara require sleep to work?

查看:176
本文介绍了Capybara需要睡觉吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然, sleep wait_until 使用最新版本的Capybara无效,根据网页 更新



但是,我有一套测试如果我添加一个 sleep(1)调用测试,那只适用于快速机器。也就是说,测试看起来像:

 描述剂量计页面做
之前做
click_link (剂量计读取历史,:匹配=>:第一个)
结束
...

变为

 描述剂量计页面执行
之前执行
unix_wait
click_link(剂量计读取历史记录,:匹配=>:第一个)
结束
...

我已将 unix_wait 定义为:

  def unix_wait 
case RbConfig :: CONFIG ['host_os']
当/ darwin /
当/ linux-gnu /
sleep(1)
结束
结束

问题是,我有一台旧的Ubuntu 12.04四核笔记本电脑运行这些测试在Jenkins上,如果没有 unix_wait 调用,一切运行良好。在运行Ubuntu 13.10和macbook pro笔记本电脑的hexacore桌面上随机测试失败,但是如果我添加了 unix_wait 调用,则测试通过。



测试失败本身表示加载失败(即某些运行中缺少css元素,但其他运行中没有),并且当手动加载站点时,被测试的内容实际上有效。 / p>

那么这里适当的行动是什么?显然,测试期间不允许 sleep wait_until 也不允许。然而,睡眠是有效的,但它对我来说似乎非常粗糙。我应该查看 #synchronized 吗?根据我从那些博客文章中收集到的信息,当我打电话给 click_link 时,这已经被调用,并且测试仍然失败。



这里接受的协议是什么?



我应该补充一下,因为我认为这很重要:这些都是javascript测试。我正在使用基于qt4(而不是qt5)的capybara-webkit。我正在考虑切换到poltergeist或其他一些javascript驱动程序作为调试步骤。

解决方案

如果你还没有这样做,在您的测试断言中,如果您检查页面上的内容,它将等待一段时间,直到该内容可用。



所以,而不是添加睡眠你可以添加类似

  expect(page).to have_content'Success'

Capybara适应Ajax和元素加载等因此在检查内容时会隐式等待。



如果您需要允许加载您知道可能需要更长时间的元素,即第三方参与查询/登录,您可以更改默认等待时间

  Capybara.default_wait_time = 5 


Apparently, sleep or wait_until are not valid using recent versions of Capybara, according to the webpage updates.

However, I have a set of tests that only work on fast machines if I add a sleep(1) call to the test. That is, a test that looks like:

describe "dosimeters page" do
  before do
    click_link("Dosimeter Read History", :match=>:first)
  end
...

becomes

describe "dosimeters page" do
  before do
    unix_wait
    click_link("Dosimeter Read History", :match=>:first)
  end
...

where I've defined unix_wait as:

def unix_wait
  case RbConfig::CONFIG['host_os']
  when /darwin/
  when /linux-gnu/
    sleep(1)
  end
end

The thing is, I have an old Ubuntu 12.04 quadcore laptop running these tests on Jenkins, and everything works well on it without the unix_wait calls. The tests failed randomly on a hexacore desktop running Ubuntu 13.10 and on a macbook pro laptop, but if I add in the unix_wait call, then the tests pass.

The test failures themselves are indicative of loading failures (ie, css elements missing on some runs, but not on others), and the things being tested actually work when the site is loaded manually.

So what's the appropriate action here? Apparently, sleep isn't allowed during testing, nor is wait_until. However, sleep is working, but it does seem extremely crude to me. Should I be looking at #synchronized? From what I gather from those blog posts, that's already getting called when I call click_link, and the tests are still failing.

What is the accepted protocol here?

I should add, because I think it's important: These are all javascript tests. I'm using capybara-webkit built on qt4 (not qt5). I'm considering switching to poltergeist or some other javascript driver as a debug step.

解决方案

In case your not doing this already, in your test assertion if you check for the content on the page it will wait for a set amount of time until that content becomes available.

So, instead of adding a sleep you can add something like

expect(page).to have_content 'Success'

Capybara accommodates Ajax and loading of elements etc. so it will wait implicitly when checking content.

You can alter the default wait time if you need to allow for loading of elements which you know may take longer i.e. 3rd partying queries/logins

Capybara.default_wait_time = 5

这篇关于Capybara需要睡觉吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆