Last active
December 20, 2017 15:23
-
-
Save matthieuprat/102ca5c36a142decdbad86a5c7f0578e to your computer and use it in GitHub Desktop.
Wait for AJAX
This file contains hidden or 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
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 |
This file contains hidden or 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
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