Skip to content

Instantly share code, notes, and snippets.

@rcoup
Last active December 12, 2015 13:39
Show Gist options
  • Select an option

  • Save rcoup/4780273 to your computer and use it in GitHub Desktop.

Select an option

Save rcoup/4780273 to your computer and use it in GitHub Desktop.
Selenium Async JS execution idea?
class BaseTestCase(TestCase):
def __init__(self, *args, **kwargs):
super(BaseTestCase, self).__init__(*args, **kwargs)
self._script_id = 0
def my_sync_test(self):
result = self.js("""
var result = {foo: [1,2,3]};
testDone(result);
""")
print result['foo']
def my_async_test(self):
result = self.js("""
var start = (new Date()).getTime();
setTimeout(function() {
testDone([start, new Date().getTime()]);
}, 1000);
""")
print result
def js(script):
self._script_id += 1;
self.execute_script("""
(function() {
function testDone(result) {
document.body.setAttribute("data-jsTest-%(id)d", JSON.stringify(result));
}
%(script)s
})();
""" % {'id': self._script_id, 'script': script}
result = self.find_soon('body[data-jsTest-%d]' % self._script_id).attributes['data-jsTest-%d' % self._script_id]
return json.loads(result)
@hamishcampbell
Copy link

win.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment