Sunday, July 25, 2010

Pages, Generators, & Validators

We have been working on a framework that leverages RSpec, Selenium GRID, and Team City.  As we wrote code for various projects we settled on the Page Object Pattern.  I am not going to provide code for this post but I would like to share a few definitions.

Pages -  A ruby class that contains the locators and custom methods relative to a single web page.  The class is named similar to the page title or the common term used by the team to identify the page.  The class is initialized with locators which are either XPath or CSS that map to the objects on a page.  The locators are set as attribute accessors.  Also included in this class are any custom methods that add efficiencies to navigating through the page objects.

Generators - A method or a series of methods that can grab and format the objects from a page.  One approach is to simply copy the page source and save it as an html file, which can subsequently be parsed using Nokogiri.  This approach may be necessary if you cannot readily navigate to a page due to SSL or some other restrictions.  The second approach is to navigate directly to the target page and get the source html.  Once you store the source html parse the html using Nokogiri.  The simplest form of a generator locates all of the ids on a page and formats the ids into a standard ruby format.  Here is one example for a link.

@location_link = "css=li.'location-link' a"

A generator can generate the locators names and associated css styles for links, radio buttons, buttons, check boxes, and select boxes.  All of the magic happens using Nokogiri to parse the HTML and format the locators.

Validators - This is a RSpec file that confirms the accuracy of the locators that are made by the generators.  A good validator can navigate to a page and execute browser.element? on all locators in the page class.  Any missing locators will be identified.  Validators are very helpful as we do test driven development, but they are extremely important if you have a rapidly changing UI.  You can rebuild the page files as frequently as needed.  Your automated tests key off these locators so keeping the locators current keep you tests current.

This has been a very effective pattern for our framework.  There are two important elements.  The first is a well constructed UI.  The second is Nokogiri.  Hopefully in my next post I will provide some real and usable code.

Happy Testing!