When using UI automation technologies, changes to the UI can break our tests. For example, if a textbox is being located by its ID in numerous tests, if this ID changes then all these tests will break. They won’t be able to find the ID.
Another problem that can arise with automated UI testing is code duplication. Imagine a system that had an color drop down list and an apply button in multiple places in the application. Each UI test that exercises these color choices must first locate the color choice box, select a color, locate the apply button, and then click it.
The 3 Levels Of User Interface Testing
One way to think of the “evolution” of UI testing is that of: test script, page object model (POM), and logical functional model (LFM).
Test scripts are self contained tests. They contain the whole test from starting the application through to the asserts at the end.
Test scripts can be lengthy as they have to include all the UI control finding and interaction code for every single step of the particular test. This can make them hard to read and understand exactly what behaviour is being tested.
Test scripts also don’t share any code with other tests, so code duplication can create additional maintenance effort over time.
Test scripts are an easy way to get started with automated UI testing, especially if you are learning the UI automation framework at the same time.
Pretty quickly, test scripts become unwieldy and so page object models can be introduced.
Page Object Models
Page object models (POM) represent a screen or page or group of controls in the UI that’s being tested.
For example, a screen with a color chooser could be represented by a POM class that exposes the drop down control and buttons as properties.
These properties encapsulate the locating of the UI dropdown and button (perhaps by ID). The test code itself then uses an instance of the POM to interact with the dropdown and button – it doesn’t have to worry about the UI IDs in the test itself.
There’s a couple of levels of abstraction within POMs. The property that represents a UI control can either expose the underlying automation framework object that represents the control, or a simple type such as string or int.
When first getting started with POMs you might want to go with the former approach until you get the idea of using POMs, though there’s not much additional work to implement the latter option.
By returning simple types rather than automation framework types, the actual test code can become more readable; they also help to abstract away the chosen automation framework.
Logical Functional Models
Logical function models (LFM) are an extension to POMs and may not deserve their own distinction in practice, however the distinction helps us to think how we can further enhance the readability of test code and reduce code duplication.
LFMs add methods to POMs. These methods represent logical actions that the end-user can perform on a given page.
The LFM methods can use the properties already defined on the POM.
For example, the logical user function of “I want to choose a color” can be represented as a single method in the POM, for example: ChooseColor(string color);
Within this single method, the existing POM properties representing the dropdown list of colors and button can be used.
Now, test code can simply write: myScreen.ChooseColor(“red”);
The LFM “style” further increases the ability to discern exactly what behaviour a given test is testing.
Building Page Object Models
When building a POM it’s a good idea to evolve it over time, adding properties and logical functional methods as needed by the tests.
The alternative is to model every control and user interaction on the page before starting writing test that use that POM. This increases the up-front cost of writing automated UI tests, you may not end up interacting with every UI control in your tests so it makes sense to evolve POMs in an iterative fashion.
When choosing a UI automation framework, investigate what built-in support it has for POMs/LFMs. It’s possible to hand-code them from scratch, but a little extra help from our chosen framework helps to reduce the cost of building UI automation tests.
To see POMs/LFMs in action in both WPF and Web UI automation, check out my Building the Right Thing in .NET with TestStack Pluralsight course.