Answer
Page Object Model (POM) is a design pattern in Selenium that creates a framework for maintaining and organizing automation scripts.
Core concept:
- āFor each page of the application, a separate Java class is created
- āThe class contains all web elements (locators) belonging to that page
- āThe class contains all methods (actions) that can be performed on that page
- āTest scripts are maintained in separate files and call these page object methods
Why use POM:
- āSeparates test logic from page interaction logic
- āLocator changes require editing only one class ā not every test
- āMethods are reusable across multiple test classes
- āMakes tests readable like plain English
Example structure:
CODE
src/
āāā pages/
ā āāā LoginPage.java ā Web elements + methods for Login page
ā āāā DashboardPage.java ā Web elements + methods for Dashboard
ā āāā SearchPage.java
āāā tests/
āāā LoginTest.java ā Calls LoginPage methods
āāā SearchTest.java
LoginPage.java:
Java
public class LoginPage {
private By username = By.id("username");
private By password = By.id("password");
private By loginBtn = By.id("loginBtn");
public LoginPage(WebDriver driver) { this.driver = driver; }
public DashboardPage login(String user, String pass) {
driver.findElement(username).sendKeys(user);
driver.findElement(password).sendKeys(pass);
driver.findElement(loginBtn).click();
return new DashboardPage(driver);
}
}
