Skip to content

Instantly share code, notes, and snippets.

@matthieuprat
Last active December 20, 2017 15:23
Show Gist options
  • Save matthieuprat/102ca5c36a142decdbad86a5c7f0578e to your computer and use it in GitHub Desktop.
Save matthieuprat/102ca5c36a142decdbad86a5c7f0578e to your computer and use it in GitHub Desktop.
Wait for AJAX
def wait_for_ajax(timeout: Capybara.default_max_wait_time)
Timeout.timeout(timeout) do # Bad.
loop until finished_all_ajax_requests?
end
end
def finished_all_ajax_requests?
!page.evaluate_script '(window.jQuery || {}).active'
end
def wait_for_ajax(timeout: Capybara.default_max_wait_time)
max_time = Capybara::Helpers.monotonic_time + timeout
while Capybara::Helpers.monotonic_time < max_time
return if finished_all_ajax_requests?
sleep 0.1
end
raise 'AJAX timeout exceeded'
end
def finished_all_ajax_requests?
!page.evaluate_script '(window.jQuery || {}).active'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment