Answer
BasePage Class in POM
BasePage is the parent class that all Page Object classes extend. It centralises:
- ✓WebDriver reference
- ✓WebDriverWait instance
- ✓Common utility methods (click, type, wait, scroll, check visibility)
BasePage Implementation
Java
// BasePage.java
public class BasePage {
protected WebDriver driver;
protected WebDriverWait wait;
public BasePage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, Duration.ofSeconds(15));
}
// ── Element interactions ───────────────────────────────────────
protected void click(By locator) {
waitForClickable(locator).click();
}
protected void type(By locator, String text) {
WebElement el = waitForVisible(locator);
el.clear();
el.sendKeys(text);
}
protected String getText(By locator) {
return waitForVisible(locator).getText().trim();
}
protected String getAttributeValue(By locator, String attr) {
return waitForPresent(locator).getAttribute(attr);
}
protected boolean isDisplayed(By locator) {
try {
return driver.findElement(locator).isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}
// ── Wait utilities ─────────────────────────────────────────────
protected WebElement waitForVisible(By locator) {
return wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
protected WebElement waitForClickable(By locator) {
return wait.until(ExpectedConditions.elementToBeClickable(locator));
}
protected WebElement waitForPresent(By locator) {
return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
}
protected void waitForInvisible(By locator) {
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
}
protected void waitForTextInElement(By locator, String text) {
wait.until(ExpectedConditions.textToBePresentInElementLocated(locator, text));
}
// ── Navigation ─────────────────────────────────────────────────
protected void navigateTo(String url) {
driver.get(url);
}
protected String getCurrentUrl() {
return driver.getCurrentUrl();
}
protected String getTitle() {
return driver.getTitle();
}
// ── Scroll ────────────────────────────────────────────────────
protected void scrollToElement(By locator) {
WebElement el = driver.findElement(locator);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el);
}
protected void scrollToBottom() {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
// ── Dropdown ──────────────────────────────────────────────────
protected void selectByText(By locator, String text) {
new Select(waitForPresent(locator)).selectByVisibleText(text);
}
protected void selectByValue(By locator, String value) {
new Select(waitForPresent(locator)).selectByValue(value);
}
// ── Screenshot ────────────────────────────────────────────────
protected void takeScreenshot(String name) {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(src, new File("screenshots/" + name + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Page Classes Extending BasePage
Java
// LoginPage.java
public class LoginPage extends BasePage {
// Locators
private By emailInput = By.id("email");
private By passwordInput = By.id("password");
private By loginBtn = By.cssSelector("[data-testid='login-btn']");
private By errorMsg = By.className("error-msg");
public LoginPage(WebDriver driver) {
super(driver); // calls BasePage constructor
}
public DashboardPage login(String email, String password) {
type(emailInput, email); // BasePage method
type(passwordInput, password); // BasePage method
click(loginBtn); // BasePage method
return new DashboardPage(driver);
}
public String getError() {
return getText(errorMsg); // BasePage method
}
}
// DashboardPage.java
public class DashboardPage extends BasePage {
private By welcomeMsg = By.cssSelector("h1.welcome");
private By logoutBtn = By.id("logout");
public DashboardPage(WebDriver driver) {
super(driver);
}
public boolean isWelcomeDisplayed() {
return isDisplayed(welcomeMsg); // BasePage method
}
public LoginPage logout() {
click(logoutBtn); // BasePage method
return new LoginPage(driver);
}
}
Benefits of BasePage
- ✓One place to modify wait strategy (e.g., change 15s → 20s)
- ✓All pages automatically get updated utility methods
- ✓No copy-paste of
new WebDriverWait(driver, Duration.ofSeconds(15))in every page - ✓Easy to add new helpers (hover, JS click, file upload) — all pages inherit it
