Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Pattern for Functional Testing

Regular Selenium tests (in Java) might look like:"/login");
selenium.type("id=username", "bob");
selenium.type("id=password", "password");"Login");
selenium.waitForPageToLoad();"My Account");
assertEquals("bob", selenium.getText("//table[2]/tr[3]/td[2]/");

After a few tests, this kind of thing becomes painful to manage. The typical solution is to create a bunch of constants for IDs and Xpaths, but that doesn’t help too much.

Fellow Pivot Mike Grafton came up with a cool pattern for improving on this. The idea is to create a class representing each page of your web app. Each class contains two types of methods: a bunch of action methods (clickMyAccountLink(), typeUsername()), and a bunch of inspection commands (isLoginButtonEnabled(), getLoggedInUsername()).

When an action takes you to a new page, the corresponding action method returns a new class representing that page. When it stays on the same page, the method just returns “this”. This allows methods to be chained to make the tests more readable.

Here’s how that test would look using this new pattern:

MyAccountPage myAccountPage = new LoginPage(selenium)

assertEquals("bob", myAccountPage.getLoggedInUsername());

The constructor of each page class should validate that it’s on the correct page (waiting if necessary, and perhaps asserting on the page title).

  1. Luke Melia says:

    Ouch. This is crying for webrat.

  2. Russ says:

    It needs a cool name! Like “Functional Mirror Pattern”

  3. Parker Thompson says:

    @luke: the syntax may be nice, but webrat doesn’t solve the same problem as selenium. We tend to rely on selenium because we want to prove that it works given (or in spite of) the browser’s interpretation of the html/js. Often this really matters, especially if your site has any dom-modding js.

  4. Dave Smith says:

    @Wes Sadly, “Logical Functional Model” has a ring to it like a lead bell.

  5. Josh Knowles says:

    As Luke mentioned this is a perfect use case of Webrat. In fact as of a month or so back you can leverage Webrat to drive Selenium.

    At weplay we have Cucumber features which drive our integration tests via Webrat & Selenium and it works great.


  6. Erik Hanson says:

    Josh & Luke: This pattern could definitely be applied to webrat or any other similar tool. My example was in Java and Selenium because that’s where I’ve used this pattern the most. Recently in fact we used this pattern to control an Adobe AIR app.

  7. kasino888 says:

    Functional debugging of application specific integrated circuits (ASICs) has been recognized as a very labor-intensive and expensive process. It often dominates the time and cost of the ASIC system development.Functional specification of the design is logically partitioned into linear and nonlinear components. This is is one of those practices that has not caught on as much as other technical practices in the Agile community.Thanks for the update!!

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *

Share This