Skip to content

Commit

Permalink
Add support for no button to StatefulBrowser.submit_selected
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreg committed Dec 7, 2022
1 parent 7b1609c commit d5bdc3d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mechanicalsoup/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ def choose_submit(self, submit):
:param submit: The :class:`bs4.element.Tag` (or just its
*name*-attribute) that identifies the submit element to use. If
``None``, will choose the first valid submit element in the form,
if one exists.
if one exists. If ``False``, will not use any submit element;
this is useful for simulating AJAX requests, for example.
To simulate a normal web browser, only one submit element must be
sent. Therefore, this does not need to be called if there is only
Expand Down Expand Up @@ -362,7 +363,7 @@ def choose_submit(self, submit):
# omitted from the submitted form data.
del inp['name']

if not found and submit is not None:
if not found and submit is not None and submit is not False:
raise LinkNotFoundError(
f"Specified submit element not found: {submit}"
)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_stateful_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ def test_submit_btnName(expected_post):
assert initial_state != browser._StatefulBrowser__state


@pytest.mark.parametrize("expected_post", [
pytest.param(
[
('text', 'Setting some text!'),
('comment', 'Selecting an input submit'),
], id='input'),
pytest.param(
[
('text', '= Heading =\n\nNew page here!\n'),
('comment', 'Selecting a button submit'),
], id='button'),
])
def test_submit_no_btn(expected_post):
'''Tests that no submit inputs are posted when btnName=False.'''
browser, url = setup_mock_browser(expected_post=expected_post)
browser.open(url)
browser.select_form('#choose-submit-form')
browser['text'] = dict(expected_post)['text']
browser['comment'] = dict(expected_post)['comment']
initial_state = browser._StatefulBrowser__state
res = browser.submit_selected(btnName=False)
assert res.status_code == 200 and res.text == 'Success!'
assert initial_state != browser._StatefulBrowser__state


def test_submit_dont_modify_kwargs():
"""Test that submit_selected() doesn't modify the caller's passed-in
kwargs, for example when adding a Referer header.
Expand Down

0 comments on commit d5bdc3d

Please sign in to comment.