I'm trying to test the functionality of a web app by scripting a login sequence in Python, but I'm having some troubles.
Here's what I need to do:
- Do a POST with a few parameters and headers.
- Follow a redirect
- Retrieve the HTML body.
Now, I'm relatively new to python, but the two things I've tested so far haven't worked. First I used httplib, with putrequest() (passing the parameters within the URL), and putheader(). This didn't seem to follow the redirects.
Then I tried urllib and urllib2, passing both headers and parameters as dicts. This seems to return the login page, instead of the page I'm trying to login to, I guess it's because of lack of cookies or something.
Am I missing something simple?
Thanks.
Focus on
urllib2
for this, it works quite well. Don't mess withhttplib
, it's not the top-level API.What you're noting is that
urllib2
doesn't follow the redirect.You need to fold in an instance of
HTTPRedirectHandler
that will catch and follow the redirects.Further, you may want to subclass the default
HTTPRedirectHandler
to capture information that you'll then check as part of your unit testing.You can then use this
opener
object to POST and GET, handling redirects and cookies properly.You may want to add your own subclass of
HTTPHandler
to capture and log various error codes, also.Besides the fact that you may be missing a cookie, there might be some field(s) in the form that you are not POSTing to the webserver. The best way would be to capture the actual POST from a web browser. You can use LiveHTTPHeaders or WireShark to snoop the traffic and mimic the same behaviour in your script.
Funkload is a great web app testing tool also. It wraps webunit to handle the browser emulation, then gives you both functional and load testing features on top.
I had to do this exact thing myself recently. I only needed classes from the standard library. Here's an excerpt from my code:
Here's my take on this issue.
Try twill - a simple language that allows users to browse the Web from a command-line interface. With twill, you can navigate through Web sites that use forms, cookies, and most standard Web features. More to the point, twill is written in
Python
and has a python API, e.g: