Page Factory in Selenium: Everything You Need to Know

The end goal of test automation is to ease the process of testing web pages. If test automation isn't written…

By Erin,

The end goal of test automation is to ease the process of testing web pages. If test automation isn’t written in a well-structured manner, then the testing process becomes ambiguous and ineffective. In Selenium, Page Factory optimizes test instances created using the page object model (POM). The POM ensures that tests are written in a readable and maintainable form. This page object makes it easier to see what the code is trying to do at a glance, and Page Factory initializes this process. 

In this article, we’ll learn about the use of Page Factory. We’ll also learn how to implement it within a project.

What is Page Factory?

Page Factory is a factory class in Selenium for implementing the Page Object Model. It’s an optimized version of the POM for Selenium WebDriver, which follows the separation techniques for handling the object repositories and the test cases. It’s a class that extends from the web driver classes. It allows annotations to build up a set of web elements. This makes it faster to create initial page objects.

Let’s look at a sample code block written in Java using the POM in Page Factory. It shows a class that has been created for the home page of a website. An annotation FindBy exists within the class. We’ll go over it in detail later in this article, but it’s used to locate elements on the web page.

To further understand Page Factory, let’s look at how it differs from Page Object Models.

What Is the Difference Between Page Factory and the Page Object Model?

The Page Object Model is a design pattern used to create an object repository for the web elements accessible on a web page. It uses a By annotation to describe page objects, and every object needs to be initialized. POM also has cache storage.

Page Factory, on the other hand, is a class that simply implements the POM. In terms of optimization, Page Factory is more optimized than the POM. It uses a FindBy annotation for page objects. Each object doesn’t require initialization like POM, and all elements are initialized with initElements(). It doesn’t have cache storage because it doesn’t need it. 

To provide more context on the difference in annotations and methods used, let’s look at how they both locate the search box web elements from the Google home page. To locate web elements for Page Factory, @FindBy is used, while for the POM, the method @FindElements is used. 

The line above shows that the annotation points directly to a specific locator ID, while @FindElements requires many more pointers to get a particular element.

Annotations in Page Factory

To identify UI elements for web pages, it’s essential to develop a location strategy. Annotations can also be used to do this. Some of these annotations have been highlighted earlier. Let’s look at them here:

  • @FindBy: This is used to identify web elements with just one defined criterion. It can be used to locate the following: id, name, className, CSS, XPath, tagName, linkText, and partialLinkText.
  • @FindBys: This is used to point to web elements with more than one criteria and must match all. An AND conditional relationship is used to identify the web elements based on the various criteria defined.
  • @FindAll: This is similar to the @FindBys, but it needs to match at least one of the criteria. In this case, an OR conditional relationship is used.
  • @CacheLookUp: The end goal of this annotation is to make the code execute faster. It’s used mostly for WebElements in a test case. For the specific element, it keeps the WebElement in the cache memory after the first time it’s run.

Expand Your Test Coverage

Fast and flexible authoring of AI-powered end-to-end tests — built for scale.
Start Testing Free

Initialization

After we locate the WebElements, we need to initialize them using InitElements(). This always occurs after the @FindBy annotation. The line below shows how the initElement() is used.

In Page Factory, there is also a concept called Lazy Load, which uses AjaxElementLocatorFactory. It can be used when an element must be found for an operation and pass a timeout value. It can also be used when the application in the picture uses Ajax elements. 

So let’s look at how to use Page Factory with a specific example.

How to Use Page Factory in Selenium

This guide will use Java to implement Page Factory in Selenium. The following steps are necessary to follow this guide.

After installing the components listed above, you can now follow the next steps to learn how to use Page Factory.

Scenario

To fully explain this concept, we’ll use the scenario below:

  1. Open the Chrome browser.
  2. Open the URL https://google.com.
  3. Write Testing in the text box.
  4. Click Search.
  5. Close the browser.

Steps

To automate the test case above, we’ll first create a new Maven project. Next, let’s give both the groupID and attributeID the value PageFactoryID

Now, add the dependencies for Selenium and Junit. The {junit.version} and {selenium.version} should be replaced with the version on their website.

Now that we’ve added the dependencies let’s define the project’s structure.

Project Structure

Packages

In the POM, there are recommended sets of packages for projects, which are the webpage and test packages. For this project, create a package with the name com.google.page. Within your created package, com.google creates two other packages: webpages and tests

Classes

Next, let’s create a class named GoogleHomePage. This is where the locators and methods for the homepage will be stored. This class imports the webdriver, webelement, annotation, and pagefactory.

Testing

We have now created the class for the page; the next class to create is for the test instance. Create a class named GoogleHomeTest. In this class, the webdriver objects are used to open the chrome browser, implement waits, and then the URL is defined. Note: this class is created within the com.google.test page.

So the test ends by closing the chrome driver. Now you’ve implemented Page Factory in Selenium!

Conclusion

When carrying out test automation, consider implementing Page Factory in Selenium. And remember to use @CacheLookup within InitElements(). This will ensure the web elements load just once and do not have to reload whenever the method is called. Also, note that asides from single web elements, Page Factory also locates lists of elements.

Tools for test automation, such as Testim, are helpful in areas like these. So take advantage of the 14-day trial and learning resources, and try Testim for free now!

Testim's latest articles, right in your inbox.

From our latest feature releases, to the way it impacts the businesses of our clients, follow the evolution of our product

Blog Subscribe