Using Page Objects to Overcome Protractor's Shortcomings
The Protractor community is getting bigger by the day. At ThoughtWorks we have blogged about ways to get hands-on with Protractor and how to test your AngularJS apps with it. In this blog, let’s look beyond Protractor’s support for writing UI Tests for AngularJS. Let’s examine a few of Protractor’s shortcomings and then understand how they can be overcome with Page Objects.
Protractor has Shortcomings?!
Let us go through this example spec from the Protractor GitHub page:
Although this spec is syntactically perfect and does the right thing, it can cause a few problems:
Lack of Domain Specific Language (DSL) Support: It is hard to understand what is being tested, because the Protractor specific vocabulary ( element, by.binding, by.model, etc.) is not related to the business and features. Having test specifications that speak the jargon of the domain goes a long way in helping business understand the rationale behind the tests.
Code Duplication: Following are the steps to add a new Todo function:
- Get the addTodo input by model
- Get the addButton button by css
- Write something on the input using sendKeys
- Click on the button to confirm.
High Coupling: What would happen if the input field where you type a new todo has to be changed? What if it was required to click a button before being able to enter a new todo? Such changes (which may seem unlikely at the start) end up happening in every project. It is essential to not be highly coupled, but instead be flexible enough to be ready for change.
Maintenance: All these issues above will make your awesome Protractor tests a huge pain to maintain in a medium/long term. As the project grows and change creeps in, nobody will be keen on changing the numerous ‘Adding todo logic’ spread across your code.
As you can see, even a very small application example like the Todo one above is likely to suffer from issues. These problems get magnified as the complexity of your application increases.
How PageObjects can help
Firstly, what are Page Objects? Here is a definition from the Selenium team:
Page Object is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT. The tests then use the methods of this page object class whenever they need to interact with that page of the UI. The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change. Subsequently all changes to support that new UI are located in one place.
How can PageObjects help? Let’s take a look at how we can refactor the same example above using Page Objects:
Refactored Spec - Gist
Notice that the much of the code is contained inside the AngularPage. The idea is to move all the logic required to interact with the page inside this class.
Our suite now is more focused on the behaviour of the page, than on how select this or that element.
Now let’s take a look at how the page object looks like.
The Page Object - Gist
We’ll now revisit the shortcomings I had listed earlier and see how the refactored Page Objects version helped overcome them:
Lack of Domain Language Support: Notice that all the Protractor specific words are gone in the refactored version and you have more business-like expressions instead. That is much easier to read, especially by business folks, thus helping them better understand the test specifications.
Code Duplication: The lines of code that were likely to be copied and pasted are now inside the Page Object and we just call this Page Object if we need to execute them.
High Coupling: Since the tests only talk to the Page Object, it is totally decoupled from the elements inside each page. This means that if we need to change something on the UI the tests will stay pretty much the same. With Page Object we have the right abstraction of the page elements and their actions.
Maintenance: The effort to maintain the specs should be smaller since we have encapsulated the implementation inside the page objects.
I hope you are curious to try it out. If you do so, please come back and share your learnings.
- Selenium’s comprehensive overview of Page Objects
- Page Object - Martin Fowler
- Domain Specific Language - Martin Fowler
- Don’t Repeat Yourself - Dave Thomas, interviewed by Bill Venners
- Cohesion And Coupling - Jeremy Miller
Cross posted at ThoughtWorks Insights