Time for action - using Twill's browser object

Here we see how to access the browser object directly, and use it to interact with the web.

  1. Place the following code into a Python test module:
    from unittest import TestCase
    import twill
    class test_twill_browser(TestCase):
    def test_slashdot(self):
    browser = twill.get_browser()
    browser.go('http://slashdot.org/')
    self.assertTrue(browser.get_code() in (200, 201))
    html = browser.get_html()
    self.assertTrue(html.count('slashdot') > 150)
    link = browser.find_link('Science')
    browser.follow_link(link)
    form = browser.get_form(2)
    form.set_value('aardvark', name = 'fhfilter')
    browser.clicked(form, None)
    browser.submit()
    self.assertEqual(browser.get_code(), 200)
    
  2. Run the test module using nosetests. If Slashdot hasn't ...

Get Python Testing Beginner's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.