Skip to content

Commit

Permalink
Adding support for buttons that live outside of their form, and inste…
Browse files Browse the repository at this point in the history
…ad specify a `form` attribute
  • Loading branch information
theunraveler committed Sep 11, 2020
1 parent 4848636 commit c155f83
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion splinter/driver/lxmldriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,13 @@ def select(self, value):
self._control.value = value

def _get_parent_form(self):
parent_form = next(self._control.iterancestors("form"))
# First, try to find the form by the `form` attribute.
if 'form' in self._control.attrib:
parent_form = self._control.getroottree().xpath(
'//*[@id="%s"][1]' % self._control.attrib['form']
)[0]
else:
parent_form = next(self._control.iterancestors("form"))
return self.parent._forms.setdefault(parent_form._name(), parent_form)


Expand Down
8 changes: 8 additions & 0 deletions tests/form_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ def test_clicking_submit_button_posts_button_value_if_value_present(self):
"submit-button: submit-button-value",
)

def test_submitting_a_form_when_input_is_outside_the_form(self):
self.browser.find_by_css('input[form="form-with-submit-outside"]').click()
assert "submit-input: submit-input-value" == self.browser.find_by_xpath("/descendant-or-self::*").text

def test_submitting_a_form_when_button_is_outside_the_form(self):
self.browser.find_by_css('button[form="form-with-submit-outside"]').click()
assert "submit-button: submit-button-value" == self.browser.find_by_xpath("/descendant-or-self::*").text

def test_submiting_a_form_and_verifying_page_content(self):
self.browser.fill("query", "my name")
self.browser.find_by_name("send").click()
Expand Down
5 changes: 5 additions & 0 deletions tests/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ <h1 id="firstheader">Example Last Header</h1>
<br>
</form>

<form action="/post" method="POST" id="form-with-submit-outside">
</form>
<input type="submit" form="form-with-submit-outside" name="submit-input" value="submit-input-value" />
<button type="submit" form="form-with-submit-outside" name="submit-button" value="submit-button-value"></button>

<a href="http://example.com/">Link for Example.com</a>
<a href="http://example.com/last">Link for Example.com</a>
<a href="http://example.com/">Link for last Example.com</a>
Expand Down

0 comments on commit c155f83

Please sign in to comment.