def __repr__(self): try: url = utils.elide(self.url().toDisplayString(QUrl.EncodeUnicode), 100) except AttributeError: url = '<AttributeError>' return utils.get_repr(self, tab_id=self.tab_id, url=url)
def wait_for_load_finished(self, path, *, port=None, https=False, timeout=None, load_status='success'): """Wait until any tab has finished loading.""" __tracebackhide__ = True if timeout is None: if 'CI' in os.environ: timeout = 15000 else: timeout = 5000 url = self.path_to_url(path, port=port, https=https) # We really need the same representation that the webview uses in its # __repr__ url = utils.elide(QUrl(url).toDisplayString(QUrl.EncodeUnicode), 100) pattern = re.compile( r"(load status for <qutebrowser\.browser\.webview\.WebView " r"tab_id=\d+ url='{url}/?'>: LoadStatus\.{load_status}|fetch: " r"PyQt5\.QtCore\.QUrl\('{url}'\) -> .*)".format( load_status=re.escape(load_status), url=re.escape(url))) try: self.wait_for(message=pattern, timeout=timeout) except testprocess.WaitForTimeout: raise testprocess.WaitForTimeout("Timed out while waiting for {} " "to be loaded".format(url))
def _on_navigation_request( self, navigation: usertypes.NavigationRequest ) -> None: """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, navigation.navigation_type, navigation.is_main_frame)) if not navigation.url.isValid(): # Also a WORKAROUND for missing IDNA 2008 support in QUrl, see # https://bugreports.qt.io/browse/QTBUG-60364 if navigation.navigation_type == navigation.Type.link_clicked: msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal log.webview.debug("Ignoring invalid URL {} in " "acceptNavigationRequest: {}".format( navigation.url.toDisplayString(), navigation.url.errorString())) navigation.accepted = False
def _wait_for_new(self, timeout, do_skip, **kwargs): """Wait for a log message which doesn't exist yet. Called via wait_for. """ __tracebackhide__ = lambda e: e.errisinstance(WaitForTimeout) message = kwargs.get('message', None) if message is not None: elided = quteutils.elide(repr(message), 50) self._log("\n----> Waiting for {} in the log".format(elided)) spy = QSignalSpy(self.new_data) elapsed_timer = QElapsedTimer() elapsed_timer.start() while True: # Skip if there are pending messages causing a skip self._maybe_skip() got_signal = spy.wait(timeout) if not got_signal or elapsed_timer.hasExpired(timeout): msg = "Timed out after {}ms waiting for {!r}.".format( timeout, kwargs) if do_skip: pytest.skip(msg) else: raise WaitForTimeout(msg) match = self._wait_for_match(spy, kwargs) if match is not None: if message is not None: self._log("----> found it") return match
def __repr__(self): try: url = utils.elide(self.url().toDisplayString(QUrl.EncodeUnicode), 100) except (AttributeError, RuntimeError) as exc: url = '<{}>'.format(exc.__class__.__name__) return utils.get_repr(self, tab_id=self.tab_id, url=url)
def wait_for_load_finished_url(self, url, *, timeout=None, load_status='success'): """Wait until a URL has finished loading.""" __tracebackhide__ = (lambda e: e.errisinstance( testprocess.WaitForTimeout)) if timeout is None: if 'CI' in os.environ: timeout = 15000 else: timeout = 5000 # We really need the same representation that the webview uses in its # __repr__ qurl = QUrl(url) if not qurl.isValid(): raise ValueError("Invalid URL {}: {}".format(url, qurl.errorString())) url = utils.elide(qurl.toDisplayString(QUrl.EncodeUnicode), 100) assert url pattern = re.compile( r"(load status for <qutebrowser\.browser\..* " r"tab_id=\d+ url='{url}/?'>: LoadStatus\.{load_status}|fetch: " r"PyQt5\.QtCore\.QUrl\('{url}'\) -> .*)".format( load_status=re.escape(load_status), url=re.escape(url))) try: self.wait_for(message=pattern, timeout=timeout) except testprocess.WaitForTimeout: raise testprocess.WaitForTimeout("Timed out while waiting for {} " "to be loaded".format(url))
def __repr__(self) -> str: try: qurl = self.url() url = qurl.toDisplayString(QUrl.EncodeUnicode) # type: ignore except (AttributeError, RuntimeError) as exc: url = '<{}>'.format(exc.__class__.__name__) else: url = utils.elide(url, 100) return utils.get_repr(self, tab_id=self.tab_id, url=url)
def wait_for_load_finished(self, path, timeout=15000): """Wait until any tab has finished loading.""" url = self.path_to_url(path) # We really need the same representation that the webview uses in its # __repr__ url = utils.elide(QUrl(url).toDisplayString(QUrl.EncodeUnicode), 100) pattern = re.compile( r"(load status for <qutebrowser.browser.webview.WebView " r"tab_id=\d+ url='{url}'>: LoadStatus.success|fetch: " r"PyQt5.QtCore.QUrl\('{url}'\) -> .*)".format(url=re.escape(url))) self.wait_for(message=pattern, timeout=timeout)
def _on_navigation_request(self, navigation): """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, navigation.navigation_type, navigation.is_main_frame)) if (navigation.navigation_type == navigation.Type.link_clicked and not navigation.url.isValid()): msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal navigation.accepted = False
def _js_cb_single(self, callback, js_elem): """Handle a found focus elem coming from JS and call the real callback. Args: callback: The callback to call with the found element. Called with a WebEngineElement or None. js_elem: The element serialized from javascript. """ debug_str = ('None' if js_elem is None else utils.elide(repr(js_elem), 1000)) log.webview.debug("Got element from JS: {}".format(debug_str)) if js_elem is None: callback(None) else: elem = webengineelem.WebEngineElement(js_elem, tab=self._tab) callback(elem)
def _js_cb_single(self, callback, js_elem): """Handle a found focus elem coming from JS and call the real callback. Args: callback: The callback to call with the found element. Called with a WebEngineElement or None. js_elem: The element serialized from javascript. """ debug_str = ('None' if js_elem is None else utils.elide( repr(js_elem), 1000)) log.webview.debug("Got element from JS: {}".format(debug_str)) if js_elem is None: callback(None) else: elem = webengineelem.WebEngineElement(js_elem, tab=self._tab) callback(elem)
def wait_for_load_finished_url(self, url, *, timeout=None, load_status='success', after=None): """Wait until a URL has finished loading.""" __tracebackhide__ = ( lambda e: e.errisinstance(testprocess.WaitForTimeout)) if timeout is None: if 'CI' in os.environ: timeout = 15000 else: timeout = 5000 qurl = QUrl(url) if not qurl.isValid(): raise ValueError("Invalid URL {}: {}".format( url, qurl.errorString())) if (qurl == QUrl('about:blank') and not qtutils.version_check('5.10', compiled=False)): # For some reason, we don't get a LoadStatus.success for # about:blank sometimes. # However, if we do this for Qt 5.10, we get general testsuite # instability as site loads get reported with about:blank... pattern = "Changing title for idx * to 'about:blank'" else: # We really need the same representation that the webview uses in # its __repr__ url = utils.elide(qurl.toDisplayString(QUrl.EncodeUnicode), 100) assert url pattern = re.compile( r"(load status for <qutebrowser\.browser\..* " r"tab_id=\d+ url='{url}/?'>: LoadStatus\.{load_status}|fetch: " r"PyQt5\.QtCore\.QUrl\('{url}'\) -> .*)".format( load_status=re.escape(load_status), url=re.escape(url))) try: self.wait_for(message=pattern, timeout=timeout, after=after) except testprocess.WaitForTimeout: raise testprocess.WaitForTimeout("Timed out while waiting for {} " "to be loaded".format(url))
def wait_for_load_finished(self, path, *, port=None, https=False, timeout=None, load_status="success"): """Wait until any tab has finished loading.""" if timeout is None: if "CI" in os.environ: timeout = 15000 else: timeout = 5000 url = self.path_to_url(path, port=port, https=https) # We really need the same representation that the webview uses in its # __repr__ url = utils.elide(QUrl(url).toDisplayString(QUrl.EncodeUnicode), 100) pattern = re.compile( r"(load status for <qutebrowser\.browser\.webview\.WebView " r"tab_id=\d+ url='{url}'>: LoadStatus\.{load_status}|fetch: " r"PyQt5\.QtCore\.QUrl\('{url}'\) -> .*)".format(load_status=re.escape(load_status), url=re.escape(url)) ) self.wait_for(message=pattern, timeout=timeout)
def wait_for_load_finished_url(self, url, *, timeout=None, load_status='success', after=None): """Wait until a URL has finished loading.""" __tracebackhide__ = (lambda e: e.errisinstance( testprocess.WaitForTimeout)) if timeout is None: if 'CI' in os.environ: timeout = 15000 else: timeout = 5000 qurl = QUrl(url) if not qurl.isValid(): raise ValueError("Invalid URL {}: {}".format(url, qurl.errorString())) if (qurl == QUrl('about:blank') and not qtutils.version_check('5.10', compiled=False)): # For some reason, we don't get a LoadStatus.success for # about:blank sometimes. # However, if we do this for Qt 5.10, we get general testsuite # instability as site loads get reported with about:blank... pattern = "Changing title for idx * to 'about:blank'" else: # We really need the same representation that the webview uses in # its __repr__ url = utils.elide(qurl.toDisplayString(QUrl.EncodeUnicode), 100) assert url pattern = re.compile( r"(load status for <qutebrowser\.browser\..* " r"tab_id=\d+ url='{url}/?'>: LoadStatus\.{load_status}|fetch: " r"PyQt5\.QtCore\.QUrl\('{url}'\) -> .*)".format( load_status=re.escape(load_status), url=re.escape(url))) try: self.wait_for(message=pattern, timeout=timeout, after=after) except testprocess.WaitForTimeout: raise testprocess.WaitForTimeout("Timed out while waiting for {} " "to be loaded".format(url))
def _on_navigation_request(self, navigation): """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, navigation.navigation_type, navigation.is_main_frame)) if not navigation.url.isValid(): # Also a WORKAROUND for missing IDNA 2008 support in QUrl, see # https://bugreports.qt.io/browse/QTBUG-60364 if navigation.navigation_type == navigation.Type.link_clicked: msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal log.webview.debug("Ignoring invalid URL {} in " "acceptNavigationRequest: {}".format( navigation.url.toDisplayString(), navigation.url.errorString())) navigation.accepted = False
def _on_navigation_request( self, navigation: usertypes.NavigationRequest) -> None: """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, navigation.navigation_type, navigation.is_main_frame)) if navigation.is_main_frame: self.data.last_navigation = navigation if not navigation.url.isValid(): if navigation.navigation_type == navigation.Type.link_clicked: msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal log.webview.debug("Ignoring invalid URL {} in " "acceptNavigationRequest: {}".format( navigation.url.toDisplayString(), navigation.url.errorString())) navigation.accepted = False
def wait_for_load_finished_url(self, url, *, timeout=None, load_status='success', after=None): """Wait until a URL has finished loading.""" __tracebackhide__ = ( lambda e: e.errisinstance(testprocess.WaitForTimeout)) if timeout is None: if testutils.ON_CI: timeout = 15000 else: timeout = 5000 qurl = QUrl(url) if not qurl.isValid(): raise ValueError("Invalid URL {}: {}".format( url, qurl.errorString())) # We really need the same representation that the webview uses in # its __repr__ url = utils.elide(qurl.toDisplayString(QUrl.EncodeUnicode), 100) assert url pattern = re.compile( r"(load status for <qutebrowser\.browser\..* " r"tab_id=\d+ url='{url}/?'>: LoadStatus\.{load_status}|fetch: " r"PyQt5\.QtCore\.QUrl\('{url}'\) -> .*)".format( load_status=re.escape(load_status), url=re.escape(url))) try: self.wait_for(message=pattern, timeout=timeout, after=after) except testprocess.WaitForTimeout: raise testprocess.WaitForTimeout("Timed out while waiting for {} " "to be loaded".format(url))
def wait_for_load_finished(self, path, *, port=None, https=False, timeout=None, load_status='success'): """Wait until any tab has finished loading.""" if timeout is None: if 'CI' in os.environ: timeout = 15000 else: timeout = 5000 url = self.path_to_url(path, port=port, https=https) # We really need the same representation that the webview uses in its # __repr__ url = utils.elide(QUrl(url).toDisplayString(QUrl.EncodeUnicode), 100) pattern = re.compile( r"(load status for <qutebrowser\.browser\.webview\.WebView " r"tab_id=\d+ url='{url}/?'>: LoadStatus\.{load_status}|fetch: " r"PyQt5\.QtCore\.QUrl\('{url}'\) -> .*)".format( load_status=re.escape(load_status), url=re.escape(url))) self.wait_for(message=pattern, timeout=timeout)
def __repr__(self): url = utils.elide(self.url().toDisplayString(), 50) return utils.get_repr(self, tab_id=self.tab_id, url=url)
def test_too_small(self): """Test eliding to 0 chars which should fail.""" with self.assertRaises(ValueError): utils.elide('foo', 0)
def test_length_one(self): """Test eliding to 1 char which should yield ...""" self.assertEqual(utils.elide('foo', 1), self.ELLIPSIS)
def __repr__(self): flags = QUrl.EncodeUnicode urlstr = self.url().toDisplayString(flags) # type: ignore[arg-type] url = utils.elide(urlstr, 100) return utils.get_repr(self, tab_id=self._tab_id, url=url)
def test_fits(self): """Test eliding with a string which fits exactly.""" self.assertEqual(utils.elide('foo', 3), 'foo')
def test_elided(self): """Test eliding with a string which should get elided.""" assert utils.elide('foobar', 3) == 'fo' + self.ELLIPSIS
def test_fits(self): """Test eliding with a string which fits exactly.""" assert utils.elide('foo', 3) == 'foo'
def __repr__(self): url = utils.elide(self.url().toDisplayString(QUrl.EncodeUnicode), 100) return utils.get_repr(self, tab_id=self._tab_id, url=url)
def test_elided(self): """Test eliding with a string which should get elided.""" self.assertEqual(utils.elide('foobar', 3), 'fo' + self.ELLIPSIS)
def test_too_small(self): """Test eliding to 0 chars which should fail.""" with pytest.raises(ValueError): utils.elide('foo', 0)
def test_length_one(self): """Test eliding to 1 char which should yield ...""" assert utils.elide('foo', 1) == self.ELLIPSIS
def __repr__(self): url = utils.elide(self.url().toDisplayString(QUrl.EncodeUnicode), 100) return utils.get_repr(self, tab_id=self.tab_id, url=url)