Twill: The Command Line Web Browser

I’ve had Twill on my list of technologies to look at for a couple of years now. I finally had the chance to use it today and have been mightily impressed so want to share my thoughts.

For those not aware, Twill is a scripting language implemented in Python for web browsing. Using the command line or scripts, you can interactively or programmatically access websites.

Now, my particular circumstances were that I wanted to start writing functional tests for a web application I am developing in Django with a FluidDB backend. Although Django provides superb additions to the standard Python unittest, you’re still writing code that is relatively low-level. I wanted to step back from the Python implementation and think of tests at a higher level, thinking in terms of user functionality rather than objects, methods and functions. I wanted to express my tests in a more high-level way, a more domain-specific way.

Twill’s core language is simple and readable, which is just what I wanted. It can be easily hooked into Python and unittest if I desired that functionality in the future.

Let’s say I have a Django development server running on my local machine and I want Twill to go to a particular URL and check I have an HTTP status returned of 200:

go http://127.0.0.1:8000/search/genre
code 200

That’s it: clear and readable. If the status isn’t 200, Twill will raise an error and report the code it actually received.

One thing I need to do with my tests is check for the presence of certain HTML elements, to ensure things like forms and headings are being set correctly:

find '<h2 class="heading">Jazz</h2>'

Here I’m checking that I have the heading Jazz present in the current URL loaded. Again, if this condition is not met, Twill will raise an appropriate error message.

Am I sure a valid form submission is working correctly and redirecting me to the right URL? If I set the genre field of the form named search_form to the value “grindcore”, and then submit the form I can then verify the returned URL is what I expect it to be:

formvalue search_form genre grindcore
submit
code 200
url http://127.0.0.1:8000/genre/grindcore

The 200 is possibly superfluous, but it adds a little extra confidence that things are working okay.

I’m not one for things which bring in lots of dependencies, provide a high barrier to entry or get in the way of the task at hand. Twill is painless to install, quick to learn and enables readable web tests to be written quickly and easily using just a plain old text editor. It might lack the functionality of tools like Selenium, but the Python API provides an easy way to extend its capabilities if desired.

Go take a look: http://twill.idyll.org/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s