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()
    self.assertTrue(browser.get_code() in (200, 201))
    html = browser.get_html()
    self.assertTrue(html.count('slashdot') > 150)
    link = browser.find_link('Science')
    form = browser.get_form(2)
    form.set_value('aardvark', name = 'fhfilter')
    browser.clicked(form, None)
    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.