Skip to content

Commit

Permalink
Merge pull request #248 from ordinary-jamie/feature/add-clickbutton-k…
Browse files Browse the repository at this point in the history
…warg

Add TestResponse.clickbutton onclick kwarg
  • Loading branch information
gawel authored Mar 9, 2023
2 parents 8ef619f + b5c16e7 commit fb46c6e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def test_clickbutton(self):
'This is foo.',
app.get('/').clickbutton(buttonid='button1', verbose=True)
)
self.assertIn(
'This is foo.',
app.get('/').clickbutton(buttonid='button3', onclick=r".*href='(.*?)'", verbose=True)
)
self.assertRaises(
IndexError,
app.get('/').clickbutton, buttonid='button2'
Expand Down
6 changes: 4 additions & 2 deletions webtest/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,18 @@ def click(self, description=None, linkid=None, href=None,
return self.goto(str(found_attrs['uri']), extra_environ=extra_environ)

def clickbutton(self, description=None, buttonid=None, href=None,
index=None, verbose=False):
onclick=None, index=None, verbose=False):
"""
Like :meth:`~webtest.response.TestResponse.click`, except looks
for link-like buttons.
This kind of button should look like
``<button onclick="...location.href='url'...">``.
"""
href_extract = re.compile(onclick) if onclick else re.compile(r"location\.href='(.*?)'")

found_html, found_desc, found_attrs = self._find_element(
tag='button', href_attr='onclick',
href_extract=re.compile(r"location\.href='(.*?)'"),
href_extract=href_extract,
content=description,
id=buttonid,
href_pattern=href,
Expand Down

0 comments on commit fb46c6e

Please sign in to comment.