Skip to content

Commit 8fd4554

Browse files
committed
Bai 23 - Excel file - DataProvider
1 parent 213b767 commit 8fd4554

File tree

6 files changed

+218
-18
lines changed

6 files changed

+218
-18
lines changed

src/main/java/com/anhtester/helpers/ExcelHelpers.java

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
66

77
import java.io.*;
8+
import java.util.Arrays;
89
import java.util.HashMap;
910
import java.util.Hashtable;
1011
import java.util.Map;
@@ -81,7 +82,7 @@ public Row getRowData(int rowNum) {
8182
return row;
8283
}
8384

84-
85+
//Get Excel data from the sheet
8586
public Object[][] getExcelData(String excelPath, String sheetName) {
8687
Object[][] data = null;
8788
Workbook workbook = null;
@@ -123,7 +124,7 @@ public Object[][] getExcelData(String excelPath, String sheetName) {
123124
int noOfRows = sheet.getPhysicalNumberOfRows();
124125
int noOfCols = row.getLastCellNum();
125126

126-
System.out.println(noOfRows + " - " + noOfCols);
127+
System.out.println("Row: " + (noOfRows - 1) + " - Column: " + noOfCols);
127128

128129
Cell cell;
129130
data = new Object[noOfRows - 1][noOfCols];
@@ -206,6 +207,130 @@ public Object[][] getDataHashTable(String excelPath, String sheetName, int start
206207

207208
}
208209

210+
// Get data from specific rows
211+
public Object[][] getDataFromSpecificRows(String excelPath, String sheetName, int[] rowNumbers) {
212+
System.out.println("Excel File: " + excelPath);
213+
System.out.println("Sheet Name: " + sheetName);
214+
System.out.println("Reading data from specific rows: " + Arrays.toString(rowNumbers));
215+
216+
Object[][] data = null;
217+
218+
try {
219+
File f = new File(excelPath);
220+
221+
if (!f.exists()) {
222+
System.out.println("File Excel path not found.");
223+
throw new FileNotFoundException("File Excel path not found.");
224+
}
225+
226+
fis = new FileInputStream(excelPath);
227+
workbook = WorkbookFactory.create(fis);
228+
sheet = workbook.getSheet(sheetName);
229+
230+
if (sheet == null) {
231+
System.out.println("Sheet name not found.");
232+
throw new RuntimeException("Sheet name not found.");
233+
}
234+
235+
int columns = getColumns();
236+
System.out.println("Column count: " + columns);
237+
238+
// Khởi tạo mảng data với kích thước bằng số lượng dòng được chỉ định
239+
data = new Object[rowNumbers.length][columns];
240+
241+
// Đọc dữ liệu từ các dòng được chỉ định
242+
for (int i = 0; i < rowNumbers.length; i++) {
243+
int rowNum = rowNumbers[i];
244+
// Kiểm tra xem dòng có tồn tại không
245+
if (rowNum > sheet.getLastRowNum()) {
246+
System.out.println("WARNING: Row " + rowNum + " does not exist in the sheet.");
247+
// Gán giá trị rỗng cho dòng không tồn tại
248+
for (int j = 0; j < columns; j++) {
249+
data[i][j] = "";
250+
}
251+
continue;
252+
}
253+
254+
for (int j = 0; j < columns; j++) {
255+
data[i][j] = getCellData(rowNum, j);
256+
}
257+
}
258+
259+
// Đóng workbook và FileInputStream
260+
workbook.close();
261+
fis.close();
262+
263+
} catch (Exception e) {
264+
System.out.println("Exception in getDataFromSpecificRows: " + e.getMessage());
265+
e.printStackTrace();
266+
}
267+
268+
return data;
269+
}
270+
271+
// Get data from specific rows with hashtable
272+
public Object[][] getDataHashTableFromSpecificRows(String excelPath, String sheetName, int[] rowNumbers) {
273+
System.out.println("Excel File: " + excelPath);
274+
System.out.println("Sheet Name: " + sheetName);
275+
System.out.println("Reading data from specific rows: " + Arrays.toString(rowNumbers));
276+
277+
Object[][] data = null;
278+
279+
try {
280+
File f = new File(excelPath);
281+
282+
if (!f.exists()) {
283+
System.out.println("File Excel path not found.");
284+
throw new FileNotFoundException("File Excel path not found.");
285+
}
286+
287+
fis = new FileInputStream(excelPath);
288+
workbook = WorkbookFactory.create(fis);
289+
sheet = workbook.getSheet(sheetName);
290+
291+
if (sheet == null) {
292+
System.out.println("Sheet name not found.");
293+
throw new RuntimeException("Sheet name not found.");
294+
}
295+
296+
int columns = getColumns();
297+
// Khởi tạo mảng data với kích thước bằng số lượng dòng được chỉ định
298+
data = new Object[rowNumbers.length][1];
299+
300+
// Đọc dữ liệu từ các dòng được chỉ định
301+
for (int i = 0; i < rowNumbers.length; i++) {
302+
int rowNum = rowNumbers[i];
303+
// Kiểm tra xem dòng có tồn tại không
304+
if (rowNum > sheet.getLastRowNum()) {
305+
System.out.println("WARNING: Row " + rowNum + " does not exist in the sheet.");
306+
data[i][0] = new Hashtable<String, String>();
307+
continue;
308+
}
309+
310+
Hashtable<String, String> table = new Hashtable<>();
311+
for (int j = 0; j < columns; j++) {
312+
// Lấy tên cột từ dòng đầu tiên (header)
313+
String columnName = getCellData(0, j);
314+
// Lấy giá trị từ dòng hiện tại và cột j
315+
String cellValue = getCellData(rowNum, j);
316+
// Thêm vào Hashtable
317+
table.put(columnName, cellValue);
318+
}
319+
data[i][0] = table;
320+
}
321+
322+
// Đóng workbook và FileInputStream
323+
workbook.close();
324+
fis.close();
325+
326+
} catch (Exception e) {
327+
System.out.println("Exception in getDataHashTableFromSpecificRows: " + e.getMessage());
328+
e.printStackTrace();
329+
}
330+
331+
return data;
332+
}
333+
209334
public int getRowContains(String sTestCaseName, int colNum) {
210335
int i;
211336
int rowCount = getRows();

src/test/java/com/anhtester/Bai23_Excel_File/DemoDataProvider.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.anhtester.Bai23_Excel_File;
22

3+
import com.anhtester.dataproviders.DataProviderFactory;
34
import org.testng.annotations.DataProvider;
45
import org.testng.annotations.Test;
56

7+
import java.util.Hashtable;
8+
69
public class DemoDataProvider {
710

811
@DataProvider(name = "loginData")
@@ -18,9 +21,36 @@ public void loginTest(String username, String password) {
1821
// Thực hiện thao tác test login ở đây...
1922
}
2023

21-
// @Test(dataProvider = "loginSuccess", dataProviderClass = DataProviderFactory.class)
22-
// public void testLogin(String username, String password) {
23-
// System.out.println("Login with: " + username + " - " + password);
24-
// }
24+
@Test(dataProvider = "loginSuccess", dataProviderClass = DataProviderFactory.class)
25+
public void testLogin(String username, String password) {
26+
System.out.println("Login with: " + username + " - " + password);
27+
}
28+
29+
@Test(dataProvider = "login_from_excel", dataProviderClass = DataProviderFactory.class)
30+
public void testLoginFromExcel(String username, String password) {
31+
System.out.println("Login with: " + username + " - " + password);
32+
}
33+
34+
@Test(dataProvider = "login_from_excel_hashtable", dataProviderClass = DataProviderFactory.class)
35+
public void testLoginFromExcelHashtable(Hashtable<String, String> data) {
36+
System.out.println("Login with: " + data.get("USERNAME") + " - " + data.get("PASSWORD"));
37+
}
38+
39+
// Sử dụng DataProvider với các dòng cụ thể cố định (1, 3)
40+
@Test(dataProvider = "login_specific_rows", dataProviderClass = DataProviderFactory.class)
41+
public void testLoginWithSpecificRows(String username, String password) {
42+
System.out.println("Username: " + username);
43+
System.out.println("Password: " + password);
44+
}
45+
46+
// Sử dụng DataProvider với các dòng cụ thể dạng Hashtable
47+
@Test(dataProvider = "login_specific_rows_hashtable", dataProviderClass = DataProviderFactory.class)
48+
public void testLoginWithSpecificRowsHashtable(Hashtable<String, String> data) {
49+
String username = data.get("USERNAME");
50+
String password = data.get("PASSWORD");
51+
52+
System.out.println("Username: " + username);
53+
System.out.println("Password: " + password);
54+
}
2555

2656
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.anhtester.Bai23_Excel_File;
22

3+
import com.anhtester.constants.ConfigData;
34
import com.anhtester.helpers.ExcelHelpers;
45
import org.testng.annotations.Test;
56

67
public class DemoExcelFile {
78

89
@Test
910
public void testReadExcelFile() {
10-
String filePath = "src/test/resources/test_data/data.xlsx";
1111
ExcelHelpers excelHelpers = new ExcelHelpers();
12-
excelHelpers.setExcelFile(filePath, "Login");
12+
excelHelpers.setExcelFile(ConfigData.EXCEL_DATA_FILE_PATH, "Login");
1313

1414
System.out.println("USERNAME: " + excelHelpers.getCellData(1, "USERNAME"));
1515
System.out.println("PASSWORD: " + excelHelpers.getCellData(1, "PASSWORD"));
@@ -19,14 +19,13 @@ public void testReadExcelFile() {
1919

2020
}
2121

22-
// @Test
23-
// public void testWriteExcelFile() {
24-
// String filePath = "src/test/resources/test_data/data.xlsx";
25-
// ExcelHelpers excelHelpers = new ExcelHelpers();
26-
// excelHelpers.setExcelFile(filePath, "Login");
27-
//
28-
// excelHelpers.setCellData("Passed", 1, "EXPECTED_RESULT");
29-
// excelHelpers.setCellData("Failed", 2, "EXPECTED_RESULT");
30-
// }
22+
@Test
23+
public void testWriteExcelFile() {
24+
ExcelHelpers excelHelpers = new ExcelHelpers();
25+
excelHelpers.setExcelFile(ConfigData.EXCEL_DATA_FILE_PATH, "Login");
26+
27+
excelHelpers.setCellData("Passed", 1, "EXPECTED_RESULT");
28+
excelHelpers.setCellData("Failed", 2, "EXPECTED_RESULT");
29+
}
3130

3231
}

src/test/java/com/anhtester/dataproviders/DataProviderFactory.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.anhtester.dataproviders;
22

3+
import com.anhtester.constants.ConfigData;
4+
import com.anhtester.helpers.ExcelHelpers;
35
import org.testng.annotations.DataProvider;
46

57
public class DataProviderFactory {
@@ -9,4 +11,48 @@ public Object[][] userDataLoginSuccess() {
911
return new Object[][]{{"admin", "admin123"}, {"test", "test123"}};
1012
}
1113

14+
@DataProvider(name = "login_from_excel")
15+
public Object[][] login_from_excel() {
16+
ExcelHelpers excelHelpers = new ExcelHelpers();
17+
return excelHelpers.getExcelData(
18+
ConfigData.EXCEL_DATA_FILE_PATH,
19+
"Login"
20+
);
21+
}
22+
23+
@DataProvider(name = "login_from_excel_hashtable")
24+
public Object[][] login_from_excel_hashtable() {
25+
ExcelHelpers excelHelpers = new ExcelHelpers();
26+
return excelHelpers.getDataHashTable(
27+
ConfigData.EXCEL_DATA_FILE_PATH,
28+
"Login",
29+
2,
30+
3
31+
);
32+
}
33+
34+
@DataProvider(name = "login_specific_rows")
35+
public Object[][] login_specific_rows() {
36+
ExcelHelpers excelHelpers = new ExcelHelpers();
37+
// Đọc dữ liệu từ các dòng 1, 3
38+
int[] specificRows = new int[]{1, 3};
39+
return excelHelpers.getDataFromSpecificRows(
40+
ConfigData.EXCEL_DATA_FILE_PATH,
41+
"Login",
42+
specificRows
43+
);
44+
}
45+
46+
@DataProvider(name = "login_specific_rows_hashtable")
47+
public Object[][] login_specific_rows_hashtable() {
48+
ExcelHelpers excelHelpers = new ExcelHelpers();
49+
// Đọc dữ liệu từ các dòng 1, 3
50+
int[] specificRows = new int[]{1, 3};
51+
return excelHelpers.getDataHashTableFromSpecificRows(
52+
ConfigData.EXCEL_DATA_FILE_PATH,
53+
"Login",
54+
specificRows
55+
);
56+
}
57+
1258
}

src/test/resources/configs/config.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ STEP_ACTION_TIMEOUT = 1
99
#Set data/config/report path
1010
JSON_CONFIG_FILE_PATH = src/test/resources/configs/device.json
1111
JSON_DATA_FILE_PATH = src/test/resources/test_data/data.json
12-
EXCEL_DATA_FILE_PATH =
12+
EXCEL_DATA_FILE_PATH = src/test/resources/test_data/data.xlsx
1313
TEST_DATA_FOLDER_PATH = src/test/resources/test_data
1414
LOCATE = en
1515
SCREENSHOT_FAIL = true
2.31 KB
Binary file not shown.

0 commit comments

Comments
 (0)