7K Views

Page object Model and Page factory in Selenium

Updated 20 June 2023

Facebook Linkedin

As we know that in creating of selenium test cases can result in an un-maintainable project. one of reason is that too many duplicated codes used. Duplicate code could be caused by duplicated functionality and this will result in duplicate use of locators. The disadvantage of duplicated code is that the project is less maintainable. If some locator will get change we have to walk through whole code to adjust locator where necessary. By using page object pattern using Pagefactory we can reduce duplicate test code, and also it improve readability of test code. Implementation of page object model created by writing seperate  test object and test script.

Advantage of  using Page Object Pattern

  • Easy to maintain Test scripts
  • It improve readability of scripts
  • Re-usability of code
  • Reduce or Eliminate duplicacy

How to implement Page Object Model

There are two different ways of implementing pom:

1) Regular java classes: Please visit Page Object Model

2) Page Factory class:  Please follow  the article

Selenium PageFactory

Page factory will initialiaze every WebElement variable with refrence to element on webpage based on “locators” defined. This is done by using @FindBy Annotiations

Annotations?

In PageFactory , Annotations are used to give names for Webelements to improve code readability. And Annotation @FindBy is used to identify WebElement in WebPage.

By Default PageFactory will search for elements on the page with matching id attribute, if that fails then it will search webelement by name attribute.

The @FindBy annotation supports all other locators strategy that we use:

id, name, className, css, xpath, tagName, linkText.

In PageFactory we use Annotations and it is like

@FindBy(id = “Username”)

public WebElement EnterUsername;

As i told above that PageFactory contains all the elements of webpage at start when we initalize any page class objects. In PageFactory we call a method on the webelement, the driver will go and find it on the current once again.

 

Divide the single PageFactory Objects in to different Page Classes

1. Create a ‘New Package‘ file and name it as ‘pageObjects’, by right click on the Project and select New > Package.

2. Create  ‘New class‘ file for Home Page & LogIn Page and  select New > Class. In our case it is Home Page and LogIn Page.

Home Page Class

As in below code i explain  in Shopify Store how customer will create ticket regarding his query related to any products.

LogIn Page Class

Test Case for creating ticket  customer  in shopify store

The below class consist a Test scenarios of create ticket by customer in shopify store.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.