Skip to content

Commit 3aac9ef

Browse files
committed
Bai 23 - Excel file - DataProvider
1 parent 8fd4554 commit 3aac9ef

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,23 @@ public void testLoginWithSpecificRowsHashtable(Hashtable<String, String> data) {
5353
System.out.println("Password: " + password);
5454
}
5555

56+
// Sử dụng DataProvider động với tham số từ testng.xml
57+
@Test(dataProvider = "dynamic_rows", dataProviderClass = DataProviderFactory.class)
58+
public void testLoginWithDynamicRows(String username, String password) {
59+
System.out.println("Username: " + username);
60+
System.out.println("Password: " + password);
61+
62+
}
63+
64+
// Sử dụng DataProvider động dạng Hashtable với tham số từ testng.xml
65+
@Test(dataProvider = "dynamic_rows_hashtable", dataProviderClass = DataProviderFactory.class)
66+
public void testLoginWithDynamicRowsHashtable(Hashtable<String, String> data) {
67+
String username = data.get("USERNAME");
68+
String password = data.get("PASSWORD");
69+
70+
System.out.println("Username: " + username);
71+
System.out.println("Password: " + password);
72+
73+
}
74+
5675
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import com.anhtester.constants.ConfigData;
44
import com.anhtester.helpers.ExcelHelpers;
5+
import org.testng.ITestContext;
56
import org.testng.annotations.DataProvider;
67

8+
import java.util.Arrays;
9+
710
public class DataProviderFactory {
811

912
@DataProvider(name = "loginSuccess")
@@ -55,4 +58,65 @@ public Object[][] login_specific_rows_hashtable() {
5558
);
5659
}
5760

61+
62+
// Truyền tham số từ suite XML vào DataProvider
63+
64+
/**
65+
* DataProvider động cho phép truyền tham số là các dòng cần đọc
66+
*
67+
* @param rowIndices Chuỗi chứa các chỉ số dòng, phân cách bởi dấu phẩy, ví dụ: "1,3,5"
68+
* @return Dữ liệu từ các dòng được chỉ định
69+
*/
70+
@DataProvider(name = "dynamic_rows")
71+
public Object[][] dynamic_rows(ITestContext context) {
72+
// Lấy tham số từ test context hoặc suite XML
73+
String rowIndicesStr = context.getCurrentXmlTest().getParameter("rowIndices");
74+
if (rowIndicesStr == null || rowIndicesStr.isEmpty()) {
75+
// Mặc định đọc các dòng 1, 2, 3 nếu không có tham số nào được truyền
76+
rowIndicesStr = "1,2,3";
77+
}
78+
79+
// Chuyển đổi chuỗi thành mảng các số nguyên
80+
int[] rowIndices = Arrays.stream(rowIndicesStr.split(","))
81+
.map(String::trim)
82+
.mapToInt(Integer::parseInt)
83+
.toArray();
84+
85+
ExcelHelpers excelHelpers = new ExcelHelpers();
86+
return excelHelpers.getDataFromSpecificRows(
87+
ConfigData.EXCEL_DATA_FILE_PATH,
88+
"Login",
89+
rowIndices
90+
);
91+
}
92+
93+
/**
94+
* DataProvider động trả về dữ liệu dạng Hashtable
95+
*
96+
* @param rowIndices Chuỗi chứa các chỉ số dòng, phân cách bởi dấu phẩy, ví dụ: "1,3,5"
97+
* @return Dữ liệu dạng Hashtable từ các dòng được chỉ định
98+
*/
99+
@DataProvider(name = "dynamic_rows_hashtable")
100+
public Object[][] dynamic_rows_hashtable(ITestContext context) {
101+
// Lấy tham số từ test context hoặc suite XML
102+
String rowIndicesStr = context.getCurrentXmlTest().getParameter("rowIndices");
103+
if (rowIndicesStr == null || rowIndicesStr.isEmpty()) {
104+
// Mặc định đọc các dòng 1, 2, 3 nếu không có tham số nào được truyền
105+
rowIndicesStr = "1,2,3";
106+
}
107+
108+
// Chuyển đổi chuỗi thành mảng các số nguyên
109+
int[] rowIndices = Arrays.stream(rowIndicesStr.split(","))
110+
.map(String::trim)
111+
.mapToInt(Integer::parseInt)
112+
.toArray();
113+
114+
ExcelHelpers excelHelpers = new ExcelHelpers();
115+
return excelHelpers.getDataHashTableFromSpecificRows(
116+
ConfigData.EXCEL_DATA_FILE_PATH,
117+
"Login",
118+
rowIndices
119+
);
120+
}
121+
58122
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
3+
<suite name="Test Suite">
4+
<test name="Login Test with Specific Rows">
5+
<!-- Định nghĩa các dòng bạn muốn đọc dữ liệu từ Excel (1, 3, 5) -->
6+
<parameter name="rowIndices" value="1,3"/>
7+
<classes>
8+
<class name="com.anhtester.Bai23_Excel_File.DemoDataProvider">
9+
<methods>
10+
<include name="testLoginWithDynamicRows"/>
11+
<include name="testLoginWithDynamicRowsHashtable"/>
12+
</methods>
13+
</class>
14+
</classes>
15+
</test>
16+
17+
<!-- Bạn có thể định nghĩa test khác với các dòng khác -->
18+
<test name="Login Test with Different Rows">
19+
<parameter name="rowIndices" value="2,3"/>
20+
<classes>
21+
<class name="com.anhtester.Bai23_Excel_File.DemoDataProvider">
22+
<methods>
23+
<include name="testLoginWithDynamicRows"/>
24+
<include name="testLoginWithDynamicRowsHashtable"/>
25+
</methods>
26+
</class>
27+
</classes>
28+
</test>
29+
</suite>

0 commit comments

Comments
 (0)