Skip to content

Instantly share code, notes, and snippets.

@mreinsch
Created March 3, 2016 18:51
Show Gist options
  • Save mreinsch/133e7555416399ebd2f0 to your computer and use it in GitHub Desktop.
Save mreinsch/133e7555416399ebd2f0 to your computer and use it in GitHub Desktop.
wait_for_ajax helper for capyabara
module CapybaraHelperMethods
def wait_for_ajax
counter = 0
while !finished_all_ajax_requests?
counter += 1
sleep 0.2
raise "AJAX request took too long." if counter >= (Capybara.default_wait_time * 5)
end
end
def finished_all_ajax_requests?
active = page.evaluate_script <<-SCRIPT.strip.gsub(/\s+/,' ')
(function () {
if (typeof jQuery != 'undefined') {
return jQuery.active;
}
else {
console.error("jQuery was undefined on " + document.URL + " - will retry...");
return -1;
}
})()
SCRIPT
active && active.zero?
end
end
RSpec.configuration.include CapybaraHelperMethods, :type => :feature
@mreinsch
Copy link
Author

mreinsch commented Mar 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment