What if fixing one button could update all your page tests at once?
Why Base page class pattern in Selenium Java? - Purpose & Use Cases
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.
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 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.
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(); } }
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); } }
It enables fast, easy updates and cleaner test code by reusing common page actions everywhere.
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.
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.