0
0
Selenium Javatesting~3 mins

Why Base page class pattern in Selenium Java? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if fixing one button could update all your page tests at once?

The Scenario

Imagine testing a website with many pages. Each page has buttons, links, and forms. You write separate code for each page's actions again and again.

The Problem

This manual way means repeating code everywhere. If a button changes, you must fix it in many places. It takes too long and causes mistakes.

The Solution

The base page class pattern creates one main page class with common code. Other page classes inherit from it. This way, shared code is written once and reused.

Before vs After
Before
public class LoginPage {
  WebDriver driver;
  public void clickButton() { driver.findElement(By.id("btn")).click(); }
}
public class HomePage {
  WebDriver driver;
  public void clickButton() { driver.findElement(By.id("btn")).click(); }
}
After
public class BasePage {
  WebDriver driver;
  public BasePage(WebDriver driver) {
    this.driver = driver;
  }
  public void clickButton() { driver.findElement(By.id("btn")).click(); }
}
public class LoginPage extends BasePage {
  public LoginPage(WebDriver driver) {
    super(driver);
  }
}
public class HomePage extends BasePage {
  public HomePage(WebDriver driver) {
    super(driver);
  }
}
What It Enables

It enables fast, easy updates and cleaner test code by reusing common page actions everywhere.

Real Life Example

When a website changes its menu button ID, you update it once in the base page class. All tests using that button work without extra fixes.

Key Takeaways

Manual page code repeats and is hard to maintain.

Base page class holds shared code for all pages.

Inheritance lets pages reuse common actions easily.