Page Object Model (POM) is a design pattern in Selenium testing. Why does using POM help keep test code easy to maintain?
Think about how changes in the website affect your test scripts.
POM keeps locators and page actions in separate classes. When UI changes, only page classes need updates, not all tests. This separation makes maintenance easier.
Given a POM class with a locator for a login button, if the locator changes in the page class but test code is not updated, what happens when the test runs?
public class LoginPage { private WebDriver driver; private By loginButton = By.id("loginBtn"); public LoginPage(WebDriver driver) { this.driver = driver; } public void clickLogin() { driver.findElement(loginButton).click(); } } // Test code: LoginPage page = new LoginPage(driver); page.clickLogin();
What happens if the locator does not match any element?
If the locator is outdated, Selenium cannot find the element and throws NoSuchElementException, causing test failure.
In a POM test, you want to verify the page title is exactly "Dashboard" after login. Which assertion is correct?
String actualTitle = driver.getTitle();
Exact match is required, not partial or null checks.
assertEquals checks that actualTitle exactly matches "Dashboard", which is the requirement.
What is the main issue in this POM test code that causes flaky tests?
public void clickSubmit() throws InterruptedException {
driver.findElement(By.id("submitBtn")).click();
Thread.sleep(5000);
}Think about why fixed waits are discouraged in tests.
Thread.sleep causes tests to wait a fixed time regardless of page readiness, leading to slow or flaky tests. Better to use explicit waits.
Which approach best supports maintainability when organizing locators in a large POM framework?
Consider encapsulation and immutability for stable locators.
Private static final variables keep locators immutable and encapsulated within page classes, improving maintainability and preventing accidental changes.