diff --git a/tests/end2end/data/click_element.html b/tests/end2end/data/click_element.html index 9e5ce5bb5..628674f44 100644 --- a/tests/end2end/data/click_element.html +++ b/tests/end2end/data/click_element.html @@ -3,7 +3,7 @@ quteprocess.click_element test - Test Element + Test Element "Don't", he shouted Duplicate Duplicate diff --git a/tests/end2end/fixtures/quteprocess.py b/tests/end2end/fixtures/quteprocess.py index 7fbe808da..536d740e0 100644 --- a/tests/end2end/fixtures/quteprocess.py +++ b/tests/end2end/fixtures/quteprocess.py @@ -526,7 +526,7 @@ class QuteProc(testprocess.Process): """Press the given keys using :fake-key.""" self.send_cmd(':fake-key -g "{}"'.format(keys)) - def click_element(self, text): + def click_element_by_text(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) @@ -548,6 +548,21 @@ class QuteProc(testprocess.Process): raise ValueError('Invalid response from qutebrowser: {}' .format(message)) + def click_element_by_id(self, elem_id): + """Click the element with the given ID.""" + script = ( + 'var _elem = document.getElementById("{elem_id}"); ' + 'if (_elem === null) {{ console.log("qute:no elem"); }} ' + 'else {{ console.log("qute:okay"); _elem.click(); }}' + ).format(elem_id=javascript.string_escape(elem_id)) + self.send_cmd(':jseval ' + script, escape=False) + message = self.wait_for_js('qute:*').message + if message.endswith('qute:no elem'): + raise ValueError('No element with ID {!r} found'.format(elem_id)) + elif not message.endswith('qute:okay'): + raise ValueError('Invalid response from qutebrowser: {}' + .format(message)) + def compare_session(self, expected): """Compare the current sessions against the given template. diff --git a/tests/end2end/fixtures/test_quteprocess.py b/tests/end2end/fixtures/test_quteprocess.py index 4c9f74f63..96c7ae247 100644 --- a/tests/end2end/fixtures/test_quteprocess.py +++ b/tests/end2end/fixtures/test_quteprocess.py @@ -240,28 +240,44 @@ def test_log_line_no_match(): quteprocess.LogLine("Hello World!") -class TestClickElement: +class TestClickElementByText: @pytest.fixture(autouse=True) def open_page(self, quteproc): quteproc.open_path('data/click_element.html') def test_click_element(self, quteproc): - quteproc.click_element('Test Element') + quteproc.click_element_by_text('Test Element') quteproc.wait_for_js('click_element clicked') def test_click_special_chars(self, quteproc): - quteproc.click_element('"Don\'t", he shouted') + quteproc.click_element_by_text('"Don\'t", he shouted') quteproc.wait_for_js('click_element special chars') def test_duplicate(self, quteproc): with pytest.raises(ValueError) as excinfo: - quteproc.click_element('Duplicate') + quteproc.click_element_by_text('Duplicate') assert 'not unique' in str(excinfo.value) def test_nonexistent(self, quteproc): with pytest.raises(ValueError) as excinfo: - quteproc.click_element('no element exists with this text') + quteproc.click_element_by_text('no element exists with this text') + assert 'No element' in str(excinfo.value) + + +class TestClickElementById: + + @pytest.fixture(autouse=True) + def open_page(self, quteproc): + quteproc.open_path('data/click_element.html') + + def test_click_element(self, quteproc): + quteproc.click_element_by_id('test') + quteproc.wait_for_js('click_element clicked') + + def test_nonexistent(self, quteproc): + with pytest.raises(ValueError) as excinfo: + quteproc.click_element_by_id('blah') assert 'No element' in str(excinfo.value) diff --git a/tests/end2end/test_dirbrowser.py b/tests/end2end/test_dirbrowser.py index 52ef10817..a0c0a1aa2 100644 --- a/tests/end2end/test_dirbrowser.py +++ b/tests/end2end/test_dirbrowser.py @@ -179,7 +179,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()) - quteproc.click_element(text=folder) + quteproc.click_element_by_text(text=folder) expected_url = urlutils.file_url(dir_layout.path(folder)) quteproc.wait_for_load_finished_url(expected_url) page = parse(quteproc)