@world.absorb
def wait_for_js_variable_truthy(variable):
 javascript = '\n        var callback = arguments[arguments.length - 1];\n        var unloadHandler = function() {{\n          callback("unload");\n        }}\n        addEventListener("beforeunload", unloadHandler);\n        addEventListener("unload", unloadHandler);\n        var intervalID = setInterval(function() {{\n          try {{\n            if({variable}) {{\n              clearInterval(intervalID);\n              removeEventListener("beforeunload", unloadHandler);\n              removeEventListener("unload", unloadHandler);\n              callback(true);\n            }}\n          }} catch (e) {{}}\n        }}, 10);\n    '.format(variable=variable)
 for _ in range(5):
  try:
   result = world.browser.driver.execute_async_script(dedent(javascript))
  except WebDriverException as wde:
   if ('document unloaded while waiting for result' in wde.msg):
    result = 'unload'
   else:
    raise
  if (result == 'unload'):
   world.wait(1)
   continue
  else:
   return result

@ world . absorb def wait_for_js_variable_truthy ( variable ) : javascript = '\n        var callback = arguments[arguments.length - 1];\n        var unloadHandler = function() {{\n          callback("unload");\n        }}\n        addEventListener("beforeunload", unloadHandler);\n        addEventListener("unload", unloadHandler);\n        var intervalID = setInterval(function() {{\n          try {{\n            if({variable}) {{\n              clearInterval(intervalID);\n              removeEventListener("beforeunload", unloadHandler);\n              removeEventListener("unload", unloadHandler);\n              callback(true);\n            }}\n          }} catch (e) {{}}\n        }}, 10);\n    ' . format ( variable = variable ) for _ in range ( 5 ) : try : result = world . browser . driver . execute_async_script ( dedent ( javascript ) ) except WebDriverException as wde : if ( 'document unloaded while waiting for result' in wde . msg ) : result = 'unload' else : raise if ( result == 'unload' ) : world . wait ( 1 ) continue else : return result
using seleniums execute_async_script function .