When I am teaching Page Objects to people I tend to split things out in terms of Elements and Actions. Simon’s original article on Page Objects speaks in terms of generic services so I’m not sure where/when I started using this distinction.
Elements are things you want to interact with either from putting something or taking something out. For instance, if it is:
- A text box
- A select box
- A check box
- A radio button
- Something that has some ‘interesting’ text
As I implement Page Objects (it’s a pattern, implement as you will), an element is interacted with through overloaded getters and setters. So typing ‘pink rhinoceros’ into a text box is done via the set and using it in an assert is in the get.
An Action is something that is done on the page and results in ‘some thing’. These I implement as methods in the Page Object class and in general these start with ‘do_’ or ‘go_to_’. For example:
Not only does this split make you think about what you are trying to achieve with this particular Page Object, but the implementation style makes for very readable scripts. Not quite as readable than say ones with Cucumber or Robot Framework mind you, but they are missing that extra layer of overhead that you inject when using one of those tools
So there’s another one of my little tricks.