Page class design helps organize web page elements and actions in one place. It makes tests easier to read and maintain.
When automating web tests to keep code clean and reusable.
When multiple tests interact with the same web page.
When you want to separate test logic from page details.
When you want to reduce duplicate code for locating elements.
When you want to improve test readability and maintenance.
Syntax
Selenium Java
public class PageName {
private WebDriver driver;
// Constructor
public PageName(WebDriver driver) {
this.driver = driver;
}
// Locators
private By elementLocator = By.id("elementId");
// Actions
public void clickElement() {
driver.findElement(elementLocator).click();
}
public String getElementText() {
return driver.findElement(elementLocator).getText();
}
}
The class holds locators and methods for one page.
The WebDriver is passed in the constructor to interact with the browser.
Examples
This page class models a login page with fields and a button.
Selenium Java
public class LoginPage {
private WebDriver driver;
private By usernameField = By.name("username");
private By passwordField = By.name("password");
private By loginButton = By.cssSelector("button[type='submit']");
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();
}
}
This page class models a home page with a welcome message element.
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 opens a browser, goes to the login page, enters credentials, clicks login, and prints the page title.
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 String getPageTitle() {
return driver.getTitle();
}
}
class TestLogin {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
LoginPage loginPage = new LoginPage(driver);
loginPage.enterUsername("testuser");
loginPage.enterPassword("password123");
loginPage.clickLogin();
String title = loginPage.getPageTitle();
System.out.println("Page title after login: " + title);
driver.quit();
}
}
OutputSuccess
Important Notes
Use clear and meaningful names for locators and methods.
Keep page classes focused on one page only.
Pass WebDriver from test to page class to avoid creating multiple drivers.
Summary
Page class design organizes web page elements and actions in one place.
It improves test code readability and reuse.
Each page class should have locators and methods for that page only.
Practice
(1/5)
1. What is the main purpose of a page class in Selenium Java?
easy
A. To handle API requests
B. To write test assertions directly
C. To group web page elements and actions in one place
D. To manage database connections
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 C
Quick Check:
Page class purpose = Group elements and actions [OK]
Hint: Page classes group elements and actions per page [OK]
Common Mistakes:
Confusing page classes with test classes
Using page classes for database or API logic
Writing assertions inside page classes
2. Which of the following is the correct way to declare a locator in a Selenium page class using Java?
easy
A. public void submitButton = driver.findElement(By.id("submit"));
B. public String submitButton = "submit";
C. private WebElement submitButton = driver.findElement(By.id("submit"));
D. private By submitButton = By.id("submit");
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 D
Quick Check:
Locators = By objects [OK]
Hint: Use By for locators, not WebElement directly [OK]
Common Mistakes:
Declaring locators as WebElement fields
Using public visibility for locators
Assigning void type to variables
3. Given the following page class snippet, what will 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();
}
}
medium
A. "Welcome Page"
B. "Login Page"
C. null
D. Throws NullPointerException
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 A
Quick Check:
driver.getTitle() = "Welcome Page" [OK]
Hint: driver.getTitle() returns current page title string [OK]
Common Mistakes:
Assuming getTitle() returns page class name
Expecting null if not set explicitly
Confusing with element text retrieval
4. Identify the error in this page class method:
public void clickLogin() {
WebElement loginBtn = driver.findElement(By.id("login"));
loginBtn.click();
}
medium
A. No error, method is correct
B. Should store locator as a By field, not find element inside method
C. Missing exception handling for element not found
D. Method should return WebElement instead of void
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 B
Quick Check:
Locators as By fields = best practice [OK]
Hint: Keep locators as fields, find elements in methods [OK]
Common Mistakes:
Finding elements repeatedly inside methods
Ignoring locator reuse
Assuming exception handling is mandatory here
5. You want to design a page class for a login page with username, password fields, and a login button. Which design approach is best to follow?
hard
A. Store locators as private By fields; create public methods to enter username, enter password, and click login
B. Store WebElement fields publicly and access them directly in tests
C. Write all test steps inside the page class constructor
D. Use static methods only without storing any locators
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 A
Quick Check:
Private locators + public methods = best design [OK]
Hint: Private locators + public action methods = good page class [OK]
Common Mistakes:
Making locators public and exposing WebElements
Putting test logic in constructors
Using static methods without instance context
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.