Skip to content

Instantly share code, notes, and snippets.

@WMahoney09
Last active February 13, 2017 18:01
Show Gist options
  • Save WMahoney09/168f4c865f235927d3d7d65e4961ada1 to your computer and use it in GitHub Desktop.
Save WMahoney09/168f4c865f235927d3d7d65e4961ada1 to your computer and use it in GitHub Desktop.
handle ASYNC calls to API and subsequent Angular Digest cycle
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