chadoh
chadoh

Reputation: 4432

Capybara: Verify that ajax-loaded content is not present

visit "/whatever"
expect(page).to have_no_content "This is loaded in asynchronously"

This should fail, but passes. Why? When the page first loads, the content isn't there, and Capybara reasonably doesn't wait for it.

What is the blessed way to wait for this content, now that Capybara 2.0 has declared wait_until bad news?

Upvotes: 0

Views: 379

Answers (2)

chadoh
chadoh

Reputation: 4432

visit "/whatever"
wait_for_ajax do
  expect(page).to have_no_content "This is loaded in asynchronously"
end

Then, in spec_helper:

def wait_for_ajax
  page.evaluate_script("jQuery.active") == 0
  yield
end

This will evaluate the script until it is true, then run your assertion.

Upvotes: 1

chadoh
chadoh

Reputation: 4432

visit "/whatever"
wait_for_ajax do
  expect(page).to have_no_content "This is loaded in asynchronously"
end

Then, in spec_helper:

def wait_for_ajax
  sleep(Capybara.default_wait_time)
  yield
end

Note that this waits to the end of Capybara's default_wait_time every time it runs this spec. But I'm not sure how else you'd check for something like that. If your ajax executes quickly, you can compensate for this by setting the default_wait_time to something like 1 second.

Upvotes: 1

Related Questions