Introduction to Functional Web Testing With Twill & Selenium

Part 1 :: Writing Twill Tests :: Shell mode v. Python mode v. Python Shell mode

Synopsis

There are three primary ways to use twill -- shell, python, python shell -- and each lends itself to some very specific implementation patterns.

Shell mode v. Python mode v. Python Shell mode

Let's talk about the three primary ways you can use twill. First up is shell mode. Twill comes bundled with its own interactive mode that allows you to experiment with twill syntax. Let's try some:

              [terryp@tpmacbook] ~ :: twill-sh

               -= Welcome to twill! =-

              current page:  *empty page* 
              >> go http://127.0.0.1
              ==> at http://127.0.0.1
              current page: http://127.0.0.1
              >> info

              Page information:
                      URL: http://127.0.0.1
                      HTTP code: 200
                      Content type: text/html (HTML)
                      Page title: Introduction to Functional Web Testing With Twill & Selenium

              current page: http://127.0.0.1
              >>
            

Twill's shell is similar to the Python interpreter, but the twill commands can be executed quickly within the shell without a lot of syntactical overhead. I've found this version of twill to be good for prototyping, experimentation, and getting a feel for how twill works and what it can do.

To use it to write tests, though, you're going to want to embed twill commands directly into a Python script. Let's create 'twill_go_info.py':

              import twill.commands as tc

              tc.go("http://127.0.0.1")
              tc.info()
            

And here's the output when we run this file:

              [terryp@tpmacbook] code :: python twill_go_info.py 
              ==> at http://127.0.0.1

              Page information:
                      URL: http://127.0.0.1
                      HTTP code: 200
                      Content type: text/html (HTML)
                      Page title: Introduction to Functional Web Testing With Twill & Selenium
            

You can also use the twill module directly in the Python interpreter, which is one of the best ways to experiment and prototype:

              [terryp@tpmacbook] ~ :: python
              Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27) 
              [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
              Type "help", "copyright", "credits" or "license" for more information.
              >>> import twill.commands as tc
              >>> tc.go("http://127.0.0.1")
              ==> at http://127.0.0.1
              'http://127.0.0.1'
              >>> tc.info()

              Page information:
                      URL: http://127.0.0.1
                      HTTP code: 200
                      Content type: text/html (HTML)
                      Page title: Introduction to Functional Web Testing With Twill & Selenium

              >>>
            

Because you're in the Python interpreter, you've also got access to the rest of the language, and you can play around in a much more broad and useful way.

 

ready to write some tests?

Fluid 960 Grid System, created by Stephen Bau, based on the 960 Grid System by Nathan Smith. Released under the GPL/ MIT Licenses.