We all know about Page Object Pattern suggested by the Selenium group. In my earlier blog mentioned here, I had documented the undocumented documentation of Page Object Pattern. In this blog I will talk about few practices while implementing Page Object Pattern that makes your automation better and save you from spending extra time while debugging or while writing new tests.

 

Page Validation

 

This is even suggested by the selenium group in their documentation. But it only suffices one part of automation which is page validation when initializing a page object while writing tests. But there are other scenarios where we may need more than just validation.

Consider the example of a simple Gmail Page. Assume you need to automate opening and closing of the compose section. If we go by the original documentation we can validate the opening of compose page but we won’t be able to verify directly the compose page without giving up on the code readability.

What is better? Waiting for an element not present or Waiting for a page not present on the UI. I assume most of the experienced people will agree to waiting for a page not present on the ui.

To do so simply have a method “isPresent()” on your page classes where ever required and just return true or false based on you specific page presence validation. For ex for my ComposePage class I will validate whether the “to” field is present when I open the compose page. This method can be utilized to check whether a Page is present on the UI or not hence giving more control to the end user who is writing automation tests.

There are certain frameworks that allow you to do such validations out of the box. They are:

 

Using Domain Objects

 

While doing automation there are lot paces where our functional methods or actions takes dome data input some this data can be converted to a domain object. Let’s try to understand it by an example. Consider a functionality where we create users inside the system, while creating user we need to enter user id, first name, last name, email-id and password.

To support the said functionality we will have both kind of methods functional as well as structural ( as mentioned in the blog here) so the methods in our CreateUserPage will be “createUser()” and “enterUserID()”,“enterFirstName()”,“enterlastName()”, etc. These methods takes arguments as string parameters as mentioned below:

 

We can convert such input parameters to domain objects, in this case a UserDetails domain class can be declared and used as mentioned below.

 

Now we can add an extra method “createUser()” which takes the UserDetails class object  to our existing CreateUserFormPage class as shown below.

 

In this way the code is more readable and data can be managed in a better ways using domain objects.