Interview #104: Selenium: How to design TestNG framework from scratch?

Interview #104: Selenium: How to design TestNG framework from scratch?

Designing a Selenium TestNG framework from scratch requires a structured approach to ensure efficiency, maintainability, scalability, and reusability. Below is a step-by-step guide to creating a well-architected Selenium TestNG framework.

Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-9606623245

1. Understanding Framework Components

Before implementing the framework, it's crucial to understand the key components:

  • Selenium WebDriver – Automates browser actions.
  • TestNG – Provides test execution management.
  • Maven – Manages dependencies.
  • Extent Reports/Allure Reports – Generates test reports.
  • Log4j – Enables logging mechanisms.
  • Page Object Model (POM) – Enhances maintainability by separating test scripts from UI locators.
  • Data-Driven Testing (Excel, JSON, or Properties files) – Handles test data efficiently.
  • CI/CD (Jenkins, GitHub Actions, or Azure DevOps) – Ensures continuous integration and execution.


2. Setting Up the Project Structure

Step 1: Create a Maven Project

  1. Open IntelliJ IDEA or Eclipse.
  2. Create a new Maven Project (selenium-testng-framework).
  3. Add the following dependencies in the pom.xml:

pom.xml (Dependencies)

<dependencies>
    <!-- Selenium WebDriver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.10.0</version>
    </dependency>

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.7.0</version>
        <scope>test</scope>
    </dependency>

    <!-- WebDriver Manager for handling browser drivers -->
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.5.3</version>
    </dependency>

    <!-- Apache POI for Excel Data Handling -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.3</version>
    </dependency>

    <!-- Log4j for Logging -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.1</version>
    </dependency>

    <!-- Extent Reports for Test Reporting -->
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>5.0.9</version>
    </dependency>
</dependencies>        

3. Implementing Page Object Model (POM)

POM improves the readability and maintainability of test scripts by separating UI elements from test logic.

Step 2: Create Page Classes

Folder Structure

src/main/java/com/framework/pages/
src/test/java/com/framework/tests/        

LoginPage.java

package com.framework.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
    WebDriver driver;

    // Locators using @FindBy annotation
    @FindBy(id = "username") 
    private WebElement usernameField;

    @FindBy(id = "password") 
    private WebElement passwordField;

    @FindBy(id = "loginButton") 
    private WebElement loginButton;

    // Constructor
    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    // Page actions
    public void login(String username, String password) {
        usernameField.sendKeys(username);
        passwordField.sendKeys(password);
        loginButton.click();
    }
}        

4. Creating Base Test Class

Step 3: BaseTest.java for Test Setup & Teardown

package com.framework.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import io.github.bonigarcia.wdm.WebDriverManager;

public class BaseTest {
    protected WebDriver driver;

    @BeforeMethod
    public void setup() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://example.com");
    }

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

5. Writing Test Cases in TestNG

Step 4: Writing Test Cases Using TestNG

LoginTest.java

package com.framework.tests;

import com.framework.pages.LoginPage;
import org.testng.annotations.Test;

public class LoginTest extends BaseTest {

    @Test
    public void verifyLogin() {
        LoginPage loginPage = new LoginPage(driver);
        loginPage.login("testuser", "password123");
        System.out.println("Login Test Passed");
    }
}        

6. Implementing Data-Driven Testing

Step 5: Using Excel for Test Data

ExcelUtility.java

package com.framework.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelUtility {
    public static String getData(String sheetName, int row, int col) throws IOException {
        FileInputStream fis = new FileInputStream("TestData.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheet(sheetName);
        String data = sheet.getRow(row).getCell(col).getStringCellValue();
        workbook.close();
        return data;
    }
}        

Using Excel Data in Test Case

@Test
public void loginWithExcelData() throws IOException {
    String username = ExcelUtility.getData("Login", 1, 0);
    String password = ExcelUtility.getData("Login", 1, 1);
    
    LoginPage loginPage = new LoginPage(driver);
    loginPage.login(username, password);
}        

7. Adding Reports and Logging

Step 6: Configure Extent Reports

ExtentManager.java

package com.framework.utils;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;

public class ExtentManager {
    private static ExtentReports extent;

    public static ExtentReports getInstance() {
        if (extent == null) {
            ExtentSparkReporter reporter = new ExtentSparkReporter("Reports/TestReport.html");
            extent = new ExtentReports();
            extent.attachReporter(reporter);
        }
        return extent;
    }
}        

Integrate Reports in Test Case

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

public class LoginTest extends BaseTest {
    @Test
    public void verifyLogin() {
        ExtentTest test = ExtentManager.getInstance().createTest("Login Test");
        LoginPage loginPage = new LoginPage(driver);
        loginPage.login("testuser", "password123");
        test.log(Status.PASS, "Login successful");
    }
}        

8. Running Tests with TestNG XML

testng.xml

<suite name="TestSuite">
    <test name="LoginTests">
        <classes>
            <class name="com.framework.tests.LoginTest"/>
        </classes>
    </test>
</suite>        

Run with:

mvn test        

Conclusion

A well-structured Selenium TestNG framework includes: ✔ Page Object Model (POM) ✔ Data-driven Testing with Excel ✔ Logging with Log4j ✔ Reporting with Extent Reports ✔ Continuous Integration with Maven and Jenkins

By following these steps, you create a scalable, reusable, and maintainable Selenium TestNG framework from scratch. 🚀

Article content


Levent Ozturk

Senior SDET | UI & API Test Automation | Playwright, Selenium, RESTAssured | Java, JavaScript, SQL | CI/CD with Jenkins, Azure, Docker | Agile/Scrum | BDD with Cucumber & Gherkin | Payment processing | FinTech

5mo

I recently came across a concise yet impactful explanation of the Selenium-TestNG automation framework. The clarity and simplicity of the information were truly refreshing and easy to grasp. To further enhance understanding, I also utilize a visual map to tackle common automation framework interview questions. I wanted to share this valuable resource with you all. A big thank you to the original sharer for providing such accessible and mind-stimulating content! Feel free to adjust it to fit your style or add any additional details you'd like to include.

  • No alternative text description for this image

To view or add a comment, sign in

Others also viewed

Explore topics