Skip to content

Commit 3bb6a76

Browse files
committed
Bài 29 - DataProvider
1 parent 2e86692 commit 3bb6a76

File tree

8 files changed

+188
-6
lines changed

8 files changed

+188
-6
lines changed

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import java.io.File;
55
import java.io.FileInputStream;
66
import java.io.FileOutputStream;
7+
import java.io.IOException;
78
import java.util.HashMap;
9+
import java.util.Hashtable;
810
import java.util.Map;
911

1012
import org.apache.poi.ss.usermodel.*;
1113
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
14+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1215

1316
public class ExcelHelper {
1417

@@ -144,4 +147,123 @@ public void setCellData(String text, String columnName, int rowIndex) {
144147
}
145148
}
146149

150+
public Object[][] getExcelData(String filePath, String sheetName) {
151+
Object[][] data = null;
152+
Workbook workbook = null;
153+
try {
154+
// load the file
155+
FileInputStream fis = new FileInputStream(filePath);
156+
157+
// load the workbook
158+
workbook = new XSSFWorkbook(fis);
159+
160+
// load the sheet
161+
Sheet sh = workbook.getSheet(sheetName);
162+
163+
// load the row
164+
Row row = sh.getRow(0);
165+
166+
//
167+
int noOfRows = sh.getPhysicalNumberOfRows();
168+
int noOfCols = row.getLastCellNum();
169+
170+
System.out.println(noOfRows + " - " + noOfCols);
171+
172+
Cell cell;
173+
data = new Object[noOfRows - 1][noOfCols];
174+
175+
//
176+
for (int i = 1; i < noOfRows; i++) {
177+
for (int j = 0; j < noOfCols; j++) {
178+
row = sh.getRow(i);
179+
cell = row.getCell(j);
180+
181+
switch (cell.getCellType()) {
182+
case STRING:
183+
data[i - 1][j] = cell.getStringCellValue();
184+
break;
185+
case NUMERIC:
186+
data[i - 1][j] = String.valueOf(cell.getNumericCellValue());
187+
break;
188+
case BLANK:
189+
data[i - 1][j] = cell.getStringCellValue();
190+
break;
191+
default:
192+
data[i - 1][j] = cell.getStringCellValue();
193+
break;
194+
}
195+
}
196+
}
197+
} catch (Exception e) {
198+
System.out.println("The exception is:" + e.getMessage());
199+
throw new RuntimeException(e);
200+
}
201+
return data;
202+
}
203+
204+
//Hàm này dùng cho trường hợp nhiều Field trong File Excel
205+
public int getColumns() {
206+
try {
207+
row = sh.getRow(0);
208+
return row.getLastCellNum();
209+
} catch (Exception e) {
210+
System.out.println(e.getMessage());
211+
throw (e);
212+
}
213+
}
214+
215+
//Get last row number (lấy vị trí dòng cuối cùng tính từ 0)
216+
public int getLastRowNum() {
217+
return sh.getLastRowNum();
218+
}
219+
220+
//Lấy số dòng có data đang sử dụng
221+
public int getPhysicalNumberOfRows() {
222+
return sh.getPhysicalNumberOfRows();
223+
}
224+
225+
public Object[][] getDataHashTable(String excelPath, String sheetName, int startRow, int endRow) {
226+
System.out.println("Excel Path: " + excelPath);
227+
Object[][] data = null;
228+
229+
try {
230+
File f = new File(excelPath);
231+
if (!f.exists()) {
232+
try {
233+
System.out.println("File Excel path not found.");
234+
throw new IOException("File Excel path not found.");
235+
} catch (Exception e) {
236+
e.printStackTrace();
237+
}
238+
}
239+
240+
fis = new FileInputStream(excelPath);
241+
242+
wb = new XSSFWorkbook(fis);
243+
244+
sh = wb.getSheet(sheetName);
245+
246+
int rows = getLastRowNum();
247+
int columns = getColumns();
248+
249+
System.out.println("Row: " + rows + " - Column: " + columns);
250+
System.out.println("StartRow: " + startRow + " - EndRow: " + endRow);
251+
252+
data = new Object[(endRow - startRow) + 1][1];
253+
Hashtable<String, String> table = null;
254+
for (int rowNums = startRow; rowNums <= endRow; rowNums++) {
255+
table = new Hashtable<>();
256+
for (int colNum = 0; colNum < columns; colNum++) {
257+
table.put(getCellData(colNum, 0), getCellData(colNum, rowNums));
258+
}
259+
data[rowNums - startRow][0] = table;
260+
}
261+
262+
} catch (IOException e) {
263+
e.printStackTrace();
264+
}
265+
266+
return data;
267+
}
268+
147269
}

