This post is intended to bond all together in a complete guide how to do write better automation code using different design patterns. Generally, automation project consists of two parts. Automation framework project and tests project. Current guide is intended to describe how to build your automation testing framework. How to structure your tests is a different topic. Remember once having correctly designed framework then tests will be much more clean, maintainable and easy to write.
I will show how to build automation framework using most popular design patterns: Page Object, Web Driver Factory, Facade, Null Object, Singleton Design Patterns
Using Page objects Design Pattern while building automation:
Everything starts by defining proper page objects. There is no fixed recipe for this. It all depends on the structure of application under test. The general rule is that repeating elements (header, footer, menu, widget, etc) are extracted as separate objects. The whole idea is to have one element defined in only one place (stay DRY)! Below is our HomePage object. What you can do generally is make search and clear search terms.
Using WebDriver factory Design Pattern while building automation:
WebDriver factory will be responsible for instantiating the WebDriver based on a condition which browser we want to run our tests with.
The constructor takes an argument browser type. Browser type is defined as an enumeration. This is very important. Avoid passing back and forth strings. Always stick to enums or special purpose classes. This will save you time investigating bugs in your automation.
public enum Browsers
Chrome, IE, Firefox
Using NullWebElement Design Pattern while building automation:
This is null object pattern and implements IWebElement. There is a NULL property that is used to compare is given element is not found or no.
Using WebDriver facade Design Pattern while building automation:
WebDriver facade main responsibility is to define custom behavior on elements location. This gives you centralized control over elements location. The constructor takes browser type and uses the factory to create WebDriver instance which is used internally in the facade. FindElement method defines explicit wait. If the element is not found then NullWebElement which is actual implementation of Null object pattern. The idea is to safely locate elements with try/catch and then just use them skipping checks for null.
Tests Creation and calling all design patterns as developed
As I mentioned initially this post is about using efficiently design patterns in your framework automation project. Tests design are not being discussed here. Once you have designed the framework one simple test (without asserts) that makes search will look like the code below.
Design patterns are enabling you to write maintainable code. They increase the value of your code. As shown in this series of posts they are not so complicated and you can easily adopt them in your automation. Most important is design patterns increase your value as a professional. So make the time to learn them!
Building better QA for tomorrow