Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix button in form issue #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
coverage
coveralls
docutils
flake8
flake8==2.6.2
mock
nose
sphinx
Expand Down
3 changes: 3 additions & 0 deletions robobrowser/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Submit(Input):
pass


class Button(Input):
pass

class FileInput(BaseField):

@BaseField.value.setter
Expand Down
7 changes: 6 additions & 1 deletion robobrowser/forms/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .. import exceptions


_tags = ['input', 'textarea', 'select']
_tags = ['input', 'button', 'textarea', 'select']
_tag_ptn = re.compile(
'|'.join(_tags),
re.I
Expand Down Expand Up @@ -53,6 +53,11 @@ def _parse_field(tag, tags):
checkboxes = _group_flat_tags(tag, tags)
return fields.Checkbox(checkboxes)
return fields.Input(tag)
if tag_type == 'button':
tag_type = tag.get('type', 'submit').lower()
if tag_type == 'submit':
return fields.Submit(tag)
return fields.Button(tag)
if tag_type == 'textarea':
return fields.Textarea(tag)
if tag_type == 'select':
Expand Down
73 changes: 71 additions & 2 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ def setUp(self):
<input name="multi" value="multi1" />
<input name="multi" value="multi2" />
<input type="submit" name="submit" value="submit" />
<button type="button" name="yes" value="yes" />
</form>
'''
self.form = Form(self.html)

def test_fields(self):
keys = set(('vocals', 'guitar', 'drums', 'bass', 'multi', 'submit'))
keys = set(('vocals', 'guitar', 'drums', 'bass',
'multi', 'submit', 'yes'))
assert_equal(set(self.form.fields.keys()), keys)
assert_equal(set(self.form.keys()), keys)

Expand All @@ -82,7 +84,8 @@ def test_repr(self):
assert_equal(
repr(self.form),
'<RoboForm vocals=, guitar=, drums=roger, bass=, '
'multi=multi1, multi=multi2, submit=submit>'
'multi=multi1, multi=multi2, submit=submit, '
'yes=yes>'
)

def test_repr_empty(self):
Expand Down Expand Up @@ -123,6 +126,8 @@ def setUp(self):
<form>
<input type="submit" name="submit1" value="value1" />
<input type="submit" name="submit2" value="value2" />
<button type="submit" name="submit3" value="value3" />
<button type="submit" name="submit4" value="value4" />
</form>
'''
self.form = Form(self.html)
Expand All @@ -145,6 +150,8 @@ def test_serialize_multi(self):
serialized = self.form.serialize(submit)
assert_equal(serialized.data['submit1'], 'value1')
assert_false('submit2' in serialized.data)
assert_false('submit3' in serialized.data)
assert_false('submit4' in serialized.data)


class TestParser(unittest.TestCase):
Expand All @@ -169,6 +176,12 @@ def test_parse_input(self):
assert_equal(len(_fields), 1)
assert_true(isinstance(_fields[0], fields.Input))

def test_parse_button(self):
html = '<button type="button" name="yes" value="yes" />'
_fields = _parse_fields(BeautifulSoup(html))
assert_equal(len(_fields), 1)
assert_true(isinstance(_fields[0], fields.Button))

def test_parse_file_input(self):
html = '<input name="band" type="file" />'
_fields = _parse_fields(BeautifulSoup(html))
Expand Down Expand Up @@ -300,6 +313,52 @@ def test_serialize(self):
)


class TestButton(unittest.TestCase):

def setUp(self):
self.html = '<button name="brian" value="may" />'
self.input = fields.Input(BeautifulSoup(self.html).find('button'))

def test_name(self):
assert_equal(self.input.name, 'brian')

def test_initial(self):
assert_equal(self.input._value, 'may')
assert_equal(self.input.value, 'may')

def test_value(self):
self.input.value = 'red special'
assert_equal(self.input._value, 'red special')
assert_equal(self.input.value, 'red special')

def test_serialize(self):
assert_equal(
self.input.serialize(),
{'brian': 'may'}
)

def test_invalid_name(self):
html = '<button />'
assert_raises(exceptions.InvalidNameError, lambda: fields.Button(html))


class TestButtonBlank(unittest.TestCase):

def setUp(self):
self.html = '<button name="blank" />'
self.input = fields.Button(BeautifulSoup(self.html).find('button'))

def test_initial(self):
assert_equal(self.input._value, None)
assert_equal(self.input.value, '')

def test_serialize(self):
assert_equal(
self.input.serialize(),
{'blank': ''}
)


class TestTextarea(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -628,6 +687,16 @@ def test_input_disabled(self):
input = fields.Input(BeautifulSoup(html).find('input'))
assert_true(input.disabled)

def test_button_enabled(self):
html = '<button name="brian" value="may" />'
input = fields.Button(BeautifulSoup(html).find('button'))
assert_false(input.disabled)

def test_button_disabled(self):
html = '<button name="brian" value="may" disabled />'
input = fields.Button(BeautifulSoup(html).find('button'))
assert_true(input.disabled)

def test_checkbox_enabled(self):
html = '''
<input type="checkbox" name="member" value="mercury" checked />vocals<br />
Expand Down