7

My page object model class is getting rather long.

Should I create a different class with the same name as the page object model with a 2 added at the end. Or does this go against the page object model?

  • it depends on opinion, taste, and circumstances, no objective answer can be given, IMHO – Peter M. Sep 25 '15 at 16:06
  • The application only has one page? I would expect each page only to have a couple of items and some actions, maybe you can explain why you put everything into a single class. – Niels van Reijmersdal Sep 26 '15 at 7:52
  • @NielsvanReijmersdal Not sure what you meant by that? Suppose I have a class for my homepage, then everything on that homepage will be in our homepage class. – Robben Sep 28 '15 at 15:58
  • I guess I mean I do not understand why your page has so much elements and actions. A page has navigation, some content and maybe a form or two. You are splitting classes for each page in the navigation and such? :) – Niels van Reijmersdal Sep 28 '15 at 19:42
  • @NielsvanReijmersdal It's long because we write methods which corresponds to each particular stories. Maybe we should use more generic methods. – Robben Sep 29 '15 at 16:01
9

No. Do not just split your pageObject into pageObject2, 3, 4, etc. This goes for all programming - split your classes logically by function, not arbitrarily!

Your page object should be the source of truth for that specific page. When you start trying to interact with your page, does it really make sense that you should search between MyForm.java and MyForm2.java when trying to find a submit button?

Note that Martin Fowler, one of the popularisers of the PageObject pattern, states that Page Objects do not need to be full webpages. They simply just link together related elements:

"There's an argument here that the name "page object" is misleading because it makes you think you should have just one page object per page. Something like "panel object" would be better - but the term "page object" is what's become accepted. Another illustration of why naming is one of the TwoHardThings."

So, you can break down your pageObject into discrete, reusable chunks.

For example, on a project I worked on, we had a similar widget for search-as-you-type that was used on a number of pages. There were a number of different elements to interact with and methods that could be used against this single small part of the page (enter search, select first answer, count number of answers.. etc). This search element appeared on quite a few pages. Instead of repeating the exact same code on different pages, we created a separate class for this object and included that as a public object on every PageObject it appeared on. Calling a search on a page was as easy as doing:

myDashboardPage.searchWidget.enterSearch("search text");
myUserPage.searchWidget.selectFirstAnswer();

With no duplication of code on the pageObjects, and a logical hierarchy of pages -> widget -> html elements.

5

I think that you should create a page object class for each page of the site.

If you have a single page site, you can create page widgets instead of page objects.

The page widgets will correspond to components of the web page (example: footer, header, etc).

When the number of your page object classes increases, you should create a base class that all page object classes will inherit from.

This will give you 3 layers:

  1. test scripts: use the page objects
  2. page objects: use the base class for interacting with the site
  3. base class

See on this article a sample project about how to create page objects: http://test-able.blogspot.com/2015/09/how-to-write-better-automation-code-with-TDD.html

Alex

  • You are welcome, Robben. If you need help doing it, please contact me. You can find the contact details on my blog. – Alex Siminiuc Sep 30 '15 at 16:43
  • we faced this decision this week and my choice was one page object for all pages. My pairs did not convince me that maintaining the above structure added enough value or that new folks would be able to follow and apply it immediately and correctly. It's comforting to create structure but look carefully at what value it adds. – Michael Durrant Nov 7 '15 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.