Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 219
Prashanth Sams Avatar asked Jul 09 '14 09:07

Prashanth Sams


1 Answers

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?

like image 161
Dave Schweisguth Avatar answered Nov 07 '22 13:11

Dave Schweisguth