Why POM creates maintainable test code in Selenium Java - Automation Benefits in Action
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import java.time.Duration; // Page Object for Login Page class LoginPage { private WebDriver driver; private WebDriverWait wait; private By emailField = By.id("email"); private By passwordField = By.id("password"); private By loginButton = By.id("loginBtn"); public LoginPage(WebDriver driver) { this.driver = driver; this.wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } public void enterEmail(String email) { wait.until(ExpectedConditions.visibilityOfElementLocated(emailField)).sendKeys(email); } public void enterPassword(String password) { wait.until(ExpectedConditions.visibilityOfElementLocated(passwordField)).sendKeys(password); } public void clickLogin() { wait.until(ExpectedConditions.elementToBeClickable(loginButton)).click(); } } // Page Object for Dashboard Page class DashboardPage { private WebDriver driver; private WebDriverWait wait; private By logoutButton = By.id("logoutBtn"); public DashboardPage(WebDriver driver) { this.driver = driver; this.wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } public boolean isLogoutButtonVisible() { return wait.until(ExpectedConditions.visibilityOfElementLocated(logoutButton)).isDisplayed(); } } public class LoginTest { private WebDriver driver; private LoginPage loginPage; private DashboardPage dashboardPage; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://example.com/login"); loginPage = new LoginPage(driver); dashboardPage = new DashboardPage(driver); } @Test public void testSuccessfulLogin() { loginPage.enterEmail("testuser@example.com"); loginPage.enterPassword("Test@1234"); loginPage.clickLogin(); // Verify URL contains /dashboard new WebDriverWait(driver, Duration.ofSeconds(10)) .until(ExpectedConditions.urlContains("/dashboard")); assertTrue(driver.getCurrentUrl().contains("/dashboard"), "Dashboard URL should be displayed"); // Verify logout button is visible assertTrue(dashboardPage.isLogoutButtonVisible(), "Logout button should be visible on dashboard"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses the Page Object Model (POM) to keep the code clean and maintainable.
LoginPage class holds locators and actions for the login page. It uses explicit waits to wait for elements before interacting.
DashboardPage class holds locators and checks for the dashboard page.
The test testSuccessfulLogin uses these page objects to perform login and verify results. This separation means if the login page changes, only LoginPage needs updating, not the test itself.
Explicit waits avoid flaky tests by waiting for elements properly.
Assertions check the URL and visibility of logout button to confirm successful login.
This structure makes the test easy to read, maintain, and extend.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid).
Practice
Solution
Step 1: Understand POM structure
POM organizes code by separating page details (locators, actions) from test steps.Step 2: Identify maintenance benefit
This separation means if the page changes, only the page object needs updating, not all tests.Final Answer:
It separates page structure from test logic, making updates easier. -> Option BQuick Check:
Separation of concerns = easier maintenance [OK]
- Thinking POM speeds up test execution
- Believing POM reduces code amount drastically
- Assuming POM auto-fixes test failures
Solution
Step 1: Recall correct syntax for locating elements
In Selenium Java, findElement requires a By locator like By.id("value").Step 2: Check each option
WebElement button = driver.findElement(By.id("submit")); uses By.id with a string, which is correct. Others miss the locator type or string.Final Answer:
WebElement button = driver.findElement(By.id("submit")); -> Option CQuick Check:
Use By.id("value") for locating elements [OK]
- Omitting the locator type (By.id, By.name)
- Passing locator type without string
- Using findElement with just a string
loginPage.getTitle() if the page title is "Welcome Page"?
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public String getTitle() {
return driver.getTitle();
}
}Solution
Step 1: Understand getTitle() method
The method returns the title of the current page from the WebDriver.Step 2: Given page title is "Welcome Page"
Calling getTitle() returns the actual page title string, which is "Welcome Page".Final Answer:
"Welcome Page" -> Option AQuick Check:
driver.getTitle() returns current page title [OK]
- Confusing method return with page object name
- Assuming method returns null or exception without driver
- Thinking getTitle() returns page URL
public void clickLogin() {
WebElement loginBtn = driver.findElement(By.id);
loginBtn.click();
}Solution
Step 1: Check findElement syntax
By.id requires a string argument like By.id("loginBtn"), but here it is missing.Step 2: Fix the locator
Add the id string inside By.id, e.g., By.id("loginBtn") to correctly locate the element.Final Answer:
By.id is missing the locator string; fix by adding the id value. -> Option DQuick Check:
By.id("value") needs a string argument [OK]
- Omitting the locator string in By.id
- Using findElements when only one element needed
- Thinking click() is invalid on WebElement
Solution
Step 1: Understand POM's separation of concerns
POM keeps locators and page actions inside Page Object classes, separate from test logic.Step 2: Effect of website changes
If locators or layout change, only the Page Object classes need updating, so tests remain unchanged.Final Answer:
Only the Page Object classes need updates, not all test scripts. -> Option AQuick Check:
Update page objects, keep tests stable [OK]
- Believing tests auto-update without code changes
- Thinking POM uses AI or automation to fix locators
- Assuming tests must be rewritten after UI changes
Ready to prove it?
Pass the test and this pattern moves unknown
