Mustafizur Rahaman’s Post

View profile for Mustafizur Rahaman

Automation Test Engineer | Expert in Selenium, Java & API Testing (Rest Assured) | Cucumber, TestNG, Git/Jenkins | Driving Quality at Scale

What is POM? POM (Page Object Model) is a design pattern used in test automation mainly with Selenium. It helps you separate test logic from UI elements, making your code cleaner, reusable, and easier to maintain. Why we use POM • To avoid duplicate locators and messy test scripts. • To make updates easy if one element changes, you fix it in one place only. • To keep tests readable and maintainable. Different Ways to Implement POM 1. Without PageFactory (classic way) –using By locators and driver.findElement(). 2. With PageFactory – using @FindBy annotations for cleaner code. Example: Login Page (with PageFactory) 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; // Locators @FindBy(id = "username") WebElement usernameField; @FindBy(id = "password") WebElement passwordField; @FindBy(id = "loginBtn") WebElement loginButton; // Constructor public LoginPage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } // Actions public void login(String username, String password) { usernameField.sendKeys(username); passwordField.sendKeys(password); loginButton.click(); } } Usage in Test Class import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class LoginTest { @Test public void verifyLogin() { WebDriver driver = new ChromeDriver(); driver.get("https://example.com/login"); LoginPage loginPage = new LoginPage(driver); loginPage.login("admin", "password123"); driver.quit(); } } Bottom line POM = Clean structure + Reusable locators + Easy maintenance. It’s the backbone of any good Selenium automation framework. #Selenium #POM #TestAutomation #SDET #Java

To view or add a comment, sign in

Explore content categories