Does a capybara require work from sleep?

It seems sleep or wait_until not valid using the latest versions of Capybara, according to the updates web page .

However, I have a test suite that only works on fast machines if I add a sleep(1) call to the test. That is, it looks like this:

 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 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 that tests Jenkins, and everything works on it without unix_wait calls. Tests were not randomly run on the hexacore desktop running Ubuntu 13.10 and on the macbook pro laptop, but if I add unix_wait to the call, the tests will pass.

The tests themselves indicate download failures (i.e. css elements that are absent on some runs, but not on others), and checked things actually work when the site loads manually.

So what is the action here? Apparently, sleep not allowed during testing, nor is wait_until . However, the dream works, but for me it seems very rude. Should I look at #synchronized ? From what I am compiling from those blog posts that are already being called when I call click_link , and the tests still fail.

What is the accepted protocol here?

I have to add because I think this is important: these are all javascript tests. I am using capybara-webkit built on qt4 (not qt5). I am considering switching to poltergeist or some other javascript driver as a debugging step.

+8
javascript ruby-on-rails sleep capybara capybara-webkit
source share
2 answers

If you don’t do this already, in your test statement, if you check the content on the page, it will wait for the set amount of time until that content becomes available.

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

 expect(page).to have_content 'Success' 

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

You can change the default timeout if you need to allow the loading of items that, as you know, may take longer, i.e. third-party requests / logins

 Capybara.default_wait_time = 5 
+4
source share

A good alternative to 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 all the conditions that you have. It works for me from time to time.

 visit current_url 
0
source share

All Articles