Best practices for implementing Page Object model for pages with many elements

1.2k views Asked by At

I'm implementing Page Object model with Page Object factory and struggling to figure out the best way to handle pages with many elements. Page Object gets ugly when you have several checkboxes and want to create several methods for each of them. Let's say I have a page with checkbox1... checkboxN. For each of those elemements I want to have methods like isEnabled, isDisplayed, isChecked. If I implement it as it's suggested, I'll end up with 3xN methods like isCheckbox1Enabled, etc.

What is the best practice here? Should I provide simple get methods for those elements? Should I create map and single instance of each method like isCheckboxEnabled(String checkboxName)?

I don't like aproach with the map because then I have to know checkbox name inside my test...

Thanks!

1

There are 1 answers

0
alannichols On

The best approach would be to create a base class to inherit (or mix in etc.) that gives you a way to dynamically define methods for your elements when they are declared in your page objects.

I use this approach in calabash + ruby and have a base class I inherit in my page objects that defines a method called 'element' which creates methods for get_text, is_enabled?, enter_text etc. for all the elements I declare. I declare these at the beginning of my page objects and after that I can use the methods in my page objects or in my step definitions.

The syntax may be different in the language you are using but the approach should still work. locator in this case is the calabash query syntax to find the element I want to interact with.

e.g. in my page objects I inherit the method

def self.element(element_name, locator)
  define_method("get_text_#{element_name}") do
  query("#{locator}", :text)[0]
end

and then at the start of the page object I define

element(:username_textbox, "* id:'username'")

and will be able to use the methods it creates e.g.

get_text_username_textbox

A much more mature equivalent for web automation would be the page-object gem https://github.com/cheezy/page-object which does the same sort of thing but defines different sets of methods depending on the element type.