Last active
February 13, 2017 18:01
-
-
Save WMahoney09/168f4c865f235927d3d7d65e4961ada1 to your computer and use it in GitHub Desktop.
handle ASYNC calls to API and subsequent Angular Digest cycle
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
def page_ready | |
if (execute_script("return document.readyState") != 'complete') || (execute_script("return window.jQuery.active") != 0) | |
return false # Not Ready: Document not loaded or active jQuery on page | |
end | |
phase = execute_script("return window.angular.element(document.body).injector().get('$rootScope').$$phase") | |
pending = execute_script("return window.angular.element(document.body).injector().get('$http').pendingRequests.length") | |
if (phase === '$apply' || phase === '$digest' || pending != 0) | |
return false # Not Ready: Angular digesting or loading data | |
end | |
return true # Ready | |
rescue | |
false | |
end | |
def digest_helper(seconds=30) | |
timeout = seconds | |
end_time = Time.now + timeout | |
until Time.now > end_time | |
result = page_ready | |
return result if result # ASYNC and Digest finished | |
end | |
raise(StandardError, "Digest Helper: Page was not ready after #{timeout} seconds") # ASYNC and Digest took longer than time allowed | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment