Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara does not seem to be waiting (as it should do!?) (Async ajax event)

Tags:

rspec

capybara

We have a background process that we poll and reload a chart when the processing (from another server, UNIX process) is completed.

I cannot get Capybrara to properly wait (despite the general consensus that it should)

Its an AJAX event that polls the DB and then refreshes the page content when its completed. The timing, even for testing, is varied - a sleep n.seconds sometimes works, but not always.

  • I have tried expect(page.has_content), assert page.has_text, find, and has_content. None seem to actually wait.
  • I have timeout set to 90 seconds. Generally the background process is more than 20 seconds.
  • I have tried different default_wait_time

I have found lots of articles claiming that it should wait within its default polling / time-out. Is there something we should be checking? Note we are headless via Poltergeist.

There is no CSS object that changes on the display, its just a string (this is taken from a puts page.html to debug the tests)

From this

            <div class="bill-summary-strap-line">
              Calculating your bill costs
              <br/>
            </div>

To this

<div class="bill-summary-strap-line">
                 Calculating finished
              <br/>
            </div>

What are we missing?

RSpec code below, really hope someone has some guidance having solved this type of problem. Cheers Ben

       puts page.html  # test
       #assert page.has_text?('Calculating your bill costs')
       expect(page).to have_content 'Calculating your bill costs'
       page.driver.render("#{LOGDIR}/output1.png", :full => true)
       sleep 30.seconds  # would like to remove this
       puts page.html # test output
       #page.should have_content("Calculating finished")
       #expect(page.has_content? "Calculating finished").to be_true
       assert page.has_text?("Calculating finished") 
       page.driver.render("#{LOGDIR}/output2.png", :full => true)
like image 883
Ben Avatar asked Nov 02 '13 08:11

Ben


1 Answers

In your spec_helper, you have the following code:

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, {debug: false, :default_wait_time => 30, :timeout => 90})
end

My guess is that this timeout is basically ignored, as Capybara itself will set the timeout based on Capybara.default_wait_time. This would explain why your time was set to 2 inside the spec.

The poltergeist docs suggest just using this in your spec_helper:

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

You shouldn't need to register or reregister the driver.

like image 54
Shepmaster Avatar answered Sep 25 '22 03:09

Shepmaster