added dont_click attr to FormRequest

This commit is contained in:
Ismael Carnales 2009-10-29 13:18:13 -02:00
parent 9b5fef4f48
commit a244d23b89
3 changed files with 27 additions and 3 deletions

View File

@ -250,7 +250,7 @@ objects.
The :class:`FormRequest` objects support the following class method in
addition to the standard :class:`Request` methods:
.. classmethod:: FormRequest.from_response(response, [formnumber=0, formdata=None, clickdata=None, ...])
.. classmethod:: FormRequest.from_response(response, [formnumber=0, formdata=None, clickdata=None, dont_click=False, ...])
Returns a new :class:`FormRequest` object with its form field values
pre-populated with those found in the HTML ``<form>`` element contained
@ -276,6 +276,10 @@ objects.
more info.
:type clickdata: dict
:param dont_click: If True the form data will be sumbitted without
clicking in any element.
:type clickdata: bool
The other parameters of this class method are passed directly to the
:class:`FormRequest` constructor.

View File

@ -37,7 +37,8 @@ class FormRequest(Request):
self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
@classmethod
def from_response(cls, response, formnumber=0, formdata=None, clickdata=None, **kwargs):
def from_response(cls, response, formnumber=0, formdata=None,
clickdata=None, dont_click=False, **kwargs):
encoding = getattr(response, 'encoding', 'utf-8')
forms = ParseFile(StringIO(response.body), response.url,
encoding=encoding, backwards_compat=False)
@ -55,6 +56,10 @@ class FormRequest(Request):
for k, v in formdata.iteritems():
for v2 in v if hasattr(v, '__iter__') else [v]:
form.new_control('text', k, {'value': v2})
if not dont_click:
url, body, headers = form.click_request_data(**(clickdata or {}))
else:
url, body, headers = form._switch_click('request_data')
url, body, headers = form.click_request_data(**(clickdata or {}))
return cls(url, method=form.method, body=body, headers=headers, **kwargs)

View File

@ -292,6 +292,21 @@ class FormRequestTest(RequestTest):
self.assertEqual(urlargs['one'], ['1'])
self.assertEqual(urlargs['two'], ['2'])
def test_from_response_dont_click(self):
respbody = """
<form action="get.php" method="GET">
<input type="submit" name="clickeable1" value="clicked1">
<input type="hidden" name="one" value="1">
<input type="hidden" name="two" value="3">
<input type="submit" name="clickeable2" value="clicked2">
</form>
"""
response = Response("http://www.example.com/this/list.html", body=respbody)
r1 = self.request_class.from_response(response, dont_click=True)
urlargs = cgi.parse_qs(urlparse(r1.url).query)
self.assertFalse('clickeable1' in urlargs, urlargs)
self.assertFalse('clickeable2' in urlargs, urlargs)
def test_from_response_errors_noform(self):
respbody = """<html></html>"""
response = Response("http://www.example.com/lala.html", body=respbody)