From b6c5ff25fdaa8a37ad777def442eb2e28564d326 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 29 Mar 2016 13:18:10 +0200 Subject: [PATCH] tests: add click_element to quteprocess --- tests/integration/quteprocess.py | 33 +++++++++++++++++++++++++--- tests/integration/test_dirbrowser.py | 8 +------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tests/integration/quteprocess.py b/tests/integration/quteprocess.py index d41356f3e..1cce4fb1a 100644 --- a/tests/integration/quteprocess.py +++ b/tests/integration/quteprocess.py @@ -37,6 +37,7 @@ from PyQt5.QtCore import pyqtSignal, QUrl import testprocess from qutebrowser.misc import ipc from qutebrowser.utils import log, utils +from qutebrowser.browser import webelem from helpers import utils as testutils @@ -253,9 +254,13 @@ class QuteProc(testprocess.Process): path if path != '/' else '') def wait_for_js(self, message): - """Wait for the given javascript console message.""" - self.wait_for(category='js', function='javaScriptConsoleMessage', - message='[*] {}'.format(message)) + """Wait for the given javascript console message. + + Return: + The LogLine. + """ + return self.wait_for(category='js', function='javaScriptConsoleMessage', + message='[*] {}'.format(message)) def _is_error_logline(self, msg): """Check if the given LogLine is some kind of error message.""" @@ -422,6 +427,28 @@ class QuteProc(testprocess.Process): """Press the given keys using :fake-key.""" self.send_cmd(':fake-key -g "{}"'.format(keys)) + def click_element(self, text): + """Click the element with the given text.""" + # Use Javascript and XPath to find the right element, use console.log to + # return an error (no element found, ambiguous element) + script = ( + 'var _es = document.evaluate(\'//*[text()="{text}"]\', document, ' + 'null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);' + 'if (_es.snapshotLength == 0) {{ console.log("qute:no elems"); }} ' + 'else if (_es.snapshotLength > 1) {{ console.log("qute:ambiguous ' + 'elems") }} ' + 'else {{ console.log("qute:okay"); _es.snapshotItem(0).click() }}' + ).format(text=webelem.javascript_escape(text)) + self.send_cmd(':jseval ' + script) + message = self.wait_for_js('qute:*').message + if message.endswith('qute:no elems'): + raise ValueError('No element with {!r} found'.format(text)) + elif message.endswith('qute:ambiguous elems'): + raise ValueError('Element with {!r} is not unique'.format(text)) + elif not message.endswith('qute:okay'): + raise ValueError('Invalid response from qutebrowser: {}' + .format(message)) + @pytest.yield_fixture(scope='module') def quteproc_process(qapp, httpbin, request): diff --git a/tests/integration/test_dirbrowser.py b/tests/integration/test_dirbrowser.py index 6265c2998..452dfbb4b 100644 --- a/tests/integration/test_dirbrowser.py +++ b/tests/integration/test_dirbrowser.py @@ -178,13 +178,7 @@ def test_enter_folder_smoke(dir_layout, quteproc): @pytest.mark.parametrize('folder', DirLayout.layout_folders()) def test_enter_folder(dir_layout, quteproc, folder): quteproc.open_url(dir_layout.file_url()) - # Use Javascript and XPath to click the link that has the folder name as - # text. - quteproc.send_cmd( - ':jseval document.evaluate(\'//a[text()="{}"]\', document, null, ' - 'XPathResult.ANY_TYPE, null).iterateNext().click()' - .format(folder) - ) + quteproc.click_element(text=folder) page = parse(quteproc) assert page.path == dir_layout.path(folder) assert page.parent == dir_layout.path()