Testing With Selenium WebDriver (75 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (65 Blogs)
SEE MORE

Page Factory in Selenium: Everything You need to know

Published on Aug 09,2024 19 Views

A passionate and knowledgeable tech enthusiast known for his expertise in the... A passionate and knowledgeable tech enthusiast known for his expertise in the world of technology and programming. With a deep-rooted passion for coding, Sarfaraz...

Page factories are one of the compelling design patterns that strengthen Selenium’s POM framework. This article will discuss the concept of Page Factory in Selenium, its benefits, and how they could be implemented within Selenium WebDriver.

We will consider the review with the Page Object Model and provide a well-pointed guide on utilizing Page Factory in your Selenium projects.

Table of Content

What is a Page Factory?

Selenium Page Factory is an inbuilt design pattern to support the Page Object Model framework. This class promotes the Page Object Model that Selenium WebDriver provides. What is a page factory in Selenium? It’s a tool to ease the creation and handling of web elements in test automation scripts.

Page Factory is based on the @FindBy annotation for locating WEB elements. This annotation helps testers define WEB elements using different locator strategies. Page Factory supports lazy initialization of the WEB elements, which means WEB elements are located in the DOM only when required.

Page Factory is an organized and clean way to structure test code. Fundamentally, it separates the object repository from test methods. Separation makes the code clear to read and reuse. Further, Page Factory reduces the amount of code needed to find and manipulate Web elements.

 

What is the difference between Page Factory and Page Object Model?

Even though both the Page object model and Page Factory are considered design patterns in Selenium for Web Automation Testing, they differ in how they are implemented and used.

  • The Page Object Model (POM) is a design pattern in which every web page has a separate class. Each class holds the web elements on the page and the methods to manipulate them. POM improves the maintenance of test cases and reduces code duplication. It models the web pages of your application as a series of objects.
  • On the other hand, Page Factory implements and optimizes the Page Object Model. It uses annotations to find web elements, making the syntax for finding and initializing Web Elements very simple.
  • In POM, you usually use the “driver.findElement()” method to locate the elements. Using Page Factory, you would use annotations such as @FindBy. Page Factory also uses the initElements() method. This creates the web elements defined in your Page Object.
  • The next massive difference is lazy initialization. In POM, elements are initialized while creating the page object. In-Page Factory, they are only initialized when used, enhancing your test performance.
  • It also supports the concept of Ajax elements. It can handle dynamic elements which may not appear immediately when a page loads. This feature comes in handy when testing modern web applications with dynamic content.

 

If you want to learn more, consider taking a selenium tutorial.

How to Use Page Factory in Selenium?

The implementation of the Page Factory in Selenium involves several steps. Here is a step-by-step guide to implementing Page Factory in your Selenium projects:

  • Create a Page Object class: Create a class for each web page you want to test. This class contains the page’s web elements and methods.
  • Declare Web Elements: Declare the web elements in your Page Object class as private variables. Tag or annotate these variables using the @FindBy annotation. In this tagging, you specify how the element should be located. You may use various locator strategies like id, name, className, cssSelector, or xpath.
  • Define Elements: Create a constructor for your Page Object class. In this constructor, you should initialize the web elements using PageFactory.initElements(). This will take two parameters — the instance of WebDriver and the Page Object class itself, usually passed with ‘this.’
  • Define Methods: You would have methods within your Page Object class to operate on those web elements. These methods will represent the user’s activities on that particular page.
  • Test Classes: Here, you will create an instance of this class within your test classes. You can use the methods you created to interact with the web page.

 

Advantage of Page Factory in Selenium

Why do we use Page Factory in Selenium? There are several advantages why this has become a popular choice for test automation:

  • Improved code structure: Page Factory helps keep a clean and organized code structure. It separates the Object Repository from the test methods, making the code easier to read and maintain. It reduces code duplication: Using the Page Factory, you will define web elements once and reuse them across multiple tests.
  • Lazy Initialization: Page Factory initializes WebElements lazily. The elements are initialized only when they are to be used in the test, which can significantly speed up your test suite.
  • Accessible Element Location: The annotation @FindBy makes the location of elements easy. You can quickly locate elements specifying different locator strategies without messing up your code with long findElement() calls.
  • Ajax Elements Support: Page Factory will handle dynamic elements not present in a page immediately upon loading. This feature is especially useful when testing modern web applications.
  • Improved Test Maintenance: If the UI of your application changes, you now need to update only the Page Object. All tests using that Page Object will automatically pick changes to element locators.
  • These advantages allow Page Factory to become a powerful tool for creating automated tests that are both robust and maintainable.

Knowing about Selenium is good for a person interested in software testing. Selenium helps the tester use web applications smoothly, improving his efficiency and accuracy.

 

Initialization of Web Components in Page Factory Using Selenium

The process of initializing the page factory in Selenium is straightforward. The critical method for this is PageFactory.initElements(). This method takes two parameters: the instance of the WebDriver and the Page Object class itself.

Here’s how you typically do the initialization of web components:


public class LoginPage {

WebDriver driver;

public LoginPage(WebDriver driver) {

this.driver = driver;

PageFactory.initElements(driver, this);

}

// Web elements and methods go here

}

In the code above, PageFactory.initElements(driver, this) initializes all the web elements in the class LoginPage using @FindBy annotations.

You can also use PageFactory.initElements() in your test class. This approach allows you to initialize elements without creating a Page Object class.

This information can help you prepare for selenium interview questions .

 

First Selenium program using Page Factory

Let’s create a simple Selenium program using Page Factory. This example will show how to automate the login process using Page Factory.

First of all, let’s create the Page Object class:


Import org. openqa. Selenium.WebDriver;

Import org. openqa. Selenium.WebElement;

Import org. openqa. Selenium. Support.FindBy;

Import org. openqa. Selenium. Support.PageFactory;

public class LoginPage {

WebDriver driver;

@FindBy(id = "username")

private WebElement usernameInput;

@FindBy(id = "password")

private WebElement password input;

@FindBy(id = "login")

private WebElement loginButton;

public LoginPage(WebDriver driver) {

this.driver = driver;

PageFactory.initElements(driver, this);

}

 

public void login(String username, String password) {

 

usernameInput.sendKeys(username);

 

passwordInput.sendKeys(password);

 

loginButton.click();

}

}

 

Now, let’s implement the test class:


Import org. openqa. Selenium.WebDriver;

 

Import org. openqa. Selenium. Chrome.ChromeDriver;

 

Import org.testng.annotations.Test;

 

 

public class LoginTest {

@Test

public void testLogin() {

System.setProperty("webdriver.chrome.driver","path/to/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://example.com/login");

LoginPage loginPage = new LoginPage(driver);

loginPage.login("testuser", "password123");

// You could perform needful assertions here that justify that we've successfully logged in.

driver.quit();

}

}

 

In the above example, we created a LoginPage class that was implemented using the Page Factory. We have used this class in our LoginTest to run a login operation. This example explains how Page Factory helps make our test code less complex and easier to read and maintain.

To learn more about selenium and its fundamentals, you can take the Selenium course. By learning this course, you can master the skills to automate web applications smoothly and enhance your career in the software testing field.

 

Conclusion

The page factory is another vital tool in Selenium for automated website testing. It supports the Page Object Model and, as such, makes element management and test maintenance easy. Testers could use the Page Factory to develop more effective and solid automated tests.

FAQs

What is page factory in Selenium?

Page Factory is a class provided by the Selenium WebDriver to support the Page Object Model. It uses annotations to find and initialize Web Elements.

What is the difference between POM and Page Factory?

POM is a design pattern; on the other hand, the Page Factory implements the POM. The Page Factory uses annotations and lazy initialization, which POM does not have.

Why do we use page factory in Selenium?

We use the Page Factory to enhance code structuring, lessen duplication, and make element handling easier during Selenium tests.

What is the use of page factory?

It creates more maintainable and efficient automated tests by separating the object repository from the test methods.

How do I initialize a page factory?

You initialize Page Factory using PageFactory.initElements(), typically in your Page Object class constructor.

Upcoming Batches For Selenium Certification Training Course
Course NameDateDetails
Selenium Certification Training Course

Class Starts on 14th September,2024

14th September

SAT&SUN (Weekend Batch)
View Details
Selenium Certification Training Course

Class Starts on 23rd September,2024

23rd September

MON-FRI (Weekday Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Page Factory in Selenium: Everything You need to know

edureka.co