Permalink
Browse files

Add run_js_blocking to tab API

  • Loading branch information...
The-Compiler committed Jul 13, 2016
1 parent 4f97b63 commit 5b1cca92ab199798337d873f8a708df7acebe12a
@@ -611,6 +611,14 @@ def run_js_async(self, code, callback=None):
"""
raise NotImplementedError
+ def run_js_blocking(self, code):
+ """Run javascript and block.
+
+ This returns the result to the caller. Its use should be avoided when
+ possible as it runs a local event loop for QtWebEngine.
+ """
+ raise NotImplementedError
+
def shutdown(self):
raise NotImplementedError
@@ -343,6 +343,21 @@ def run_js_async(self, code, callback=None):
else:
self._widget.page().runJavaScript(code, callback)
+ def run_js_blocking(self, code):
+ loop = qtutils.EventLoop()
+ js_ret = None
+
+ def js_cb(val):
+ nonlocal js_ret
+ js_ret = val
+ loop.quit()
+
+ self.run_js_async(code, js_cb)
+ loop.exec_() # blocks until loop.quit() in js_cb
+ assert js_ret is not None
+
+ return js_ret
+
def shutdown(self):
log.stub()
@@ -525,10 +525,13 @@ def dump_async(self, callback, *, plain=False):
callback(frame.toHtml())
def run_js_async(self, code, callback=None):
- result = self._widget.page().mainFrame().evaluateJavaScript(code)
+ result = self.run_js_blocking(code)
if callback is not None:
callback(result)
+ def run_js_blocking(self, code):
+ return self._widget.page().mainFrame().evaluateJavaScript(code)
+
def icon(self):
return self._widget.icon()
@@ -121,6 +121,12 @@ def test_tab(qtbot, view, config_stub, tab_registry):
assert view.parent() is tab_w
+class TestJs:
+
+ def test_blocking(self, tab):
+ assert tab.run_js_blocking('1 + 1') == 2
+
+
class TestTabData:
def test_known_attr(self):

0 comments on commit 5b1cca9

Please sign in to comment.