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.
Practice
BasePage class in Selenium Java testing?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 CQuick Check:
BasePage groups common actions = A [OK]
- Confusing BasePage with test data storage
- Thinking BasePage runs tests directly
- Mixing BasePage with database handling
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 usingthis.driver = driver;.Final Answer:
public BasePage(WebDriver driver) { this.driver = driver; } -> Option AQuick Check:
Constructor no return + this.driver = driver [OK]
- Adding void return type to constructor
- Reversing assignment order
- Declaring driver inside constructor without class field
public void clickElement(By locator) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
System.out.println("Clicked successfully");
}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 DQuick Check:
Wait fails = TimeoutException, no print [OK]
- Assuming print runs even if element not found
- Confusing TimeoutException with NoSuchElementException
- Thinking click happens immediately without wait
public void enterText(By locator, String text) {
WebElement element = driver.findElement(locator);
element.sendKeys();
}Solution
Step 1: Check sendKeys usage
The sendKeys method requires a String argument to input text.Step 2: Fix the method call
Changeelement.sendKeys();toelement.sendKeys(text);to send the provided text.Final Answer:
sendKeys() is missing the text argument; fix by adding sendKeys(text) -> Option AQuick Check:
sendKeys needs text argument [OK]
- Calling sendKeys without arguments
- Using findElements instead of findElement for single element
- Changing locator type incorrectly
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?Solution
Step 1: Use isElementVisible with locator argument
The method requires a By locator to check visibility, so passloginButtonLocator.Step 2: Conditionally click if visible
Use an if statement to callclickElement(loginButtonLocator)only if the element is visible.Final Answer:
if (isElementVisible(loginButtonLocator)) { clickElement(loginButtonLocator); } -> Option BQuick Check:
Check visibility with locator before click [OK]
- Calling isElementVisible without locator argument
- Clicking without checking visibility
- Calling clickElement without locator
Ready to prove it?
Pass the test and this pattern moves unknown
