Created
March 3, 2016 18:51
-
-
Save mreinsch/133e7555416399ebd2f0 to your computer and use it in GitHub Desktop.
wait_for_ajax helper for capyabara
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
based on https://coderwall.com/p/aklybw/wait-for-ajax-with-capybara-2-0