Make most of search BDD tests work with QtWebEngine

This commit is contained in:
Florian Bruhin 2017-04-27 21:01:10 +02:00
parent 4cd977cab6
commit d62ebdb926
4 changed files with 84 additions and 21 deletions

View File

@ -40,7 +40,7 @@ from qutebrowser.browser.webengine import (webview, webengineelem, tabhistory,
webenginedownloads) webenginedownloads)
from qutebrowser.misc import miscwidgets from qutebrowser.misc import miscwidgets
from qutebrowser.utils import (usertypes, qtutils, log, javascript, utils, from qutebrowser.utils import (usertypes, qtutils, log, javascript, utils,
objreg, jinja) objreg, jinja, debug)
_qute_scheme_handler = None _qute_scheme_handler = None
@ -128,12 +128,21 @@ class WebEngineSearch(browsertab.AbstractSearch):
super().__init__(parent) super().__init__(parent)
self._flags = QWebEnginePage.FindFlags(0) self._flags = QWebEnginePage.FindFlags(0)
def _find(self, text, flags, cb=None): def _find(self, text, flags, callback, caller):
"""Call findText on the widget with optional callback.""" """Call findText on the widget."""
if cb is None: def wrapped_callback(found):
self._widget.findText(text, flags) """Wrap the callback to do debug logging."""
else: found_text = 'found' if found else "didn't find"
self._widget.findText(text, flags, cb) if flags:
flag_text = 'with flags {}'.format(debug.qflags_key(
QWebEnginePage, flags, klass=QWebEnginePage.FindFlag))
else:
flag_text = ''
log.webview.debug(' '.join([caller, found_text, text, flag_text])
.strip())
if callback is not None:
callback(found)
self._widget.findText(text, flags, wrapped_callback)
def search(self, text, *, ignore_case=False, reverse=False, def search(self, text, *, ignore_case=False, reverse=False,
result_cb=None): result_cb=None):
@ -148,7 +157,7 @@ class WebEngineSearch(browsertab.AbstractSearch):
self.text = text self.text = text
self._flags = flags self._flags = flags
self._find(text, flags, result_cb) self._find(text, flags, result_cb, 'search')
def clear(self): def clear(self):
self._widget.findText('') self._widget.findText('')
@ -160,10 +169,10 @@ class WebEngineSearch(browsertab.AbstractSearch):
flags &= ~QWebEnginePage.FindBackward flags &= ~QWebEnginePage.FindBackward
else: else:
flags |= QWebEnginePage.FindBackward flags |= QWebEnginePage.FindBackward
self._find(self.text, flags, result_cb) self._find(self.text, flags, result_cb, 'prev_result')
def next_result(self, *, result_cb=None): def next_result(self, *, result_cb=None):
self._find(self.text, self._flags, result_cb) self._find(self.text, self._flags, result_cb, 'next_result')
class WebEngineCaret(browsertab.AbstractCaret): class WebEngineCaret(browsertab.AbstractCaret):

View File

@ -36,7 +36,7 @@ from qutebrowser.browser import browsertab
from qutebrowser.browser.network import proxy from qutebrowser.browser.network import proxy
from qutebrowser.browser.webkit import webview, tabhistory, webkitelem from qutebrowser.browser.webkit import webview, tabhistory, webkitelem
from qutebrowser.browser.webkit.network import webkitqutescheme from qutebrowser.browser.webkit.network import webkitqutescheme
from qutebrowser.utils import qtutils, objreg, usertypes, utils, log from qutebrowser.utils import qtutils, objreg, usertypes, utils, log, debug
def init(): def init():
@ -104,7 +104,7 @@ class WebKitSearch(browsertab.AbstractSearch):
super().__init__(parent) super().__init__(parent)
self._flags = QWebPage.FindFlags(0) self._flags = QWebPage.FindFlags(0)
def _call_cb(self, callback, found): def _call_cb(self, callback, found, text, flags, caller):
"""Call the given callback if it's non-None. """Call the given callback if it's non-None.
Delays the call via a QTimer so the website is re-rendered in between. Delays the call via a QTimer so the website is re-rendered in between.
@ -112,7 +112,22 @@ class WebKitSearch(browsertab.AbstractSearch):
Args: Args:
callback: What to call callback: What to call
found: If the text was found found: If the text was found
text: The text searched for
flags: The flags searched with
caller: Name of the caller.
""" """
found_text = 'found' if found else "didn't find"
# Removing FindWrapsAroundDocument to get the same logging as with
# QtWebEngine
debug_flags = debug.qflags_key(
QWebPage, flags & ~QWebPage.FindWrapsAroundDocument,
klass=QWebPage.FindFlag)
if debug_flags != '0x0000':
flag_text = 'with flags {}'.format(debug_flags)
else:
flag_text = ''
log.webview.debug(' '.join([caller, found_text, text, flag_text])
.strip())
if callback is not None: if callback is not None:
QTimer.singleShot(0, functools.partial(callback, found)) QTimer.singleShot(0, functools.partial(callback, found))
@ -137,11 +152,11 @@ class WebKitSearch(browsertab.AbstractSearch):
self._widget.findText(text, flags | QWebPage.HighlightAllOccurrences) self._widget.findText(text, flags | QWebPage.HighlightAllOccurrences)
self.text = text self.text = text
self._flags = flags self._flags = flags
self._call_cb(result_cb, found) self._call_cb(result_cb, found, text, flags, 'search')
def next_result(self, *, result_cb=None): def next_result(self, *, result_cb=None):
found = self._widget.findText(self.text, self._flags) found = self._widget.findText(self.text, self._flags)
self._call_cb(result_cb, found) self._call_cb(result_cb, found, self.text, self._flags, 'next_result')
def prev_result(self, *, result_cb=None): def prev_result(self, *, result_cb=None):
# The int() here makes sure we get a copy of the flags. # The int() here makes sure we get a copy of the flags.
@ -151,7 +166,7 @@ class WebKitSearch(browsertab.AbstractSearch):
else: else:
flags |= QWebPage.FindBackward flags |= QWebPage.FindBackward
found = self._widget.findText(self.text, flags) found = self._widget.findText(self.text, flags)
self._call_cb(result_cb, found) self._call_cb(result_cb, found, self.text, flags, 'prev_result')
class WebKitCaret(browsertab.AbstractCaret): class WebKitCaret(browsertab.AbstractCaret):

