Last active
December 12, 2015 13:39
-
-
Save rcoup/4780273 to your computer and use it in GitHub Desktop.
Selenium Async JS execution idea?
This file contains hidden or 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
| 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
win.