1
我正在使用混合框架,用於編寫Excel表格我使用的是Apache-poi庫,由數據提供者提供。如何使用Apache POI和TestNG dataProvider編寫excel
我希望我的代碼能夠以這種方式使用它,我可以讀取和寫入我的Excel表格,其中寫入了測試用例,並根據這些情況設置了他們的狀態。
目前,當我執行我的代碼時,它跳過了登錄方法。其實我是初學者,並嘗試用它來閱讀和寫作excel,任何人都可以幫我解決這個問題嗎?
public class HybridExecuteTest {
private static final String BROWSER_PATH = "D:\\abc\\setup\\FFinstalled\\firefox.exe";
private static XSSFWorkbook ExcelWBook;
private static XSSFSheet ExcelWSheet;
private static XSSFCell Cell;
private static XSSFRow Row;
WebDriver webdriver = null;
@Test(dataProvider = "hybridData")
public void testLogin(String testcaseName, String keyword,
String objectName, String objectType, String value)
throws Exception {
// TODO Auto-generated method stub
if (testcaseName != null && testcaseName.length() != 0) {
// webdriver=new FirefoxDriver();
File file = new File(BROWSER_PATH);
FirefoxBinary fb = new FirefoxBinary(file);
webdriver = new FirefoxDriver(fb, new FirefoxProfile());
}
ReadObject object = new ReadObject();
Properties allObjects = object.getObjectRepository();
UIOperation operation = new UIOperation(webdriver);
// Call perform function to perform operation on UI
operation.perform(allObjects, keyword, objectName, objectType, value);
}
@DataProvider(name = "hybridData")
// This method is to set the File path and to open the Excel file, Pass Excel Path and Sheetname as Arguments to this method
public Object[][] setExcelFile(String filePath, String fileName, String sheetName) throws Exception {
Object object[][] = null;
try {
File file = new File(filePath + "\\" + fileName);
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(file);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(sheetName);
} catch (Exception e) {throw (e);}
return object;
}
// This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num
public String getCellData(int RowNum, int ColNum) throws Exception {
try {
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
String CellData = Cell.getStringCellValue();
return CellData;
} catch (Exception e) {
return "";
}
}
// This method is to write in the Excel cell, Row num and Col num are the parameters
public String setCellData(String Result, int RowNum, int ColNum,String filePath, String fileName) throws Exception {
try {
Row = ExcelWSheet.getRow(RowNum);
Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
// Constant variables Test Data path and Test Data file name
File file = new File(filePath + "\\" + fileName);
// Open the Excel file
FileOutputStream ExcelFile = new FileOutputStream(file);
ExcelWBook.write(ExcelFile);
} catch (Exception e) {
throw (e);
}
return null;
}
}
控制檯:
SKIPPED: testLogin
org.testng.TestNGException:
Some DataProvider public java.lang.Object[][] testCases.HybridExecuteTest.setExcelFile(java.lang.String,java.lang.String,java.lang.String) throws java.lang.Exception parameters unresolved: at 1 type class java.lang.String
at 2 type class java.lang.String
at 3 type class java.lang.String
注:我去通過Apache POI的教程,通常我知道如何寫,但在框架我卡住了。請幫助這些。
這可能是有幫助的:http://learn-automation.com/readwrite-excel-files-in-selenium/ – mfulton26
@ mfulton26你可以請給解決方案witjh上面的代碼,我用數據提供商。因爲我已經注意到我已經完成了所有的教程。 – SelenyanC2
你也可以使用[TestNG的數據提供者擴展](https://github.com/cbeust/testng/wiki/3rd-party-extensions#data-provider-extension) – user861594