Session 8: Data Driven Framework
- Types of Framework
- Data Driven Framework
- Building Data Driven Framework with Excel
Project URL: https://github.com/remotecontainer/DataDrivenFrameworkWithExcel.git
Types of Framework
types_of_framework.pptx |
Build Maven TestNG Framework
Steps:
Step 1: Create a Maven Project with group ID and Artifact ID (See session 1),
Step 2: Import Following Dependencies in POM file and click Ctrl+S to save/import, (you will need selenium-java, TestNG, POI, and POI-OOXML).
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11-beta2</version>
</dependency>
Step 3: Create a folder called "driver" and save the chromedriver.exe file in that folder.
Step 4: You should have two folders called MAIN and TEST
A. Create two packages called "page" and "util" inside the MAIN folder,
B. Create a package called "test" inside the TEST folder,
Step 5: Right click on project > Run As > Maven Build > Goals: clean install > Run.
Step 2: Import Following Dependencies in POM file and click Ctrl+S to save/import, (you will need selenium-java, TestNG, POI, and POI-OOXML).
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11-beta2</version>
</dependency>
Step 3: Create a folder called "driver" and save the chromedriver.exe file in that folder.
Step 4: You should have two folders called MAIN and TEST
A. Create two packages called "page" and "util" inside the MAIN folder,
B. Create a package called "test" inside the TEST folder,
Step 5: Right click on project > Run As > Maven Build > Goals: clean install > Run.
Class Code
ExcelReader:
// Global Variables
public String path;
public FileInputStream fis = null;
private XSSFWorkbook workbook = null;
private XSSFSheet sheet = null;
private XSSFRow row = null;
private XSSFCell cell = null;
// Constructor to initialize variables
public ExcelReader(String path) {
this.path = path;
try {
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to call the value
public String getCellData(String sheetName, String colName, int rowNum) {
// For Sheet
int index = workbook.getSheetIndex(sheetName);
int col_Num = 0;
sheet = workbook.getSheetAt(index);
// For Row
row = sheet.getRow(0);
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i).getStringCellValue().trim().equals(colName.trim())) {
col_Num = i;
}
}
// For Column
row = sheet.getRow(rowNum - 1);
cell = row.getCell(col_Num);
return cell.getStringCellValue();
}
LoginTest:
ExcelReader reader = new ExcelReader("./data/testdata.xlsx");
String validuserUsername = reader.getCellData("Sheet1", "username", 2);
String validPassword = reader.getCellData("Sheet1", "password", 2);
String invalidUsername = reader.getCellData("Sheet1", "username", 3);
String invalidPassword = reader.getCellData("Sheet1", "password", 3);
// Global Variables
public String path;
public FileInputStream fis = null;
private XSSFWorkbook workbook = null;
private XSSFSheet sheet = null;
private XSSFRow row = null;
private XSSFCell cell = null;
// Constructor to initialize variables
public ExcelReader(String path) {
this.path = path;
try {
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to call the value
public String getCellData(String sheetName, String colName, int rowNum) {
// For Sheet
int index = workbook.getSheetIndex(sheetName);
int col_Num = 0;
sheet = workbook.getSheetAt(index);
// For Row
row = sheet.getRow(0);
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i).getStringCellValue().trim().equals(colName.trim())) {
col_Num = i;
}
}
// For Column
row = sheet.getRow(rowNum - 1);
cell = row.getCell(col_Num);
return cell.getStringCellValue();
}
LoginTest:
ExcelReader reader = new ExcelReader("./data/testdata.xlsx");
String validuserUsername = reader.getCellData("Sheet1", "username", 2);
String validPassword = reader.getCellData("Sheet1", "password", 2);
String invalidUsername = reader.getCellData("Sheet1", "username", 3);
String invalidPassword = reader.getCellData("Sheet1", "password", 3);