22
33import com .anhtester .constants .ConfigData ;
44import com .anhtester .helpers .ExcelHelpers ;
5+ import org .testng .ITestContext ;
56import org .testng .annotations .DataProvider ;
67
8+ import java .util .Arrays ;
9+
710public 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}
0 commit comments