The base page class pattern helps keep your test code clean and easy to maintain by putting common web page actions in one place.
When you have many web pages with similar actions like clicking or typing.
When you want to avoid repeating the same code in multiple test files.
When you want to make your tests easier to update if the website changes.
When you want to organize your Selenium tests better for teamwork.
When you want to write clear and simple test scripts that focus on test logic.
Syntax
Selenium Java
public class BasePage {
protected WebDriver driver;
public BasePage(WebDriver driver) {
this.driver = driver;
}
public void click(By locator) {
driver.findElement(locator).click();
}
public void type(By locator, String text) {
driver.findElement(locator).sendKeys(text);
}
public String getText(By locator) {
return driver.findElement(locator).getText();
}
}
The BasePage class holds common methods like click, type, and getText.
Each page class will extend BasePage to reuse these methods.
Examples
This LoginPage class uses BasePage methods to perform login steps.
Selenium Java
public class LoginPage extends BasePage {
private By usernameField = By.id("username");
private By passwordField = By.id("password");
private By loginButton = By.id("loginBtn");
public LoginPage(WebDriver driver) {
super(driver);
}
public void login(String user, String pass) {
type(usernameField, user);
type(passwordField, pass);
click(loginButton);
}
}
HomePage class reuses getText method from BasePage to get welcome message.
Selenium Java
public class HomePage extends BasePage {
private By welcomeMessage = By.id("welcomeMsg");
public HomePage(WebDriver driver) {
super(driver);
}
public String getWelcomeText() {
return getText(welcomeMessage);
}
}
Sample Program
This test opens a browser, goes to login page, uses LoginPage class to login, then checks if login succeeded by URL.
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BasePage {
protected WebDriver driver;
public BasePage(WebDriver driver) {
this.driver = driver;
}
public void click(By locator) {
driver.findElement(locator).click();
}
public void type(By locator, String text) {
driver.findElement(locator).sendKeys(text);
}
public String getText(By locator) {
return driver.findElement(locator).getText();
}
}
class LoginPage extends BasePage {
private By usernameField = By.id("username");
private By passwordField = By.id("password");
private By loginButton = By.id("loginBtn");
public LoginPage(WebDriver driver) {
super(driver);
}
public void login(String user, String pass) {
type(usernameField, user);
type(passwordField, pass);
click(loginButton);
}
}
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.login("user1", "pass123");
// Assume after login, URL changes to /home
if (driver.getCurrentUrl().contains("/home")) {
System.out.println("Login test passed");
} else {
System.out.println("Login test failed");
}
driver.quit();
}
}
OutputSuccess
Important Notes
Always keep locators private in page classes to protect them from outside changes.
Use clear method names in page classes to describe user actions.
Remember to close the browser after tests to free resources.
Summary
Base page class groups common web actions to avoid code repetition.
Page classes extend base page to reuse methods and keep tests clean.
This pattern makes tests easier to read, write, and maintain.
Practice
(1/5)
1. What is the main purpose of a BasePage class in Selenium Java testing?
easy
A. To define the main method for running tests
B. To store test data for different test cases
C. To group common web actions and avoid code repetition across page classes
D. To handle database connections during tests
Solution
Step 1: Understand the role of BasePage
The BasePage class contains common methods like click, type, and wait that many page classes use.
Step 2: Identify the benefit
By grouping these common actions, BasePage helps avoid repeating the same code in every page class.
Final Answer:
To group common web actions and avoid code repetition across page classes -> Option C
Quick Check:
BasePage groups common actions = A [OK]
Hint: BasePage holds shared actions to reuse in all pages [OK]
Common Mistakes:
Confusing BasePage with test data storage
Thinking BasePage runs tests directly
Mixing BasePage with database handling
2. Which of the following is the correct way to declare a BasePage class constructor that accepts a WebDriver instance in Java?
easy
A. public BasePage(WebDriver driver) { this.driver = driver; }
B. public void BasePage(WebDriver driver) { this.driver = driver; }
C. BasePage(WebDriver driver) { driver = this.driver; }
D. public BasePage() { WebDriver driver; }
Solution
Step 1: Identify correct constructor syntax
In Java, constructors have no return type and match the class name exactly.
Step 2: Check parameter assignment
The constructor should assign the passed driver to the class field using this.driver = driver;.
Final Answer:
public BasePage(WebDriver driver) { this.driver = driver; } -> Option A
Quick Check:
Constructor no return + this.driver = driver [OK]
Hint: Constructor has no return type and uses this.driver = driver [OK]
Common Mistakes:
Adding void return type to constructor
Reversing assignment order
Declaring driver inside constructor without class field
3. Given the following BasePage method, what will be the output if the element is not found within 5 seconds?
public void clickElement(By locator) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
System.out.println("Clicked successfully");
}
medium
A. Prints 'Clicked successfully' after clicking the element
B. Prints 'Clicked successfully' immediately without waiting
C. Throws NoSuchElementException and prints 'Clicked successfully'
D. Throws TimeoutException and does not print anything
Solution
Step 1: Understand WebDriverWait behavior
The wait.until waits up to 5 seconds for the element to be clickable.
Step 2: Outcome if element not found
If the element is not clickable within 5 seconds, wait.until throws a TimeoutException and the click and print statements are skipped.
Final Answer:
Throws TimeoutException and does not print anything -> Option D
Quick Check:
Wait fails = TimeoutException, no print [OK]
Hint: Wait throws TimeoutException if element not clickable in time [OK]
Common Mistakes:
Assuming print runs even if element not found
Confusing TimeoutException with NoSuchElementException
Thinking click happens immediately without wait
4. Identify the error in this BasePage method and how to fix it:
public void enterText(By locator, String text) {
WebElement element = driver.findElement(locator);
element.sendKeys();
}
medium
A. sendKeys() is missing the text argument; fix by adding sendKeys(text)
B. findElement should be replaced with findElements
C. Method should return boolean indicating success
D. Locator type By is incorrect; use String instead
Solution
Step 1: Check sendKeys usage
The sendKeys method requires a String argument to input text.
Step 2: Fix the method call
Change element.sendKeys(); to element.sendKeys(text); to send the provided text.
Final Answer:
sendKeys() is missing the text argument; fix by adding sendKeys(text) -> Option A
Quick Check:
sendKeys needs text argument [OK]
Hint: sendKeys always needs the text argument [OK]
Common Mistakes:
Calling sendKeys without arguments
Using findElements instead of findElement for single element
Changing locator type incorrectly
5. You have a BasePage with a method public boolean isElementVisible(By locator) that returns true if element is visible. How would you use this method in a LoginPage class to verify the login button is visible before clicking it?
hard
A. if (isElementVisible()) { clickElement(loginButtonLocator); }
B. if (isElementVisible(loginButtonLocator)) { clickElement(loginButtonLocator); }
C. clickElement(loginButtonLocator); isElementVisible(loginButtonLocator);
D. isElementVisible(loginButtonLocator); clickElement();
Solution
Step 1: Use isElementVisible with locator argument
The method requires a By locator to check visibility, so pass loginButtonLocator.
Step 2: Conditionally click if visible
Use an if statement to call clickElement(loginButtonLocator) only if the element is visible.
Final Answer:
if (isElementVisible(loginButtonLocator)) { clickElement(loginButtonLocator); } -> Option B
Quick Check:
Check visibility with locator before click [OK]
Hint: Check visibility with locator before clicking [OK]
Common Mistakes:
Calling isElementVisible without locator argument
Clicking without checking visibility
Calling clickElement without locator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.