Anton Angelov: Design Patterns in Automation Testing
I have written a bit in this blog about Software Design Patterns such as:
Anton Angelov, author of the blog "Automate the Planet", goes far beyond that.
This way, you can separate how you interact with the Document Object Model (the Page Object) with the tests themselves (The Test Class).
For locating the web elements, Anton gives two examples of the Page Object, one using PageFactory and one not using the built-in Selenium module, Page Factory.
For the second example, instead of PageFactory it uses:
There does seem to be some discussion in the comments section with this second method, if asserts, such as assertTrue in JUnit or TestNG, should be placed only in the test, not the page object itself.
Now that Anton developed this new class, the earlier work can be refactored.
With the Singleton Design Pattern, Anton goes into topics to use with the Base page class such as:
Other patterns discussed in his series:
- Their early history with Kent Beck introducing them to OOPSLA (Object-Oriented Programming, Systems, Languages & Applications) back in 1987 and their popularization with Eric Gamma, Richard Helm, Ralph Johnson and John Vlissides (The "Gang of Four") book, Design Patterns: Elements of Reusable Object-Oriented Software (1994).
- A Boston Software Craftmanship Meetup I attended back in June 2015 on "How to Study Design Patterns".
- As part of "Testing The-Internet" series, I talk about how Page Objects are used at my workplace.
Anton Angelov, author of the blog "Automate the Planet", goes far beyond that.
Design Patterns in Automation Testing
Anton Angelov, an automation developer, has been working on a series of posts called Design Patterns in Automation Testing since April. Using Selenium WebDriver and C# he presents the idea of incorporating these design patterns into automated tests.Page Object Pattern
With the Page Object Pattern, all elements, actions and validations happening on a page is contained in one single object. Anton uses the Bing search http://www.bing.com/ page as an example. Web Elements such as the SearchBox, GoButton, ResultsCount and actions such as Navigate, Search, and ValidateResults can all be stored in a class called BingMainPage.This way, you can separate how you interact with the Document Object Model (the Page Object) with the tests themselves (The Test Class).
For locating the web elements, Anton gives two examples of the Page Object, one using PageFactory and one not using the built-in Selenium module, Page Factory.
For the second example, instead of PageFactory it uses:
- "Page Object Element Map – Contains all element properties and their location logic.
- "Page Object Validator – Consists of the validations that will be performed on the page.
- "Page Object (BingMainPage)- Holds the actions that can be performed on the page like Search and Navigate. Exposes an easy access to the Page Validator through the Validate() method. The best implementations of the pattern hide the usage of the Element Map, wrapping it through all action methods.
- "UI Tests (BingTests) – This class contains a group of tests related to the above page; it can hold only a single instance of the page object".
There does seem to be some discussion in the comments section with this second method, if asserts, such as assertTrue in JUnit or TestNG, should be placed only in the test, not the page object itself.
Advanced Page Object Patterns
In order to follow the object oriented principle of DRY (Don't Repeat Yourself), this blog post walks the automater through creating a Driver class which has the static methods StartBrowser, StopBrowser, and BrowserWait.Now that Anton developed this new class, the earlier work can be refactored.
Facade Design Pattern
To make a purchase, Anton created a class, PurchaseFacade which groups together the page objects for Checkout, ItemPages, ShippingAdderess, and SignIn.Singleton Design Pattern
With the Singleton Design Pattern, Anton goes into topics to use with the Base page class such as:
- Non-thread-safe BaseSingleton Class
- Thread-safe BaseSingleton Class with Lock
- Thread-safe BaseSingleton Class with Lazy
- Thread-safe BaseSingleton Class with Nested Classes
Fluent Page Object Pattern
With this pattern, Anton chains methods, so a test can look like:public void SearchForImageFluent() { P.BingMainPage.Instance .Navigate() .Search("facebook") .ClickImages() .SetSize(Sizes.Large) .SetColor(Colors.BlackWhite) .SetTypes(Types.Clipart) .SetPeople(People.All) .SetDate(Dates.PastYear) .SetLicense(Licenses.All); }
Other patterns discussed in his series:
- IoC Container and Page Objects
- Strategy Design Pattern
- Advanced Strategy Design Pattern
- Observer Design Pattern
- Observer Design Pattern via Events and Delegates
- Observer Design Pattern via IObservable and IObserver
... Not that I understand everything that is being talked about in these articles, I do think they are a good read.
-T.J. Maher
Sr. QA Engineer, Fitbit
// Manual tester, 15 years
// Automated tester for [ 5 ] months and counting
Please note: 'Adventures in Automation' is a personal blog about automated testing. It is not an official blog of Fitbit.com.
-T.J. Maher
Sr. QA Engineer, Fitbit
// Manual tester, 15 years
// Automated tester for [ 5 ] months and counting
Please note: 'Adventures in Automation' is a personal blog about automated testing. It is not an official blog of Fitbit.com.