Introduction to Functional Web Testing With Twill & Selenium

Part 1 :: Extra Time :: Page Objects

Synopsis

The concept of page objects isn't our original idea. We're very happy to give Simon Stewart, creator of Web Driver, all of the credit for planting the seed in our collective heads at the 2007 Google Test Automation Conference in New York City. You can even watch the presentation on YouTube.

Page Objects

Right, so it's Simon's idea, so check out what are page objects? on the Web Driver site:

Within your web app's UI there are areas that your tests interact with. A Page Object simply models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix need only be applied in one place.

I was really inspired when Simon riffed on page objects, because my early test suites were so poorly organized that when our sites changed, it often required weeks of refactoring to account for the new functionality. It should have only taken a few days, and using Page Objects is a big step in that direction, because they abstract out as much reused functionality as possible into a single class. That way, when your site changes -- AND YOUR SITE WILL CHANGE -- you can account for it in one place in your test suite, instead of all over the place like I had to do. Here's a very simple example:

              from twill.commands import *
              from twill import get_browser

              class Page(object):
                  def __init__(self):
                      self.html = self.__get_html()
                      self.title = self.__get_title()


                  def __get_html(self):
                      html = get_browser().get_html()
                      return html


                  def __get_title(self):
                      title = get_browser().get_title()
                      return title

            

While it might not look like much, when leveraged inside of your test suite it becomes your primary way of interacting with the pages on your site.

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