ci coveralls pypi docs license


holmium.core provides utilities for simplifying the creation and maintenance of tests that rely on Selenium.

Nothing beats an example. Conventionally automated tests integrating with python-selenium are written similarly to the following code block (using seleniumhq.org).

import selenium.webdriver
import unittest

class SeleniumHQTest(unittest.TestCase):
    def setUp(self):
        self.driver = selenium.webdriver.Firefox()
        self.url = "http://seleniumhq.org"

    def test_header_links(self):
        elements = self.driver.find_elements_by_css_selector("nav>a")
        self.assertTrue(len(elements) > 0)
        for element in elements:
        expected_link_list = ["Projects", "Downloads", "Documentation",
                              "Support", "Blog"]
        actual_link_list = [el.text for el in elements]
        self.assertEquals(sorted(expected_link_list), sorted(actual_link_list))

    def test_projects_selenium_heading(self):
        projects_link = self.driver.find_element_by_css_selector(
        heading = self.driver.find_element_by_css_selector("section.hero>h1")
        self.assertEquals(heading.text, "Selenium Projects")

    def tearDown(self):
        if self.driver:

if __name__ == "__main__":

The above example does what most selenium tests do:

  • initialize a webdriver upon setUp
  • query for one or more web elements using either class name, id, css_selector or xpath
  • assert on the number of occurrences / value of certain elements.
  • tear down the webdriver after each test case

It suffers from the typical web development problem of coupling the test case with the HTML plumbing of the page its testing rather than the functionality its meant to exercise. The concept of PageObjects reduces this coupling and allow for test authors to separate the layout of the page under test and the functional behavior being tested. This separation also results in more maintainable test code (i.e. if an element name changes - all tests don’t have to be updated, just the PageObject).

Lets take the above test case for a spin with holmium. Take note of the following:

  • The initialization and reset of the webdriver is delegated to the TestCase base class (alternatively the class could subclass unittest.TestCase and be run with the holmium nose plugin).
  • the page elements are accessed in the test only via Element & ElementMap.
from holmium.core import TestCase, Page, Element, Locators, ElementMap
import unittest

class SeleniumHQPage(Page):
    nav_links = ElementMap(Locators.CSS_SELECTOR, "nav>a")
    header_text = Element(Locators.CSS_SELECTOR, "section.hero>h1")

class SeleniumHQTest(TestCase):
    def setUp(self):
        self.page = SeleniumHQPage(self.driver, "http://seleniumhq.org")

    def test_header_links(self):
        self.assertTrue(len(self.page.nav_links) > 0)
                ["Projects", "Downloads", "Documentation", "Support", "Blog"]
            , sorted(self.page.nav_links.keys()))

    def test_projects_selenium_heading(self):
            "Selenium Projects"

if __name__ == "__main__":

Which can then be executed in a few different ways as shown below.

# if using TestCase as the base class run as:
HO_BROWSER=firefox nosetests test_selenium_hq.py
# or..
HO_BROWSER=firefox python test_selenium_hq.py
# if using unittest.TestCase as the base class run as:
nosetests test_selenium_hq.py --with-holmium --holmium-browser=firefox

Feature Summary

  • Automatic provisioning and configuration of webdriver instances based either on environment variables or nosetest arguments. (Unit test integration)
  • Shorthand assertions for web pages (TestCase)
  • Declarative model for defining pages, sections, page elements and element collections (Page Objects)
  • Built in cucumber step definitions for accessing and navigating pages (Cucumber Features)