How to wait till an element appears after an AJAX call using capybara-webkit?

Prashanth Sams picture Prashanth Sams · Jul 9, 2014 · Viewed 15.6k times · Source

The capybara method, wait_until doesn't seems to work for capybara-webkit. Is there any alternate solution for that, or any Javascript implementations?

Intentionally need some replacement for sleep, e.g. sleep 2.

enter image description here

Answer

Dave Schweisguth picture Dave Schweisguth · Aug 10, 2014

If your AJAX call results in a change to the DOM, Capybara will wait for it if you do

page.should have_selector?("some selector")

It is an intentional Capybara feature that it waits (up to Capybara.default_wait_time) for have_selector? and related methods to be true.

If your AJAX call does not result in a change to the DOM, there's no way to wait for it on the browser side using Capybara. You might be able to detect when the AJAX call is complete in Javascript and somehow communicate that to Capybara, but that would couple your test and implementation rather tightly. A common approach in this case is to wait for the server-side effect of your AJAX call (creation or update or deletion of a model object, sending of an email, etc.) to take place. Since Capybara can't see the server side you have to wait for the server-side change yourself.

In Capybara 1 you can use Capybara's wait_until to wait for the server-side change. wait_until was removed from Capybara 2. I posted an implementation of wait_until in my answer to Why does adding "sleep 1" in an after hook cause this Rspec/Capybara test to pass?