How to use Page Factory in Selenium Page object model

Test Automation frameworks based on the Page object model design pattern and Page Factory in Selenium WebDriver help to keep the locators separate from the methods which access them. Now, this can be achieved in many ways. In this article, we will see how to use to maintain locators in Page Object Model. If you are interested to learn more or just want to refresh your memory, you can see

How to use By class in selenium to store locators.

Page Factory is an inbuilt feature of Selenium WebDriver, which is an implementation of the Page Object Model. It acts like a starting kit for your page objects and initializes the page objects through the command  PageFactory.initElements(driver, this); 

Every page object has this command in its constructor along with the command for setting the driver. this.driver=driver;

To understand more about how to use the Page Factory in selenium, see the code snippet provided below.

Example Test Scenario

In order to understand this concept, we have taken a simple scenario that is scripted in the next example code.

  • Open google search application: URL- www.google.com
  • Enter a keyword on the google search page.
  • Press key Enter
Example Class Using Page Factory in Selenium
package quickstart.Pages;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class GoogleSearchPageFactory  {
    WebDriver driver;
    @FindBy(xpath="//input[@name='q']")
    WebElement searchBox;
    
    @FindBy(xpath="//div[@class='g']//h3/span")
    WebElement firstResult;
    /**
     * Constructor of the Page class to set the driver.
     * It is also used to initialize all the elements.
     * @param driver
     */
    public GoogleSearchPageFactory(WebDriver driver) {
        this.driver=driver;
        PageFactory.initElements(driver, this);
    }
    /**
     * This method is for entering a keyword in an input field on the webpage
     * Notice how the above defined WebElement searchBox is used in place of 
     * driver.findElement(By.xpath(locator))
     * @param keyword A string value to be entered in the field
     */
    public void enterKeyWord(String keyword) {
        
        searchBox.sendKeys(keyword);
        searchBox.sendKeys(Keys.ENTER);
    }
}

Apart from XPath, You can use tagName, partialLinkText, name, linkText, id, CSS, className  as locator types in your page objects with @FindBy. You can use any type of locator and initialize a web element. In your page object, represent every webelement by a name such as searchBox and firstResult in the above example.

Advantages Of Page Factory in Selenium Over Simple POM
  • In simple POM, you need to take care of StaleElementReferecneException with the help of your framework design and coding, while this is taken care of in the PageFactory.
  • It relocates a WebElement on the page every time it has to be used, thus avoiding StaleElementReferecneException .
  • It is more readable and maintenance is effortless.
  • We don’t need to write driver.findElement every time we want to perform some operation on a WebElement.

You may also read


Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts