最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does Capybara require sleep to work? - Stack Overflow

programmeradmin0浏览0评论

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
...

bees

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.

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
...

bees

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.

Share Improve this question edited Jul 9, 2016 at 4:02 Ethan Chen 6801 gold badge7 silver badges17 bronze badges asked Nov 1, 2013 at 23:28 mmrmmr 14.9k29 gold badges97 silver badges149 bronze badges 13
  • You can try to use webdriver for debugging but I have similar issues in it. – Andrei Botalov Commented Nov 2, 2013 at 8:07
  • I got the same problem with capybara-webkit when I use ajax. I don't know if poltergeist could resolve this, but it worth a try. – basgys Commented Nov 3, 2013 at 23:21
  • Can you explain more what failures you are seeing? I'm not clear what "css elements missing" truly entails. – Shepmaster Commented Nov 4, 2013 at 1:03
  • I'm seeing test failures. Sometimes, the failures are large stack traces where html or css elements are missing and, for some reason, that throws an exception. Most often, though, the tests to find elements or click links or whatever just fail because the element isn't present on the page (yet). The failures go away when enough timing code is added, or if I do something like 'save_and_open_page' – mmr Commented Nov 4, 2013 at 1:26
  • In the 2 documentation links you provide, they don't say sleep is not valid in Capybara. In fact, that would be very strange since it's a Ruby function and not part of the Capybara DSL. – Romain Paulus Commented Nov 5, 2013 at 7:28
 |  Show 8 more ments

2 Answers 2

Reset to default 5

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 bees available.

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

expect(page).to have_content 'Success'

Capybara acmodates 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

A good alternative of wait_until and sleep is using_wait_time, an example of which is shown below.

using_wait_time 5 do
  page.should have_content '<content>'
end

You can also reload the page, after which you can check whatever conditions you have. This works for me at times.

visit current_url
发布评论

评论列表(0)

  1. 暂无评论