#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Java for Selenium

Advanced Java Programming: Interfaces, Inheritance, Collections, Exceptions

Interfaces

Define a contract of methods a class must implement, using the implements keyword — commonly used in Selenium frameworks to define common page behaviors.

Inheritance

Lets a class reuse another class's fields and methods via extends — the basis for Selenium's common "BaseTest" or "BasePage" pattern.

Collections

List, Set, and Map structures are used constantly to store WebElements, test data, or expected results.

Exceptions

Proper try-catch handling is essential in Selenium, since operations like locating an element that isn't yet present will throw exceptions that scripts must handle gracefully.

Interfaces and Inheritance in a Page Object Framework

Selenium itself is built on interfaces. WebDriver is an interface, and ChromeDriver or FirefoxDriver are classes that implement it. That is why you write WebDriver driver = new ChromeDriver();: the rest of your code depends only on the contract, so switching browsers changes a single line.

Your own framework can follow the same idea. A base class holds shared setup and helpers, and each page class inherits from it:

public interface Loadable {
    boolean isLoaded();
}

public abstract class BasePage implements Loadable {
    protected final WebDriver driver;
    protected final WebDriverWait wait;

    protected BasePage(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    protected void click(By locator) {
        wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
    }
}

public class LoginPage extends BasePage {
    private final By email = By.id("email");
    private final By submit = By.cssSelector("button[type='submit']");

    public LoginPage(WebDriver driver) { super(driver); }

    @Override
    public boolean isLoaded() {
        return driver.getCurrentUrl().contains("/login");
    }

    public void loginAs(String user) {
        driver.findElement(email).sendKeys(user);
        click(submit);
    }
}

Every page now gets the explicit wait and the safe click helper for free, and each must say how to tell that it has loaded.

Choosing the Right Collection for Test Data

CollectionSelenium use case
List<WebElement>Result of findElements, such as all rows in a table; keeps order
Set<String>driver.getWindowHandles() returns a set of unique window ids
Map<String, String>Test data by key, like field name to value, or expected labels by locale
List<WebElement> prices = driver.findElements(By.cssSelector(".price"));
List<Double> values = new ArrayList<>();
for (WebElement p : prices) {
    values.add(Double.parseDouble(p.getText().replace(",", "")));
}
List<Double> sorted = new ArrayList<>(values);
Collections.sort(sorted);
Assert.assertEquals(values, sorted, "Prices are not in ascending order");

Note that findElements returns an empty list when nothing matches, while findElement throws an exception. Checking isEmpty() is a clean way to confirm that something is absent.

Exceptions You Will Meet Most Often

  • NoSuchElementException: the locator matched nothing. Check the locator, frames and timing.
  • TimeoutException: an explicit wait gave up. The condition never became true in time.
  • StaleElementReferenceException: the page re-rendered and your saved element reference is no longer attached. Find the element again.
  • ElementClickInterceptedException: another element, such as a cookie banner, is covering the target.

Handle these with waits and clear failure messages rather than swallowing them. Catching an exception should either recover in a known way, such as retrying a stale lookup once, or rethrow with context that helps the next person debug.

You can also define your own exception for framework-level problems, for example class PageNotLoadedException extends RuntimeException, thrown when isLoaded() returns false. A specific name in the stack trace tells the reader straight away that navigation failed, rather than leaving them to guess from a generic error several steps later.

Habits That Keep Framework Code Clean

  • Avoid empty catch blocks; a hidden failure makes a broken test look green.
  • Replace Thread.sleep with explicit waits tied to a real condition.
  • Prefer composition for unrelated helpers instead of building deep inheritance chains.
  • Program to interfaces, such as List and WebDriver, not concrete classes.
  • Always call driver.quit() in an after-method so browsers do not pile up.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course