Python DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Testing Web Apps Effectively with twill
Pages: 1, 2, 3, 4

Retrieving and submitting web forms

Twill is especially good at retrieving and submitting web forms. The form-related feature uses the commands:



  • showforms
  • formvalue <form_id> <name> <value>
  • submit <button_id>
  • formclear <form_id>

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 urlopen.

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 formvalue:

>>> 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

A simple 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.

Enlarging the Horizon

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.

A new framework that is especially interesting is Selenium, which is also useful for testing Plone applications. Selenium is really spectacular, because it is based on JavaScript and it really tests your browser, clicking on links, submitting forms, and opening pop-up windows all in real time. It completely emulates the user experience at the highest possible level. It also gives you all kind of bells and whistles, eye candy, and colored HTML output (which you may or may not like but surely will impress your customer if you are going to demonstrate that the application conforms to the specifications). I cannot render justice to Selenium in a few lines; perhaps I should write an entire article on it, when I find the time. For the moment, I make no promises, and I refer you to the available documentation.

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.



Sponsored by: