How do I keep PhantomJS from returning the page to Capybara for several seconds to allow AJAX to finish loading? The HTML body I am getting back is:
<!--div class='loading'>PLEASE WAIT WHILE PAGE LOADS</div-->
Using a Capybara method that waits for you, assert that content is on the page that should exist on the page only after AJAX has completed. For example, if the page should say "Page loaded!" only after AJAX has completed, and assuming you're using RSpec, do this:
expect(page).to have_content("Page loaded!")
You can then safely test other things on the page that depend on AJAX having completed.
Capybara methods that wait for specified content to appear include
choose. Some Capybara methods don't wait, including
evaluate_script and node accessors like
first wait if you give them options that give them something specific to wait for (
:between), otherwise they don't. It's quite a minefield. (I took the lists mostly from this blog post.)
There are other ways to wait (
sleep (bad), polling the database until a change caused by AJAX appears, etc.) but using Capybara methods that wait for you is the usual and recommended way.