1 check per line format:
return {
jquery: (typeof jQuery === "undefined") ? true : jQuery.active < 1,
ajax: (typeof Ajax === "undefined") ? true : Ajax.activeRequestCount < 1,
document: document.readyState == "complete",
angular: (typeof angular === "undefined") ? true : angular.element(document).injector().get("$http").pendingRequests.length < 1,
spinner: (document.getElementById("turbolinks-progress") === null) ? true : document.getElementById("turbolinks-progress").style["display"] == "none"
}
pep8-friendly format:
function jqueryInactive() {
return (typeof jQuery === "undefined") ? true : jQuery.active < 1
}
function ajaxInactive() {
return (typeof Ajax === "undefined") ? true :
Ajax.activeRequestCount < 1
}
function angularNoRequests() {
return (typeof angular === "undefined") ? true :
angular.element(document).injector().get(
"$http").pendingRequests.length < 1
}
function spinnerInvisible() {
spinner = document.getElementById("turbolinks-progress")
return (spinner === null) ? true : spinner.style["display"] == "none"
}
return {
jquery: jqueryInactive(),
ajax: ajaxInactive(),
angular: angularNoRequests(),
spinner: spinnerInvisible(),
document: document.readyState == "complete",
}
Result of executing such script like that:
self.browser.execute_script(script_var, silent=True)
is a dict where key is a check name (jquery
, ajax
etc) and the value is bool value whether that particular check suceeded and it's safe to perform actions. Example output:
{u'jquery': True, u'spinner': False, u'ajax': True, u'document': True, u'angular': True}
So now it's just a matter of using python's all()
:
all(script_result.values())
and wrapping it with some explicit waiter to ensure the page is fully loaded 🙂