Bird
Raised Fist0
Selenium Javatesting~5 mins

Why POM creates maintainable test code in Selenium Java

Choose your learning style10 modes available
Introduction

Page Object Model (POM) helps keep test code clean and easy to update by separating page details from test steps.

When you want to test a website with many pages.
When the website design changes often.
When multiple testers work on the same project.
When you want to avoid repeating code for the same page.
When you want faster test maintenance and fewer errors.
Syntax
Selenium Java
public class LoginPage {
    private WebDriver driver;
    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.id("loginBtn");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

    public void enterPassword(String password) {
        driver.findElement(passwordField).sendKeys(password);
    }

    public void clickLogin() {
        driver.findElement(loginButton).click();
    }
}

POM uses classes to represent pages.

Locators and actions are inside page classes, not test code.

Examples
Test code calls page methods to perform actions.
Selenium Java
LoginPage login = new LoginPage(driver);
login.enterUsername("user1");
login.enterPassword("pass123");
login.clickLogin();
Another page class example with a method to get text.
Selenium Java
public class HomePage {
    private WebDriver driver;
    private By welcomeMessage = By.id("welcomeMsg");

    public HomePage(WebDriver driver) {
        this.driver = driver;
    }

    public String getWelcomeText() {
        return driver.findElement(welcomeMessage).getText();
    }
}
Sample Program

This test uses POM to keep page details inside LoginPage class. The test code is simple and easy to read.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginPage {
    private WebDriver driver;
    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.id("loginBtn");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

    public void enterPassword(String password) {
        driver.findElement(passwordField).sendKeys(password);
    }

    public void clickLogin() {
        driver.findElement(loginButton).click();
    }
}

public class LoginTest {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com/login");

        LoginPage loginPage = new LoginPage(driver);
        loginPage.enterUsername("testuser");
        loginPage.enterPassword("testpass");
        loginPage.clickLogin();

        // Assume after login, URL changes to /home
        String currentUrl = driver.getCurrentUrl();
        if (currentUrl.contains("/home")) {
            System.out.println("Test Passed: Login successful.");
        } else {
            System.out.println("Test Failed: Login unsuccessful.");
        }

        driver.quit();
    }
}
OutputSuccess
Important Notes

Changing a locator in POM means updating only one place.

Tests become shorter and clearer by using page methods.

POM helps teams avoid duplicated code and mistakes.

Summary

POM separates page details from test logic.

This makes tests easier to maintain and update.

It saves time when the website changes.

Practice

(1/5)
1. What is the main reason the Page Object Model (POM) helps create maintainable test code?
easy
A. It runs tests faster by using parallel execution.
B. It separates page structure from test logic, making updates easier.
C. It requires less code to write tests.
D. It automatically fixes broken tests.

Solution

  1. Step 1: Understand POM structure

    POM organizes code by separating page details (locators, actions) from test steps.
  2. Step 2: Identify maintenance benefit

    This separation means if the page changes, only the page object needs updating, not all tests.
  3. Final Answer:

    It separates page structure from test logic, making updates easier. -> Option B
  4. Quick Check:

    Separation of concerns = easier maintenance [OK]
Hint: Remember: POM separates page and test code for easy updates [OK]
Common Mistakes:
  • Thinking POM speeds up test execution
  • Believing POM reduces code amount drastically
  • Assuming POM auto-fixes test failures
2. Which of the following is the correct way to define a WebElement in a Page Object class using Selenium Java?
easy
A. WebElement button = driver.findElement(By.name);
B. WebElement button = driver.findElement("submit");
C. WebElement button = driver.findElement(By.id("submit"));
D. WebElement button = driver.findElement(By.id);

Solution

  1. Step 1: Recall correct syntax for locating elements

    In Selenium Java, findElement requires a By locator like By.id("value").
  2. 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.
  3. Final Answer:

    WebElement button = driver.findElement(By.id("submit")); -> Option C
  4. Quick Check:

    Use By.id("value") for locating elements [OK]
Hint: Use By.id("value") or By.name("value") with findElement [OK]
Common Mistakes:
  • Omitting the locator type (By.id, By.name)
  • Passing locator type without string
  • Using findElement with just a string
3. Given the following Page Object method, what will be the output when calling 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();
  }
}
medium
A. "Welcome Page"
B. "Login Page"
C. null
D. Throws NullPointerException

Solution

  1. Step 1: Understand getTitle() method

    The method returns the title of the current page from the WebDriver.
  2. Step 2: Given page title is "Welcome Page"

    Calling getTitle() returns the actual page title string, which is "Welcome Page".
  3. Final Answer:

    "Welcome Page" -> Option A
  4. Quick Check:

    driver.getTitle() returns current page title [OK]
Hint: driver.getTitle() returns the current page title string [OK]
Common Mistakes:
  • Confusing method return with page object name
  • Assuming method returns null or exception without driver
  • Thinking getTitle() returns page URL
4. Identify the error in this Page Object method and how to fix it:
public void clickLogin() {
  WebElement loginBtn = driver.findElement(By.id);
  loginBtn.click();
}
medium
A. loginBtn should be declared as WebElement[] instead of WebElement.
B. click() method cannot be called on WebElement.
C. driver.findElement should be replaced with driver.findElements.
D. By.id is missing the locator string; fix by adding the id value.

Solution

  1. Step 1: Check findElement syntax

    By.id requires a string argument like By.id("loginBtn"), but here it is missing.
  2. Step 2: Fix the locator

    Add the id string inside By.id, e.g., By.id("loginBtn") to correctly locate the element.
  3. Final Answer:

    By.id is missing the locator string; fix by adding the id value. -> Option D
  4. Quick Check:

    By.id("value") needs a string argument [OK]
Hint: Always provide locator string inside By.id("value") [OK]
Common Mistakes:
  • Omitting the locator string in By.id
  • Using findElements when only one element needed
  • Thinking click() is invalid on WebElement
5. How does using the Page Object Model (POM) save time when the website under test changes its layout or element locators?
hard
A. Only the Page Object classes need updates, not all test scripts.
B. Tests automatically adapt without any code changes.
C. POM uses AI to detect and fix locator changes.
D. Test scripts are deleted and rewritten faster.

Solution

  1. Step 1: Understand POM's separation of concerns

    POM keeps locators and page actions inside Page Object classes, separate from test logic.
  2. Step 2: Effect of website changes

    If locators or layout change, only the Page Object classes need updating, so tests remain unchanged.
  3. Final Answer:

    Only the Page Object classes need updates, not all test scripts. -> Option A
  4. Quick Check:

    Update page objects, keep tests stable [OK]
Hint: Update page objects only when UI changes, not all tests [OK]
Common Mistakes:
  • Believing tests auto-update without code changes
  • Thinking POM uses AI or automation to fix locators
  • Assuming tests must be rewritten after UI changes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard