Twill is especially good at retrieving and submitting web forms. The form-related feature uses the commands:
formvalue <form_id> <name> <value>
Explaining the commands is pretty straightforward.
showforms shows the forms contained in a web page. For example, try:
>>> go http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/widgets >> showforms Form #1 ## __Name______ __Type___ __ID________ __Value__________________ name text (None) password password (None) confirm checkbox (None)  of ['yes'] colour radio (None)  of ['green', 'blue', 'brown', 'ot ... size select (None) ['Medium (10")'] of ['Tiny (4")', 'S ... toppings select (None) ['cheese'] of ['cheese', 'pepperoni' ... time hidden (None) 1118768019.17 1 submit (None) Submit current page: http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/widgets
Notice that twill does a good job of emulating a browser, so it fills the hidden
time widget automatically. The previous code had to fill it explicitly with
Unnamed forms get an ordinal number to use as a form ID in the
formvalue command, which fills a field of the specified form with a given value. You can give many
formvalue commands in succession; if you are a lazy typist, you can also use
fv as an alias for
>>> fv 1 name MICHELES current page: http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/widgets >> fv 1 password SECRET current page: http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/widgets
formclear resets all the fields in a form, and
submit lets you press a Submit button, thus submitting the form:
>>> submit 1 current page: http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/widgets
show will convince you that twill has submitted the form. The best way to understand how it works is just to experiment on your own. The base distribution contains a few examples you can play with.
In this article I have shown two easy ways to test your web application: by hand with urllib or with a simple tool such as twill. Much more is under the sun. There are many sophisticated web testing frameworks out there, including enterprise-oriented ones, with lots of features and difficult learning curves. Here, on purpose, I have decided to start from the small and to discuss the topic from a do-it-yourself angle, because often the simplest things work best. Sometimes you don't need the sophistication; sometimes your preferred testing framework lacks the feature you wish for; and sometimes it is just buggy. If you need something more sophisticated, a great source for everything testing-related is Grig Gheorghiu's blog.
Michele Simionato is employed by Partecs, an open source company headquartered in Rome. He is actively developing web applications in the Zope/Plone framework.
Return to the Python DevCenter.