src/test/java/com/anhtester/Bai26_CustomDriverParallelExecution/testcases/LoginTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.anhtester.Bai26_CustomDriverParallelExecution.pages.LoginPage;
55
import com.anhtester.common.BaseTest;
66
import com.anhtester.constants.ConfigData;
7+
import com.anhtester.dataprovider.DataProviderFactory;
78
import com.anhtester.keywords.WebUI;
89
import org.testng.annotations.Parameters;
910
import org.testng.annotations.Test;
@@ -13,19 +14,19 @@ public class LoginTest extends BaseTest {
1314
LoginPage loginPage;
1415
DashboardPage dashboardPage;
1516

16-
@Test
17-
public void testLoginSuccess() {
17+
@Test(dataProvider = "DataLoginSuccess", dataProviderClass = DataProviderFactory.class)
18+
public void testLoginSuccess(String email, String password) {
1819
loginPage = new LoginPage();
19-
dashboardPage = loginPage.loginCRM(ConfigData.EMAIL, ConfigData.PASSWORD);
20+
dashboardPage = loginPage.loginCRM(email, password);
2021
loginPage.verifyLoginSuccess();
2122
WebUI.captureScreenImage("testLoginSuccess");
2223
dashboardPage.logOut();
2324
}
2425

25-
@Test
26-
public void testLoginWithEmailInvalid() {
26+
@Test(dataProvider = "DataLoginFail", dataProviderClass = DataProviderFactory.class)
27+
public void testLoginWithEmailInvalid(String email, String password) {
2728
loginPage = new LoginPage();
28-
dashboardPage = loginPage.loginCRM("admin123@example.com", "123456");
29+
dashboardPage = loginPage.loginCRM(email, password);
2930
loginPage.verifyLoginFail();
3031
WebUI.captureScreenImage("testLoginWithEmailInvalid");
3132
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.anhtester.Bai29_DataProvider;
2+
3+
import com.anhtester.dataprovider.DataProviderFactory;
4+
import com.anhtester.keywords.WebUI;
5+
import org.testng.annotations.DataProvider;
6+
import org.testng.annotations.Test;
7+
8+
import java.util.Hashtable;
9+
10+
public class DemoDataProvider {
11+
12+
@Test(dataProvider = "DataLoginSuccess", dataProviderClass = DataProviderFactory.class)
13+
public void testLoginSuccess(String email, String password) {
14+
System.out.println("EMAIL: " + email);
15+
System.out.println("PASSWORD: " + password);
16+
}
17+
18+
@Test(dataProvider = "data_provider_login_excel_hashtable", dataProviderClass = DataProviderFactory.class)
19+
public void testLoginDataFromExcelMultipleRow(Hashtable<String, String> data) {
20+
System.out.println("EMAIL: " + data.get("EMAIL"));
21+
System.out.println("PASSWORD: " + data.get("PASSWORD"));
22+
}
23+
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.anhtester.dataprovider;
2+
3+
import com.anhtester.helpers.ExcelHelper;
4+
import org.testng.annotations.DataProvider;
5+
6+
public class DataProviderFactory {
7+
8+
@DataProvider(name = "DataLoginSuccess", parallel = true)
9+
public Object[][] dataLoginSuccess() {
10+
// return new Object[][]{
11+
// {"admin@example.com", "123456"},
12+
// {"admin@example.com", "123456"}
13+
// };
14+
ExcelHelper excelHelper = new ExcelHelper();
15+
Object[][] data = excelHelper.getExcelData("src/test/resources/testdata/LoginData.xlsx", "Sheet1");
16+
return data;
17+
}
18+
19+
@DataProvider(name = "DataLoginFail", parallel = true)
20+
public Object[][] dataLoginFail() {
21+
return new Object[][]{
22+
{"admin123@example.com", "123456"},
23+
{"customer123@example.com", "123456"}
24+
};
25+
}
26+
27+
@DataProvider(name = "data_provider_login_excel_hashtable")
28+
public Object[][] dataLoginFromExcelHashtable() {
29+
ExcelHelper excelHelper = new ExcelHelper();
30+
Object[][] data = excelHelper.getDataHashTable( "src/test/resources/testdata/LoginData.xlsx", "Sheet1", 4, 3);
31+
System.out.println("Login Data from Excel: " + data);
32+
return data;
33+
}
34+
35+
}
Loading
Loading
9.46 KB
Binary file not shown.
4 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)