From 6e21e0662ccc8b875a5c1634fcf6c28cb8bfa749 Mon Sep 17 00:00:00 2001 From: Martijn Pieters Date: Fri, 9 Mar 2018 19:52:04 +0000 Subject: [PATCH] Add support for the HTML image input tag --- robobrowser/forms/fields.py | 5 +++++ robobrowser/forms/form.py | 2 ++ tests/test_forms.py | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/robobrowser/forms/fields.py b/robobrowser/forms/fields.py index c6ddeb3..3514600 100644 --- a/robobrowser/forms/fields.py +++ b/robobrowser/forms/fields.py @@ -57,6 +57,11 @@ class Submit(Input): pass +class ImageSubmit(Submit): + def serialize(self): + return {self.name + '.x': '0', self.name + '.y': '0'} + + class FileInput(BaseField): @BaseField.value.setter diff --git a/robobrowser/forms/form.py b/robobrowser/forms/form.py index 00b7222..43b63b4 100644 --- a/robobrowser/forms/form.py +++ b/robobrowser/forms/form.py @@ -44,6 +44,8 @@ def _parse_field(tag, tags): tag_type = tag.get('type', '').lower() if tag_type == 'submit': return fields.Submit(tag) + if tag_type == 'image': + return fields.ImageSubmit(tag) if tag_type == 'file': return fields.FileInput(tag) if tag_type == 'radio': diff --git a/tests/test_forms.py b/tests/test_forms.py index 51128a8..d408f3e 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -253,6 +253,14 @@ def test_parse_select_multi(self): assert_equal(len(_fields), 1) assert_true(isinstance(_fields[0], fields.MultiSelect)) + def test_parse_image_submit(self): + html = ''' + + ''' + _fields = _parse_fields(BeautifulSoup(html)) + assert_equal(len(_fields), 1) + assert_true(isinstance(_fields[0], fields.ImageSubmit)) + class TestInput(unittest.TestCase): @@ -616,6 +624,23 @@ def test_serialize(self): ) + +class TestImageSubmit(unittest.TestCase): + + def setUp(self): + self.html = '' + self.input = fields.ImageSubmit(BeautifulSoup(self.html).find('input')) + + def test_name(self): + assert_equal(self.input.name, 'image_submit') + + def test_serialize(self): + assert_equal( + self.input.serialize(), + {'image_submit.x': '0', 'image_submit.y': '0'} + ) + + class TestDisabledValues(unittest.TestCase): def test_input_enabled(self):