In Selenium, Page Object Model (POM) is a design pattern that promotes object-oriented programming principles to make your code more readable, maintainable, and reusable. PageFactory is a class that supports the Page Object Model by simplifying the element initialization process using annotations.
Here’s how you can use POM with PageFactory in Selenium WebDriver:
Steps:
Create Page Object Class:
The Page Object class represents a page in the application and contains elements and actions that can be performed on that page. You’ll define the elements on the page using @FindBy annotations.
Use @FindBy to locate elements:
Use @FindBy annotation to identify WebElements (like buttons, text fields, links, etc.) and use the PageFactory class to initialize those elements.
PageFactory Initialization:
Use PageFactory.initElements(driver, this) to initialize the elements on the page when the Page Object class is instantiated.
Write Test Case using the Page Object:
In your test scripts, create an instance of the page class and perform actions on the page using the methods you define in the page class.
Example:
1. Create the Page Object Class:
Let's say we are automating a login page.
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.WebDriver;
public class LoginPage {
// Locating elements using @FindBy
@FindBy(id = "username")
private WebElement usernameField;
@FindBy(id = "password")
private WebElement passwordField;
@FindBy(id = "loginBtn")
private WebElement loginButton;
// Constructor to initialize the elements using PageFactory
public LoginPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
// Method to perform login
public void login(String username, String password) {
usernameField.sendKeys(username);
passwordField.sendKeys(password);
loginButton.click();
}
// Method to check if login is successful by verifying some element
public boolean isLoginSuccessful() {
// Example: Check if some element exists after successful login
return someElementAfterLogin.isDisplayed();
}
}
2. Using the Page Object in a Test Case:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
public static void main(String[] args) {
// Initialize WebDriver (ChromeDriver in this case)
WebDriver driver = new ChromeDriver();
// Navigate to the login page
driver.get("https://example.com/login");
// Create an instance of LoginPage
LoginPage loginPage = new LoginPage(driver);
// Use the method to perform login
loginPage.login("testuser", "password123");
// Assert the login success (this can be more advanced based on your application)
if (loginPage.isLoginSuccessful()) {
System.out.println("Login successful!");
} else {
System.out.println("Login failed.");
}
// Close the browser
driver.quit();
}
}
Breakdown of Key Concepts:
@FindBy Annotation:
This is used to find the web elements on the page. You can use various locators like id, name, xpath, css, etc.
@FindBy(id = "username")
private WebElement usernameField;
PageFactory.initElements:
This method initializes the web elements defined in the Page Object class. It's necessary to call this in the constructor.
PageFactory.initElements(driver, this);
Page Object Methods:
Methods like login() are used to define actions that can be performed on the page. They are called in your test scripts.
Test Script (LoginTest):
The test script initializes the WebDriver, creates an instance of the Page Object class, and calls the methods defined there to perform actions on the page.
Advantages of Using POM with PageFactory:
Separation of concerns: Your tests are decoupled from the UI structure, making tests more maintainable.
Code Reusability: Reusable methods in Page Objects, such as login(), can be used across multiple tests.
Cleaner Test Code: The test script focuses on what to do rather than how to interact with the web elements.
Easy Maintenance: If the UI changes (e.g., a button is renamed or moved), you only need to update the Page Object class instead of each individual test.
Notes:
Lazy Initialization: PageFactory uses lazy initialization, meaning the elements are not initialized until they are actually used (i.e., when you interact with them).
WebDriver Waits: In a real-world scenario, consider using explicit waits to ensure elements are present or visible before interacting with them, especially for dynamic pages.
Conclusion:
By using PageFactory in the Page Object Model design pattern, you create more maintainable and readable test scripts, promoting reusability and reducing the impact of UI changes on your tests.
Learn Selenium with JAVA Training in Hyderabad
Read More
Introduction to Data Driven Framework
Introduction to Keyword Driven Framework
What is a Hybrid Framework in Selenium?
Folder Structure for a Scalable Selenium Framework
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments