Framework Structure
-
`config.properties`: Stores configuration data.
-
`DriverManager.java`: Manages WebDriver lifecycle.
- `Constants.java`:
Stores constant values (e.g., file paths).
- `utils`: Utility
classes for XML, XLSX, JSON, and meta-programming concepts.
- src/test/java:
- `features`: Holds
`.feature` files.
- `stepDefinitions`:
Contains step implementations.
- `hooks.java`:
Defines Cucumber hooks.
- `testRunners`:
Cucumber runner classes.
- resources:
- Test data files:
`data.xml`, `data.xlsx`, `data.json`.
-
`config.properties`.
1. DriverManager.java
Manages WebDriver dynamically:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DriverManager {
private static
ThreadLocal<WebDriver> driver = new ThreadLocal<>();
public static void
initializeDriver(String browser) {
switch
(browser.toLowerCase()) {
case
"chrome":
System.setProperty("webdriver.chrome.driver",
"path/to/chromedriver");
driver.set(new ChromeDriver());
break;
case
"firefox":
System.setProperty("webdriver.gecko.driver",
"path/to/geckodriver");
driver.set(new FirefoxDriver());
break;
case
"edge":
System.setProperty("webdriver.edge.driver",
"path/to/msedgedriver");
driver.set(new EdgeDriver());
break;
default:
throw
new RuntimeException("Unsupported browser: " + browser);
}
getDriver().manage().window().maximize();
}
public static
WebDriver getDriver() {
return
driver.get();
}
public static void
quitDriver() {
getDriver().quit();
driver.remove();
}
}
2. config.properties
Contains configuration:
browser=chrome
baseURL=https://example.com
timeout=30
3. ConfigReader.java
Loads configuration dynamically:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigReader {
private static
Properties properties;
static {
try {
FileInputStream fis = new
FileInputStream("src/main/resources/config.properties");
properties
= new Properties();
properties.load(fis);
} catch
(IOException e) {
e.printStackTrace();
throw new
RuntimeException("Could not load config file.");
}
}
public static
String getProperty(String key) {
return
properties.getProperty(key);
}
}
4. hooks.java
Setup and teardown with Cucumber hooks:
java
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class Hooks {
@Before
public void
setUp() {
String browser
= ConfigReader.getProperty("browser");
DriverManager.initializeDriver(browser);
DriverManager.getDriver().get(ConfigReader.getProperty("baseURL"));
}
@After
public void
tearDown() {
DriverManager.quitDriver();
}
}
5. Dynamic Data Handling (XML, XLSX, JSON)
XML Utility
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
public class XMLUtils {
public static
String getValueFromXML(String filePath, String tagName) {
try {
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document
document = builder.parse(filePath);
document.getDocumentElement().normalize();
return
document.getElementsByTagName(tagName).item(0).getTextContent();
} catch
(Exception e) {
e.printStackTrace();
throw new
RuntimeException("Error reading XML file.");
}
}
}
XLSX Utility
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
public class ExcelUtils {
public static
String getCellValue(String filePath, String sheetName, int row, int col) {
try
(FileInputStream fis = new FileInputStream(filePath);
Workbook
workbook = new XSSFWorkbook(fis)) {
Sheet
sheet = workbook.getSheet(sheetName);
Cell cell
= sheet.getRow(row).getCell(col);
return
cell.toString();
} catch
(Exception e) {
e.printStackTrace();
throw new
RuntimeException("Error reading Excel file.");
}
}
}
JSON Utility
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.FileReader;
public class JSONUtils {
public static
String getValueFromJSON(String filePath, String key) {
try {
JSONParser
parser = new JSONParser();
Object obj
= parser.parse(new FileReader(filePath));
JSONObject
jsonObject = (JSONObject) obj;
return
jsonObject.get(key).toString();
} catch
(Exception e) {
e.printStackTrace();
throw new
RuntimeException("Error reading JSON file.");
}
}
}
6. Step Definitions with Meta-Programming
Generic step definitions for handling multiple data formats:
import io.cucumber.java.en.*;
import org.openqa.selenium.WebDriver;
public class StepDefinitions {
WebDriver driver =
DriverManager.getDriver();
@Given("user
navigates to {string}")
public void
userNavigatesTo(String url) {
driver.get(url);
}
@When("user
fetches data from {string} file {string} and key {string}")
public void
userFetchesDataFromFile(String fileType, String filePath, String key) {
String value =
null;
switch
(fileType.toLowerCase()) {
case
"xml":
value
= XMLUtils.getValueFromXML(filePath, key);
break;
case
"xlsx":
value
= ExcelUtils.getCellValue(filePath, "Sheet1",
Integer.parseInt(key.split(",")[0]),
Integer.parseInt(key.split(",")[1]));
break;
case
"json":
value
= JSONUtils.getValueFromJSON(filePath, key);
break;
default:
throw
new RuntimeException("Unsupported file type: " + fileType);
}
System.out.println("Value: " + value);
}
}
7. Feature File
Feature: Dynamic Data Handling
Scenario: Fetch data
from XML
Given user
navigates to "https://example.com"
When user fetches
data from "xml" file "src/test/resources/data.xml" and key
"username"
Scenario: Fetch data
from Excel
Given user
navigates to "https://example.com"
When user fetches
data from "xlsx" file "src/test/resources/data.xlsx" and
key "1,2"
Scenario: Fetch data
from JSON
Given user
navigates to "https://example.com"
When user fetches
data from "json" file "src/test/resources/data.json" and
key "password"
8. Maven Dependencies
<dependencies>
<!-- Selenium
-->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.x.x</version>
</dependency>
<!-- Cucumber
-->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.x.x</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.x.x</version>
</dependency>
<!-- JSON
-->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<!-- Excel
(Apache POI) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.x</version>
</dependency>
</dependencies>