What if you could fix all your broken tests by changing just one file?
Why Page class design in Selenium Java? - Purpose & Use Cases
Imagine testing a website by writing code that directly finds and clicks buttons everywhere in your test scripts.
Every time the page changes, you must update many places in your code.
This manual way is slow and confusing.
When locators change, you waste hours fixing many test scripts.
It's easy to make mistakes and hard to keep tests working.
Page class design groups all page details in one place.
Tests call simple methods like loginPage.clickLogin() instead of repeating locator code.
This makes tests cleaner and easier to fix.
driver.findElement(By.id("loginBtn")).click(); driver.findElement(By.id("username")).sendKeys("user");
loginPage.enterUsername("user");
loginPage.clickLogin();It enables fast, clear, and maintainable tests that adapt easily to page changes.
When a website changes button IDs, you update only the page class, not every test script.
Manual locator use spreads repeated code and errors.
Page class design centralizes page details for easy updates.
Tests become simpler, faster to write, and easier to maintain.
Practice
Solution
Step 1: Understand the role of page classes
Page classes organize locators and actions related to a specific web page.Step 2: Identify the main purpose
This organization improves code readability and reuse by keeping page details in one place.Final Answer:
To group web page elements and actions in one place -> Option CQuick Check:
Page class purpose = Group elements and actions [OK]
- Confusing page classes with test classes
- Using page classes for database or API logic
- Writing assertions inside page classes
Solution
Step 1: Recall best practice for locators
Locators should be stored as By objects, not WebElement, to delay element lookup.Step 2: Check each option
private By submitButton = By.id("submit"); correctly declares a private By locator with By.id("submit").Final Answer:
private By submitButton = By.id("submit"); -> Option DQuick Check:
Locators = By objects [OK]
- Declaring locators as WebElement fields
- Using public visibility for locators
- Assigning void type to variables
loginPage.getTitle() return if the page title is "Welcome Page"?public class LoginPage {
private WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public String getTitle() {
return driver.getTitle();
}
}Solution
Step 1: Understand getTitle() method
The method calls driver.getTitle(), which returns the current page title.Step 2: Match with given page title
The page title is "Welcome Page", so getTitle() returns this string.Final Answer:
"Welcome Page" -> Option AQuick Check:
driver.getTitle() = "Welcome Page" [OK]
- Assuming getTitle() returns page class name
- Expecting null if not set explicitly
- Confusing with element text retrieval
public void clickLogin() {
WebElement loginBtn = driver.findElement(By.id("login"));
loginBtn.click();
}Solution
Step 1: Review locator usage best practice
Locators should be stored as By fields in the page class, not found inside methods.Step 2: Identify the problem
Finding element inside method reduces reuse and violates page class design principles.Final Answer:
Should store locator as a By field, not find element inside method -> Option BQuick Check:
Locators as By fields = best practice [OK]
- Finding elements repeatedly inside methods
- Ignoring locator reuse
- Assuming exception handling is mandatory here
Solution
Step 1: Recall page class design principles
Locators should be private By fields; actions should be public methods for reuse and encapsulation.Step 2: Evaluate options
Store locators as private By fields; create public methods to enter username, enter password, and click login follows best practice by separating locators and actions clearly.Final Answer:
Store locators as private By fields; create public methods to enter username, enter password, and click login -> Option AQuick Check:
Private locators + public methods = best design [OK]
- Making locators public and exposing WebElements
- Putting test logic in constructors
- Using static methods without instance context
Ready to prove it?
Pass the test and this pattern moves unknown
