---Advertisement---

Selenium Framework: Data-Driven, Keyword-Driven & Hybrid Frameworks Great 2025

By Manisha

Updated On:

---Advertisement---
Selenium Framework

📌What is a Selenium Framework?

A Selenium Framework is a structured set of automation code that enhances test script maintainability, reusability, and readability. Without a framework, code and test data are often mixed, making scripts difficult to manage and reuse.

Better Code Reusability – Reduces redundant code.
Higher Portability – Frameworks can be used across multiple environments.
Efficient Maintenance – Separation of test data and test scripts simplifies updates.
Improved Code Readability – Organized structure makes debugging easier.


📌Types of Selenium Frameworks

Selenium WebDriver supports three primary test automation frameworks:

1️⃣ Data-Driven Framework – Uses external data sources (Excel, CSV, XML, or databases) to drive test cases.
2️⃣ Keyword-Driven Framework – Uses predefined keywords stored in external files (Excel) to execute actions.
3️⃣ Hybrid Framework – Combines Data-Driven and Keyword-Driven frameworks for maximum flexibility.


📌1. Data-Driven Framework in Selenium

A Data-Driven Framework separates test case logic from the data, allowing test cases to be executed with multiple data sets. Instead of hardcoding values, test data is stored in external sources like:
Excel files (.xls/.xlsx) – Apache POI library
CSV files – OpenCSV
Database tables – JDBC connectivity

How Does Data-Driven Testing Work?

1️⃣ Test data is stored in external sources.
2️⃣ Automation script fetches data dynamically.
3️⃣ Same script runs multiple times with different inputs.

📌Example: Reading Data from Excel Using Apache POI

xml<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
javaimport java.io.File;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;

public class ExcelReader {
public static String getCellValue(String filePath, int row, int col) throws Exception {
FileInputStream file = new FileInputStream(new File(filePath));
Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(0);
return sheet.getRow(row).getCell(col).toString();
}
}
javaimport org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class DataDrivenTest {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");

String username = ExcelReader.getCellValue("testdata.xlsx", 1, 0);
String password = ExcelReader.getCellValue("testdata.xlsx", 1, 1);

driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("login")).click();

driver.quit();
}
}

📌2. Keyword-Driven Framework in Selenium

A Keyword-Driven Framework uses external files (such as Excel sheets) to store keywords that define test actions. The automation script reads these keywords and performs the corresponding actions.

1️⃣ Test keywords are stored in an external file.
2️⃣ Selenium script reads the keywords dynamically.
3️⃣ Each keyword corresponds to an action (click, enter text, etc.).

Test Case File (TestCases.xlsx) – Contains test steps and keywords.
Object Repository (object.properties) – Stores element locators.
Driver Script (ExecuteTest.java) – Reads keywords and performs actions.

📌Example: Implementing a Keyword-Driven Framework

Step No.KeywordObject NameObject TypeValue
1GotourlURLhttps://example.com
2SetTextusernameIDtestuser
3SetTextpasswordIDtestpass
4ClickloginID
javaimport org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class KeywordDrivenTest {
WebDriver driver;

public void performAction(String keyword, String locator, String locatorType, String value) {
switch (keyword) {
case "Goto":
driver = new ChromeDriver();
driver.get(value);
break;
case "SetText":
driver.findElement(getBy(locator, locatorType)).sendKeys(value);
break;
case "Click":
driver.findElement(getBy(locator, locatorType)).click();
break;
}
}

public By getBy(String locator, String type) {
switch (type) {
case "ID": return By.id(locator);
case "Name": return By.name(locator);
case "XPath": return By.xpath(locator);
default: return null;
}
}
}

📌3. Hybrid Framework in Selenium

Selenium Framework: A Hybrid-Driven Framework combines both Data-Driven and Keyword-Driven methodologies to create a flexible and scalable test framework.

Uses keywords to define test actions (Keyword-Driven).
Uses external data sources for dynamic input (Data-Driven).
Allows non-programmers to write test cases.

📌Hybrid Framework Implementation

  • Selenium Framework: Replace ExecuteTest.java with HybridExecuteTest.java
  • Use TestNG DataProvider for test data management
javaimport org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class HybridTest {
@DataProvider(name = "testData")
public Object[][] getData() {
return new Object[][] {
{"https://example.com", "testuser", "testpass"},
{"https://example.com", "admin", "adminpass"}
};
}

@Test(dataProvider = "testData")
public void testLogin(String url, String username, String password) {
KeywordDrivenTest test = new KeywordDrivenTest();
test.performAction("Goto", "", "", url);
test.performAction("SetText", "username", "ID", username);
test.performAction("SetText", "password", "ID", password);
test.performAction("Click", "login", "ID", "");
}
}

📌Summary

🔹 Data-Driven Framework – Uses external data sources to drive tests.
🔹 Keyword-Driven Framework – Uses keywords in external files to define test actions.
🔹 Hybrid Framework – Combines Data-Driven and Keyword-Driven approaches for better flexibility.

Why Use a Selenium Framework?
✅ Reduces script maintenance efforts.
✅ Improves test reusability and scalability.
✅ Supports automation without deep programming knowledge.


Need help setting up a Selenium framework for your project? Let me know!

Maven and Jenkins

Leave a Comment

Index