Page Factory In Selenium Webdriver: Inbuilt Page Object Model Of Selenium

Hello Folks,

We have seen below topics as of now:

  1. Introduction of Page Object Model
  2. Plain Page Object Model

In this post we will see inbuilt page object model in selenium webdriver.

Selenium webdriver has inbuilt Page Object Model called Page Factory. It provides @FindBy annotation to create object repository.

What is @FindBy annotation?

  • Annotations in java are used to provide additional information.
  • @FindBy annotation is used to locate web element using any locators available in selenium webdriver (id, xpath, css etc).
  • It is used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements.
  • It is used in conjunction with PageFactory this allows users to quickly and easily create PageObjects.

Syntax: You can use @FindBy annotation in two ways:

  1. Direct way:

@FindBy(LocatorStrategy=”Locatorvalue”) WebElement elementName;

Where LocatorStrategy may be any one from Id, name, className, tagName, css, xpath, linkText, partialLinkText.

Example:

@FindBy(id=”someID”)
@FindBy(name=”someName”)
@FindBy(className=”someClassName”)
@FindBy(tagName=”sometagName”)
@FindBy(xpath=”someXpath”)
@FindBy(css=”someCSS”)
@FindBy(linkText=”someLinktext”)
@FindBy(partialLinkText=”somePartialLinktext”)

2. Using “how” and “using”:

@FindBy(how = How.<LocatorStrategy>, using = “<Locatorvalue>”) WebElement elementName;

Example:

@FindBy(how=How.id, using=”someID”)
@FindBy(how=Howname, using=”someName”)
@FindBy(how=HowclassName, using=”someClassName”)
@FindBy(how=HowtagName, using=”sometagName”)
@FindBy(how=Howxpath, using=”someXpath”)
@FindBy(how=Howcss, using=”someCSS”)
@FindBy(how=HowlinkText, using=”someLinktext”)
@FindBy(how=HowpartialLinkText, using=”somePartialLinktext”)

You can use any syntax as per your convenience.

Same way we can use @FindBy annotation for same list of web elements as shown below:

@FindBy(tagName = “a”) List<WebElement> links;
@FindBy(how = How.TAG_NAME, using = “a”) List<WebElement> links;

How to initialize page objects(web elements) defined using @FindBy annotation?

  • We initialize page objects using PageFactory class.  It is factory class to make using Page Objects simpler and easier.
  • It has overloaded static methods called “initElements” which is used to initialize page objects.
  • Overloaded methods are as below:

  • We will see behavior of below methods:
  1. public static <T> T initElements(WebDriver driver, java.lang.Class<T> pageClassToProxy)

2. public static void initElements(WebDriver driver, java.lang.Object page)

  1. It instantiates an instance of the given class/page object, and set a lazy proxy for each of the WebElement and List<WebElement> fields that have been declared.
  2. By default, the element or the list is looked up each and every time a method is called upon it. This is the reason it is called lazy initialization. It looks for the element when a method is called on it. It also helps in handling staleElementReferenceException as it looks up for web element each and every time a method is called upon it. So, if page gets refreshed or reloaded.
  3. We can change the above behavior by mentioning another annotation called CacheLookup as shown below:

            

4. After mentioning  CacheLookup , initElements will cache web element once it is located and will not search again and again when  required.

5.  Suppose you have a page class named HomePage.class, you can use any above method in below ways:

OR

OR

If you try to use first and second way in a constructor you will get exceptions java.lang.reflect.InvocationTargetException and java.lang.StackOverflowError.

6. An exception will be thrown if the class cannot be instantiated.

We will convert example of last post in PageFactory as shown below:

HomePage.java:

LoginPage.java:

TC_001.java:

That’s it guys. Hope, concepts will be clear. It is internal logic of working of PageFactory.

We will see some interesting things about PageFactory in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

4 thoughts on “Page Factory In Selenium Webdriver: Inbuilt Page Object Model Of Selenium

    1. Local driver is assigned to global using this.driver=driver to ensure same driver is used throughout program.
      If you do not use this keyword, local will be used.

Leave a Reply

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