DEV Community

Cover image for Day 03 - Building a Complete Selenium Java Framework - The Backbone: Base Class and Configuration
Utkarsh
Utkarsh

Posted on

Day 03 - Building a Complete Selenium Java Framework - The Backbone: Base Class and Configuration

Day 03 – The Backbone: Base Class and Configuration

On Day 01, we planned the framework.
On Day 02, we created the project and structure.

Now on Day 03, we build the most important part of the framework—the Base Class.

If this framework were a building, the Base Class would be the foundation.
Every test we write later will stand on top of it.


🎯 Why Do We Need a Base Class?

In automation testing, almost every test does the same starting work:

  • Launch the browser
  • Navigate to the application URL
  • Apply waits
  • Close the browser after execution

If we write this logic inside every test, our code becomes:

  • Repetitive
  • Hard to maintain
  • Error-prone

The Base Class solves this problem by:

  • Centralising browser setup and teardown
  • Loading configuration values
  • Ensuring all tests behave consistently

Every test class will simply extend the Base Class.


Step 1: Understanding config.properties

Hard-coding values like browser name or URL inside Java code is a bad practice.

Instead, we use a configuration file:

  • Easy to change
  • No recompilation needed
  • Supports multiple environments

📄 config.properties (Initial Version)

This file lives inside:
src/main/resources/config.properties

# Application Settings
url=https://opensource-demo.orangehrmlive.com/
browser=chrome
implicit_wait=10
Enter fullscreen mode Exit fullscreen mode

🔍 What Each Property Means

  • url → Application under test
  • browser → Which browser to launch
  • implicit_wait → Global wait time for element search

Later, we’ll add more values as the framework grows.


Step 2: Creating the Base Class

Now we write the Base Class, which will:

  • Load the configuration file
  • Launch the browser
  • Apply waits
  • Navigate to the application
  • Close the browser after execution

📁 File Location

Create the file here:

src/main/java
 └── com.orangehrm.base
     └── BaseClass.java
Enter fullscreen mode Exit fullscreen mode

🧩 BaseClass.java – Complete Implementation

package com.orangehrm.base;

import java.io.FileInputStream;
import java.util.Properties;
import java.time.Duration;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BaseClass {

    protected static Properties prop;
    protected static WebDriver driver;

    // Load configuration from config.properties
    public void loadConfig() {
        try {
            prop = new Properties();
            FileInputStream fis = new FileInputStream(
                System.getProperty("user.dir") + "/src/main/resources/config.properties"
            );
            prop.load(fis);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @BeforeMethod
    public void setup() {
        loadConfig();

        String browserName = prop.getProperty("browser");

        // Browser selection
        if (browserName.equalsIgnoreCase("chrome")) {
            driver = new ChromeDriver();
        } else if (browserName.equalsIgnoreCase("firefox")) {
            driver = new FirefoxDriver();
        } else if (browserName.equalsIgnoreCase("edge")) {
            driver = new EdgeDriver();
        }

        // Browser management
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(
            Duration.ofSeconds(Integer.parseInt(prop.getProperty("implicit_wait")))
        );

        // Launch application
        driver.get(prop.getProperty("url"));
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Important Concepts You Used Today

🔹 Selenium Manager (Selenium 4)

You’ll notice we did not use:

System.setProperty("webdriver.chrome.driver", "path");
Enter fullscreen mode Exit fullscreen mode

Selenium 4 introduces Selenium Manager, which:

  • Automatically downloads drivers
  • Manages driver paths internally
  • Reduces setup headaches

🔹 Implicit Wait

  • Applied globally for the entire session
  • Selenium waits up to the defined time before throwing NoSuchElementException
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Enter fullscreen mode Exit fullscreen mode

🔹 Access Modifiers

We used protected for:

protected static WebDriver driver;
protected static Properties prop;
Enter fullscreen mode Exit fullscreen mode

Why?

  • Child classes (test classes) can access them
  • Still protected from outside misuse

🔹 TestNG Annotations

  • @BeforeMethod
    Runs before every test method
    → Browser always starts fresh

  • @AfterMethod
    Runs after every test method
    → Browser always closes cleanly

This guarantees test isolation.


Step 4: Validating the Backbone (Very Important)

Before moving forward, we must confirm:
👉 The Base Class actually works.


🧪 Create a Dummy Test

Create this file:

src/test/java
 └── DummyTest.java
Enter fullscreen mode Exit fullscreen mode
import org.testng.annotations.Test;
import com.orangehrm.base.BaseClass;

public class DummyTest extends BaseClass {

    @Test
    public void testBaseSetup() {
        // No code needed
        // Browser should open and close automatically
    }
}
Enter fullscreen mode Exit fullscreen mode

✅ Expected Behaviour

When you run this test:

  1. Browser opens
  2. Navigates to Orange HRM login page
  3. Maximises window
  4. Waits applied
  5. Browser closes after execution

If this happens → Day 03 is successful 🎉


📌 Day 03 Summary

Today, you built:

  • A centralised Base Class
  • Configuration-driven browser setup
  • Clean startup and shutdown logic
  • Selenium 4-ready WebDriver initialization

This Base Class will be reused by every test going forward.


🚀 What’s Next?

On Day 04, we will:

  • Create the Action Driver
  • Centralise reusable actions like:

    • click
    • sendKeys
    • waits

This will make test scripts short, clean, and readable.

You’ve just completed the core backbone of the framework 💪

Top comments (0)