LifeInVistaprint

January 8th, 2015

Unit Testing with Holmium and Page Objects

Author: Sajnikanth Suriyanarayanan

At Cimpress (the parent company of Vistaprint), we believe if you have to do something a second time, it better be automated. This includes testing; teams usually have thousands of unit tests, supported by hundreds of acceptance tests (API and UI) that sufficiently cover the entire code base. Out of these, UI tests have gained notoriety for being fragile, difficult to maintain and frankly being a headache. If you agree with this sentiment and have pulled your hair out when working with UI test suites, read on.

Selenium is a popular choice for automating UI acceptance tests, and scripts are usually written in Python, Ruby or even Java. If the language is Python, one of the most popular testing frameworks to execute these suites is nosetests. Nose offers a brilliant plugin architecture to extend test capabilities, and Holmium is one such plugin.

Holmium makes it easy to manage your selenium test suite and provides a multitude of command line options. Consider the following example to test search-functionality on the Vistaprint web site; save it as vista_test.py:

This can be run with nosetests as:

Here’s what happens:

  • Firefox is launched
  • Vistaprint website is shown
  • Search box is cleared and populated with Business Card
  • Search button is clicked
  • Check if URL contains search-text
  • Check if first result contains search-text
  • Browser is closed

Using Holmium

The above test is quite simple but not very maintainable because the CSS class names might change from release to release. Imagine having a comprehensive test suite with about 100 tests using these elements with the old class names; it can be time-consuming (and painful) to update each and every test!

The trick is to keep concerns separate, and this is where Page Objects help. The upshot is, Page Objects make it easier to maintain tests and reduce duplicate code. Did you also notice the browser is hard-coded in the test? This makes it difficult to reuse the same code for multiple browsers.

Holmium helps create page objects quite easily. Simultaneously, it also supports passing the browser name as a command line argument. Here’s how the above test case can be re-written to use Holmium page objects:

Notice that we have two page objects for the two pages we used in the test, Vistaprint Home page (HomePage) and Search Results page (SearchResultsPage). We define the elements we want to use in the respective pages, so if a class name changes in the future, we just have to update that in the page object.

The setUp method instantiates these page objects. Notice that we can directly navigate to the Vistaprint Home Page during instantiation, and the browser name is no longer hard-coded in the test.

test_search simply refers to the elements within the page objects; no class names here.

We now run the above test like this:

Holmium Config

Next, let’s say this test needs to be run in separate staging and production environments; hard-coding that URL within the test doesn’t help. Luckily, Holmium supports a config file and here’s how to use that:

Save the above as config.py in the same location as vista_test.py. After this, replace the hard-coded URL in the test with self.config['base_url'] so it’s like:

The test can now be run like this:

And thus, the same test can be used for multiple environments, with the URL passed as a command-line option.

Other Holmium Command Line Options:

Refer to the Holmium Documentation for other useful command line options like:

  • User Agent
  • Capabilities
  • Remote Selenium Server URL etc.

Conclusion

The above example illustrates the simplicity of using Holmium Page Objects and also introduces the various command line options available. Page Objects help in keeping page-elements and services-offered-by-a-page away from tests and reduce duplicate code. In case an element definition changes (change of ID / class / name, etc.), a single fix to that page object is all it takes instead of making changes to all the tests.

Holmium also offers amazing parametrization options for browser, test environments, user agent, and more. With these parameters, a single test script can be reused in many ways.

If you are someone with very little or no automation test experience, Holmium could be a great place to start. Happy coding!

Resources

Sajnikanth Suriyanarayanan is a Lead QA Engineer at Cimpress.

Recent Posts

Join Life in Vistaprint Search Jobs