View File

@ -11,37 +11,45 @@ Feature: Searching on a page
Scenario: Searching text Scenario: Searching text
When I run :search foo When I run :search foo
And I wait for "search found foo" in the log
Then "foo" should be found Then "foo" should be found
Scenario: Searching twice Scenario: Searching twice
When I run :search foo When I run :search foo
And I wait for "search found foo" in the log
And I run :search bar And I run :search bar
And I wait for "search found bar" in the log
Then "Bar" should be found Then "Bar" should be found
Scenario: Searching with --reverse Scenario: Searching with --reverse
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search -r foo And I run :search -r foo
And I wait for "search found foo with flags FindBackward" in the log
Then "Foo" should be found Then "Foo" should be found
Scenario: Searching without matches Scenario: Searching without matches
When I run :search doesnotmatch When I run :search doesnotmatch
And I wait for "search didn't find doesnotmatch" in the log
Then the warning "Text 'doesnotmatch' not found on page!" should be shown Then the warning "Text 'doesnotmatch' not found on page!" should be shown
@xfail_norun @xfail_norun
Scenario: Searching with / and spaces at the end (issue 874) Scenario: Searching with / and spaces at the end (issue 874)
When I run :set-cmd-text -s /space When I run :set-cmd-text -s /space
And I run :command-accept And I run :command-accept
And I wait for "search found space " in the log
Then "space " should be found Then "space " should be found
Scenario: Searching with / and slash in search term (issue 507) Scenario: Searching with / and slash in search term (issue 507)
When I run :set-cmd-text -s //slash When I run :set-cmd-text -s //slash
And I run :command-accept And I run :command-accept
And I wait for "search found /slash" in the log
Then "/slash" should be found Then "/slash" should be found
# This doesn't work because this is QtWebKit behavior. # This doesn't work because this is QtWebKit behavior.
@xfail_norun @xfail_norun
Scenario: Searching text with umlauts Scenario: Searching text with umlauts
When I run :search blub When I run :search blub
And I wait for "search didn't find blub" in the log
Then the warning "Text 'blub' not found on page!" should be shown Then the warning "Text 'blub' not found on page!" should be shown
## ignore-case ## ignore-case
@ -49,21 +57,25 @@ Feature: Searching on a page
Scenario: Searching text with ignore-case = true Scenario: Searching text with ignore-case = true
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search bar And I run :search bar
And I wait for "search found bar" in the log
Then "Bar" should be found Then "Bar" should be found
Scenario: Searching text with ignore-case = false Scenario: Searching text with ignore-case = false
When I set general -> ignore-case to false When I set general -> ignore-case to false
And I run :search bar And I run :search bar
And I wait for "search found bar with flags FindCaseSensitively" in the log
Then "bar" should be found Then "bar" should be found
Scenario: Searching text with ignore-case = smart (lower-case) Scenario: Searching text with ignore-case = smart (lower-case)
When I set general -> ignore-case to smart When I set general -> ignore-case to smart
And I run :search bar And I run :search bar
And I wait for "search found bar" in the log
Then "Bar" should be found Then "Bar" should be found
Scenario: Searching text with ignore-case = smart (upper-case) Scenario: Searching text with ignore-case = smart (upper-case)
When I set general -> ignore-case to smart When I set general -> ignore-case to smart
And I run :search Foo And I run :search Foo
And I wait for "search found Foo with flags FindCaseSensitively" in the log
Then "Foo" should be found # even though foo was first Then "Foo" should be found # even though foo was first
## :search-next ## :search-next
@ -71,19 +83,25 @@ Feature: Searching on a page
Scenario: Jumping to next match Scenario: Jumping to next match
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search foo And I run :search foo
And I wait for "search found foo" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo" in the log
Then "Foo" should be found Then "Foo" should be found
Scenario: Jumping to next match with count Scenario: Jumping to next match with count
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search baz And I run :search baz
And I wait for "search found baz" in the log
And I run :search-next with count 2 And I run :search-next with count 2
And I wait for "next_result found baz" in the log
Then "BAZ" should be found Then "BAZ" should be found
Scenario: Jumping to next match with --reverse Scenario: Jumping to next match with --reverse
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search --reverse foo And I run :search --reverse foo
And I wait for "search found foo with flags FindBackward" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo with flags FindBackward" in the log
Then "foo" should be found Then "foo" should be found
Scenario: Jumping to next match without search Scenario: Jumping to next match without search
@ -95,16 +113,20 @@ Feature: Searching on a page
Scenario: Repeating search in a second tab (issue #940) Scenario: Repeating search in a second tab (issue #940)
When I open data/search.html in a new tab When I open data/search.html in a new tab
And I run :search foo And I run :search foo
And I wait for "search found foo" in the log
And I run :tab-prev And I run :tab-prev
And I run :search-next And I run :search-next
And I wait for "search found foo" in the log
Then "foo" should be found Then "foo" should be found
# https://github.com/qutebrowser/qutebrowser/issues/2438 # https://github.com/qutebrowser/qutebrowser/issues/2438
Scenario: Jumping to next match after clearing Scenario: Jumping to next match after clearing
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search foo And I run :search foo
And I wait for "search found foo" in the log
And I run :search And I run :search
And I run :search-next And I run :search-next
And I wait for "next_result found foo" in the log
Then "foo" should be found Then "foo" should be found
## :search-prev ## :search-prev
@ -112,23 +134,33 @@ Feature: Searching on a page
Scenario: Jumping to previous match Scenario: Jumping to previous match
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search foo And I run :search foo
And I wait for "search found foo" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo" in the log
And I run :search-prev And I run :search-prev
And I wait for "prev_result found foo with flags FindBackward" in the log
Then "foo" should be found Then "foo" should be found
Scenario: Jumping to previous match with count Scenario: Jumping to previous match with count
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search baz And I run :search baz
And I wait for "search found baz" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found baz" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found baz" in the log
And I run :search-prev with count 2 And I run :search-prev with count 2
And I wait for "prev_result found baz with flags FindBackward" in the log
Then "baz" should be found Then "baz" should be found
Scenario: Jumping to previous match with --reverse Scenario: Jumping to previous match with --reverse
When I set general -> ignore-case to true When I set general -> ignore-case to true
And I run :search --reverse foo And I run :search --reverse foo
And I wait for "search found foo with flags FindBackward" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo with flags FindBackward" in the log
And I run :search-prev And I run :search-prev
And I wait for "prev_result found foo" in the log
Then "Foo" should be found Then "Foo" should be found
Scenario: Jumping to previous match without search Scenario: Jumping to previous match without search
@ -141,14 +173,20 @@ Feature: Searching on a page
Scenario: Wrapping around page Scenario: Wrapping around page
When I run :search foo When I run :search foo
And I wait for "search found foo" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo" in the log
Then "foo" should be found Then "foo" should be found
Scenario: Wrapping around page with --reverse Scenario: Wrapping around page with --reverse
When I run :search --reverse foo When I run :search --reverse foo
And I wait for "search found foo with flags FindBackward" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo with flags FindBackward" in the log
And I run :search-next And I run :search-next
And I wait for "next_result found foo with flags FindBackward" in the log
Then "Foo" should be found Then "Foo" should be found
# TODO: wrapping message with scrolling # TODO: wrapping message with scrolling

View File

@ -19,7 +19,6 @@
import json import json
import pytest
import pytest_bdd as bdd import pytest_bdd as bdd
# pylint: disable=unused-import # pylint: disable=unused-import
@ -28,14 +27,16 @@ from end2end.features.test_yankpaste_bdd import init_fake_clipboard
@bdd.then(bdd.parsers.parse('"{text}" should be found')) @bdd.then(bdd.parsers.parse('"{text}" should be found'))
def check_found_text(request, quteproc, text): def check_found_text(request, quteproc, text):
if request.config.webengine:
# WORKAROUND
# This probably should work with Qt 5.9:
# https://codereview.qt-project.org/#/c/192920/
# https://codereview.qt-project.org/#/c/192921/
# https://bugreports.qt.io/browse/QTBUG-53134
return
quteproc.send_cmd(':yank selection') quteproc.send_cmd(':yank selection')
quteproc.wait_for(message='Setting fake clipboard: {}'.format( quteproc.wait_for(message='Setting fake clipboard: {}'.format(
json.dumps(text))) json.dumps(text)))
# After cancelling the search, the text (sometimes?) shows up as selected.
# However, we can't get it via ':yank selection' it seems?
pytestmark = pytest.mark.qtwebengine_skip("Searched text is not selected...")
bdd.scenarios('search.feature') bdd.scenarios('search.feature')