Angular, Rails, Capybara, wait for ajax.
When dealing with ajax integrations tests using Capybara and Rails, most of the time we don’t know how long it will take to our backend to respond to that request. In this cases, Capybara is very good about handling ajax, waiting for elements to appear on the page,
Capybara is smart enough to retry finding the content for a brief period of time before giving up and throwing an error. This period of time is defined by
So, to solve your problem you can adjust this period, by 5, by 6, by 7. Only when it reaches the default wait time, Capybara stops finding the content and your test fails. Other approach is setting a sleep, guessing how long it takes your request.
Wait for ajax!!!
In order to stop gueesing, there is a also a good solution in CODERWALL when you use JQuery for your ajax request.
But what happens if like me, you are using AngularJS in your frontend app, making the requests with the $http service. In this case you have to find a way to figure out if there are pending request with AngularJS. Here is a solution:
Usage
Just add this method in the place you want to make sure an ajax request has finished.
Poltergeist solution
In case you are using Poltergeist as your Capybara driver, you can inspect network traffic, helping know if there are pending response parts:
You can use this approach regardless of your JS frontend framework.