2

We have a very large application that we are currently implementing rspec feature tests. It is a rails application with a knockout.js front-end framework. We have a lot of trouble with wait_for_ajax. We constantly have tests fail because of ajax, and our developers sometimes have to put wait_for_ajax in sometimes three or four times.

This can't be the correct way to do this. What is the correct way to wait for ajax calls in rspec tests?

jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36
  • Have you looked at this? http://stackoverflow.com/questions/17982402/how-can-i-use-rspec-to-test-an-ajax-request – Roy J Jun 22 '15 at 22:17

1 Answers1

2

The hard truth is that javascript tests in capybara are painful and slow.

The only way we can determining if an ajax request if the ajax requests are finished is though hacks like this:

module JavascriptTestHelpers
  def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end
end

I would be really happy if someone proves me wrong. Its almost impossible to get a handle on a specific ajax request unless you do a crazy hack like assigning the promises to the global object. In general this seems to be problematic no matter what the language when automating web browsers.

Running tests in parallel can help a bit with the slowness.

Thoughbot has a really good blog post on some of the common gotchas of capybara JS test which can cause "flapping" tests.

I think that for client heavy applications a javascript test suite in Mocha, Jasmine or (shudder) QUnit is a necessary compliment.

max
  • 96,212
  • 14
  • 104
  • 165
  • I encountered the almost the exact same issues when trying to write integration specs in Mocha that used Phantom.js to do integration specs on a PHP app. (Because writing specs in PHP is like getting your wisdom teeth removed with a bazooka). – max Jun 22 '15 at 23:03