Introduction:
Maintaining clean and Scalable code is important for efficient software testing services in the world of test automation. Page Factory and Page Object Model are architectures that help achieve this goal. Page Factory can be used to make it easier to manage test automation.
Table of Content
- What is Page Object Model (POM)?
- Description of Page Object Model (POM):
- What is Page Factory?
- Description of Page Factory:
- Implementing POM with Page Factory in a Maven Project
- Key Features Of POM
- Benefits
- Conclusion
What is the Page Object Model (POM)?
POM is a design pattern that represents a web page as a class, encapsulating its elements and actions. This approach enables testers to write more readable and maintainable code, separating test logic from page-specific details.
Description of Page Object Model (POM):
- It is a Java design pattern that is used to design classes in test scripts.
- We have to create several classes which depend upon the number of web pages which are going to be handled in automation.
- We have to declare the number of variables which depends upon the number of web elements that are going to be automated.
- To design classes, we use various kinds of oops concepts and standard ways to write code. i.e.
Declaration + Initialization + Usage
- Firstly, in one class we declare all variables as private, so we don’t get access to that variable in another class.
- After that, we use a constructor by using a public access specifier, and in that, we initialize variables by using this keyword.
- We require non-static methods to execute each web element that is stored inside the variable.
- The objective of providing encapsulation is to provide security to the class and the execution class must be made in another package.
What is Page Factory?
Page Factory is a Selenium WebDriver feature that simplifies the process of initializing and managing page objects. It provides a way to create page objects with minimal code, making it an ideal companion for POM.
Description of Page Factory:
- Page factory is a class that is in-built into the POM concept used to support page object design patterns.
- It is a class that is provided by selenium WebDriver which contains static methods i.e. initElements() which is parameterized.
- To initialize data members on a page factory, we have the initElements() method.
- We can do this by using @FindBy annotation, which takes the locator type as an argument.
- @FindBy is used to find out the live web element, non-live web element, and hidden element.
Syntax: -
Pagefactory.initElement(WebDriver ref, this)
- While executing the test script initElement() method will convert all data members into @FindBy annotation.
@FindBy(xpath = “ “)
- This process is known as basic initialization/ early initialization.
Implementing POM with Page Factory in a Maven Project:
1. Set Up Your Maven Project: Create a new Maven project and add the necessary dependencies, including Selenium WebDriver and TestNG.
2. Create Page Classes: Design page classes that represent your application's web pages. Each class should contain:
- Web elements (e.g., @FindBy annotations)
- Constructor with PageFactory.initElements
- Methods for interacting with the page
3. Use PageFactory to Initialize Page Objects: In your test class, create instances of page objects using PageFactory.initElements.
4. Write Tests Using Page Objects: Write test methods that utilize page object methods to perform actions and assertions.
Example Code:
Page Class:
public class LoginPage {
@FindBy(id = "username")
private WebElement usernameInput;
@FindBy(id = "password")
private WebElement passwordInput;
@FindBy(css = "button[type='submit']")
private WebElement submitButton;
public LoginPage() {
PageFactory.initElements(Driver.get(), this);
}
public void enterUsername(String username) {
usernameInput.sendKeys(username);
}
public void enterPassword(String password) {
passwordInput.sendKeys(password);
}
public void clickSubmit() {
submitButton.click();
}
}
Test Class:
public class LoginTest {
@Test
public void testLogin() {
LoginPage loginPage = PageFactory.initElements(Driver.get(), LoginPage.class);
loginPage.enterUsername("testuser");
loginPage.enterPassword("testpassword");
loginPage.clickSubmit();
// Assertions...
}
}
Key Features of POM:
- Encapsulation: Keeps the tests separate from the page structure.
- Code Reusability: Methods defined in the Page Object classes can be reused across multiple test cases, reducing duplication.
- Maintainability: Changes in the UI require updates only in the Page Object classes, making maintenance easier.
- Readability: The separation of test logic and page-specific code makes the tests more readable and understandable.
- Modularity: Each page is represented by a group, simplifying the way of test automation.
- Scalability: As the application develops, unused pages can be effortlessly included in the test suite without influencing existing tests.
Benefits:
- Simplified Code: Reduces the amount of code needed to initialize web elements.
- Enhanced Readability: The concise syntax provided by Page Factory enhances the readability of the code.
- Consistency: Ensures a consistent way of defining and initializing web elements across the test suite.
Conclusion:
Test automation services can be simplified with the implementation of the Page Object Model in a Maven project. You can write more readable code if you separate test logic from page-specific details. Try this approach in your next test automation project.
This approach is the best practice for any team looking to strike a balance between speed and efficiency in their testing efforts. By taking the time to build a solid foundation using POM and Page Factory, teams can save a lot of effort, reduce maintenance costs, and increase the reliability of their automated tests in the long term.
About Author
Pramol Chandekar is a Sr. QA Executive at The One Technologies, with a passion for continuous learning and embracing new technologies in the IT industry. Since beginning his journey in September 2019, Pramol has dedicated himself to mastering his craft and expanding his expertise daily, paving the way for future leadership roles. His ambitious goal is to ascend to the role of Project Lead and Project Manager, shaping impactful projects and driving innovation in the field.