Commit 4b955145 authored by Lowik BOURGEOISAT's avatar Lowik BOURGEOISAT Committed by ntome

filters: Add number/range type to FormValue, and tests

parent a1292378
......@@ -17,6 +17,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from decimal import Decimal
import lxml.html as html
from six.moves.html_parser import HTMLParser
......@@ -185,7 +186,16 @@ class FormValue(Filter):
return unicode(el.attrib['value'])
except KeyError:
return self.default_or_raise(AttributeNotFound('Element %s does not have attribute value' % el))
# TODO handle html5 number, datetime, etc.
# numeric input
elif el.attrib.get('type', '') in ('number', 'range'):
try:
if '.' in el.attrib.get('step', ''):
return Decimal(el.attrib['value'])
else:
return int(el.attrib['value'])
except KeyError:
return self.default_or_raise(AttributeNotFound('Element %s does not have attribute value' % el))
else:
raise UnrecognizedElement('Element %s is not recognized' % el)
elif el.tag == 'textarea':
......
......@@ -15,10 +15,12 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from decimal import Decimal
from unittest import TestCase
from lxml.html import fromstring
from weboob.browser.filters.html import Link
from weboob.browser.filters.html import FormValue, Link
from weboob.browser.filters.standard import RawText
......@@ -51,6 +53,29 @@ class RawTextTest(TestCase):
self.assertEqual("229,90 EUR", RawText('//p', default="foo", children=True)(e))
class FormValueTest(TestCase):
def setUp(self):
self.e = fromstring('''
<form>
<input value="bonjour" name="test_text">
<input type="number" value="5" name="test_number1">
<input type="number" step="0.01" value="0.05" name="test_number2">
<input type="checkbox" checked="on" name="test_checkbox1">
<input type="checkbox" name="test_checkbox2">
<input type="date" value="2012-12-12" name="test_date">
<input type="range" value="20" name="test_range">
</form>
''')
def test_value(self):
self.assertEqual('bonjour', FormValue('//form//input[@name="test_text"]')(self.e))
self.assertEqual(5, FormValue('//form//input[@name="test_number1"]')(self.e))
self.assertEqual(Decimal('0.05'), FormValue('//form//input[@name="test_number2"]')(self.e))
self.assertEqual(True, FormValue('//form//input[@name="test_checkbox1"]')(self.e))
self.assertEqual(False, FormValue('//form//input[@name="test_checkbox2"]')(self.e))
self.assertEqual(20, FormValue('//form//input[@name="test_range"]')(self.e))
class LinkTest(TestCase):
def test_link(self):
e = fromstring('<a href="https://www.google.com/">Google</a>')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment