Implement Maintainable Test Scripts by Applying Design Patterns

As development teams can deliver new features even faster, we also need to find a way to implement test scripts quicker. Design patterns can help with that as they provide a reusable solution to commonly occurring problems.

What you will learn…

  • Advantages of using patterns
  • Apply Page Object Model
  • Speed-up test script creation

What you should know…

  • Java’s basic syntax

About the Author

Roy De Kleijn is an Independent Test Consultant with a true passion for technical testing. He has a lot of experience in automated testing of web services, performance, and websites. He is always trying to find a way to transfer knowledge to colleagues who are interested in the automated testing of software.
For different clients, he tries to implement robust test automation frameworks, for functional and web services testing. With over ten years of experience, he assists clients to improve and implement test automation solutions with the use of all kinds of tooling.
He speaks at conferences and provides practice oriented Selenium trainings.
Roy de Kleijn blogs at www.rdekleijn.nl and can be followed on twitter @TheWebTester.


Selenium WebDriver is a great tool to automate almost anything happening within a browser. From simple user actions such as navigating through a website or filling out a form to more advanced actions such as read and write session or local storage, executing JavaScript and the creation of cookies.

It is likely that we will end up with a big amount of automated test scripts. Creating Selenium test scripts can result in an unmaintainable project. One of the reasons can be that too much duplicated code is used. Duplicated code can be caused by duplicated functionality and this, in turn, can result in duplicated use of locators. When this is the case, the code and thus the project will become less maintainable: imagine a locator changes, we now have to walk through the entire test code to modify the locators where necessary. By using the page object model, we can make a non-brittle test code and reduce or eliminate duplicated test code. Besides that, use of the page object model not only improves the readability of the code but allows us to create interactive documentation. Last but not least, we can create automated test scripts using less effort (keystrokes).

Disadvantages (when not using the page object model):

  • Code duplication: while creating automated test scripts, we have to repeat a lot of user actions; (for example: login, search, etc.)
  • Readability: we don’t immediately see what a script does if we see a sequence of Selenium WebDriver commands;
  • Synchronization: meaning, the test scripts tries to do something while the web element is not there yet;
  • Maintainability: the ease of extending the test scripts, how well is the code structured;
  • Time spent on creating test scripts: due to the duplication, it takes much longer to create test scripts.

The greatest challenge might be to solve those maintainability and brittleness issues, so we can write automated test scripts more easily and maintain them without having false positives. We can do that by applying some design pattern. Today’s software development methodologies enable us to develop new features very frequently and in a short period. So, we can’t afford duplications and extra time to be spent on fixing recurring issues. This article describes the usage of the commonly used “Page Object Model” pattern. All examples in this article are based on the website: http://demo.selenium-in-action.com

Upgrade to Premium Account to read all articles and publish your own content …


On the web

Selenium in Action website: http://www.selenium-in-action.io
Demo application: http://www.demo.selenium-in-action.io
Git repository: http://www.selenium-in-action.io/java-testng/