Skip to content

Instantly share code, notes, and snippets.

@johnmeehan
Created March 8, 2017 15:39
Show Gist options
  • Save johnmeehan/bc52805d25c90542be647ccf2eb8aac9 to your computer and use it in GitHub Desktop.
Save johnmeehan/bc52805d25c90542be647ccf2eb8aac9 to your computer and use it in GitHub Desktop.
RSpec, Capyabara, Poltergiest, Angular, jQuery wait_for_ajax
# spec/support/wait_for_ajax.rb
# # Angular, Rails, Capybara, wait for ajax
# Useage:
# click_link("Send an ajax request")
# wait_for_ajax
# expect(page).to have_content("expected ajax response message")
module WaitForAjax
def wait_for_ajax(max_wait_time = 30)
Timeout.timeout(max_wait_time) do
while pending_ajax_requests?
sleep 0.1
end
end
end
# using with poltergiest
def pending_ajax_requests?
page.driver.network_traffic.collect(&:response_parts).any?(&:empty?)
end
# Angular
# def pending_ajax_requests?
# page.evaluate_script("angular.element(document.body).injector().get('$http').pendingrequests.length") > 0
# end
# def pending_ajax_requests?
# active == page.evaluate_script('jQuery.active')
# end
end
RSpec.configure do |config|
config.include WaitForAjax
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment