Add quteproc.click_element_by_id

This commit is contained in:
Florian Bruhin 2016-08-18 13:24:47 +02:00
parent c37c501b25
commit da73a7123c
4 changed files with 39 additions and 8 deletions

View File

@ -3,7 +3,7 @@
<title>quteprocess.click_element test</title>
</head>
<body>
<span onclick='console.log("click_element clicked")'>Test Element</span>
<span id='test' onclick='console.log("click_element clicked")'>Test Element</span>
<span onclick='console.log("click_element special chars")'>"Don't", he shouted</span>
<span>Duplicate</span>
<span>Duplicate</span>

View File

@ -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.

View File

@ -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)

View File

@ -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)