def _make_proxy(self): def callback(val): self._callback_count += 1 def errback(message, raise_): self._errback_count += 1 if raise_: raise Exception() return OneShotCallbackProxy(None, callback, errback, timeout=0)
def _make_proxy(self): def callback(val): self._callback_count += 1 def errback(message, raise_): self._errback_count += 1 if raise_: raise Exception() class Logger(SplashLogger): def __init__(self, uid, verbosity): self.messages = [] super().__init__(uid, verbosity) def log(self, message, min_level=None): self.messages.append((message, min_level)) super().log(message, min_level) logger = Logger(uid=0, verbosity=2) return OneShotCallbackProxy(None, callback, errback, logger, timeout=0)
def test_negative_timeout_is_invalid(self): with self.assertRaises(ValueError): cb_proxy = OneShotCallbackProxy(None, lambda a: a, lambda b: b, -1)
def test_negative_timeout_is_invalid(self): with self.assertRaises(ValueError): logger = SplashLogger(uid=0, verbosity=2) cb_proxy = OneShotCallbackProxy(None, lambda a: a, lambda b: b, logger, -1)
def wait_for_resume(self, js_source, callback, errback, timeout): """ Run some Javascript asynchronously. The JavaScript must contain a method called `main()` that accepts one argument. The first argument will be an object with `resume()` and `error()` methods. The code _must_ call one of these functions before the timeout or else it will be canceled. """ frame = self.web_page.mainFrame() callback_proxy = OneShotCallbackProxy(self, callback, errback, self.logger, timeout) self._callback_proxies_to_cancel.add(callback_proxy) frame.addToJavaScriptWindowObject(callback_proxy.name, callback_proxy) wrapped = u""" (function () { try { eval(%(script_text)s); } catch (err) { var main = function (splash) { throw err; } } (function () { var sanitize = %(sanitize_func)s; var _result = {}; var _splash = window["%(callback_name)s"]; var splash = { 'error': function (message) { _splash.error(message, false); }, 'resume': function (value) { _result['value'] = value; try { _splash.resume(sanitize(_result)); } catch (err) { _splash.error(err, true); } }, 'set': function (key, value) { _result[key] = value; } }; delete window["%(callback_name)s"]; try { if (typeof main === 'undefined') { throw "wait_for_resume(): no main() function defined"; } main(splash); } catch (err) { _splash.error(err, true); } })(); })();undefined """ % dict(sanitize_func=SANITIZE_FUNC_JS, script_text=escape_js(js_source), callback_name=callback_proxy.name) def cancel_callback(): callback_proxy.cancel(reason='javascript window object cleared') self.logger.log("wait_for_resume wrapped script:\n%s" % wrapped, min_level=3) frame.javaScriptWindowObjectCleared.connect(cancel_callback) frame.evaluateJavaScript(wrapped)