Don't let the Lockdown slow you Down - Enroll Now and Get 3 Course at 24,999/- Only. Explore Now!

General

Page Object Model in Selenium With Page Factory

Page Object Model in Selenium With Page Factory

Before, going through PageObjectModel(POM) and PageFactory, Let’s see how would a normal selenium WebDriver Scripts look like,

Selenium Webdriver Script

If you notice the highlighted lines in the above image, you could see the Identifiers are described in the Scripts.

Do you think??? Describing as such is advisable???
Do you think??? Will it be effective.  Definitely not.

How this would be the trouble?

If you have to maintain 10K lines or more, this would be a tedious process. And there can be a chance for error too. To avoid those we can have page Factory and POM implemented in our project. This would ease our effort to maintain Object properties.

Page Object Model

 As the name suggests, Each of our pages can be considered as a java class, wherein I can have the elements(object properties) stored. This class can be used in our test scripts to identify objects. Moreover, this looks like Divide and conquer algorithm. Let’s have this Page object model with an Example

Register

For the above registration, we can have a class to be written with which has the object properties stored as Instance Variables and Methods associated with the page, which would like below.

Variables and Methods

The Black Box as above is the object Properties stored as Instance Variables And a method(DialCodeAttr) that is associated with that particular page is also given inside that class. Additionally, did you notice the above code? A Constructor is defined to initialize a reference to the receiving WebDriver instance. Now the above can be implemented  like,

Creating an Instance of the Registration Class:

POM_Class_Registartion obj=new POM_Class_Registartion(driver);

Now we can utilize the objects of the webpage using the Instance variable.

driver.findElement(obj.Fname).sendKeys(“besantTech”);

Explanation of the above Example:

Where obj(Object)  is an Instance of the class (POM_Class_Registartion).

Now Obj will be having access to the Instance Variables (nothing but the elements), So using this We can have code structured and less effort for Maintenance.

Page factory

Page factory is another advanced Concept implemented with POM. We have two important things to be added with POM which makes it be a page factory.

  • @FindBy annotation
  • initElements(driverInstance,this)

The Instance Variables that we created in the above examples can be surrounded by the @FindBy annotation. This Annotation helps with an efficient way to capture objects.

And the other line “PageFactory.initElements(arg1,arg2)” will first initialize all the objects(instance variables) that we have written inside the class. There is an additional feature available in PageFactory.Instead of using “PageFactory.initElements(arg1,arg2)” to load all the elements in a webpage . We can load the required elements only whenever it’s used/needed. This can be achieved by using the below method,

PageFactory.initElements(new AjaxElementLocatorFactory(driver,TimeOutValue) , this)

Page Factory Example
Above highlighted which is a constructor, which in turn would call an object of AjaxElementFactory for loading the objects. There is an inbuilt capability for this method to load only when the object is called/used.This makes the most efficient way to use the framework. Hereby we have discussed the ways about implementing PageObjectModel and PageFactory in our project.

Related Blogs

  1. What is Selenium?
  2. Selenium Career Opportunities
  3. RPA Vs Selenium
  4. Importance of Selenium with Software Testing
  5. Why Selenium is So Famous Recent Days?
  6. Locators in Selenium
  7. How to Take Screenshot in Selenium WebDriver
  8. Create a Selenium Maven Project with Eclipse
  9. Robot Class in Selenium WebDriver
Besant Technologies WhatsApp