【发布时间】:2016-09-29 06:56:54
【问题描述】:
我有一张excelsheet,数据如下
登录页面验证|
登录页面_登录 |用户名1 |密码1
登录页面_登录 |用户名2 |密码2
登录页面_登录 |用户名3 |密码3
我将“数组数组”返回给@Dataprovider 表单类读取 ExcelSheet(Excelutility.java)
有没有办法编写 @DataProvider 来处理 nullpointerException,同时从具有单列数据的行中读取数据。
public static Object[][] getTableArray() throws Exception
{
String[][] tabArray = null;
try {
FileInputStream ExcelFile = new FileInputStream(FilePath);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startRow = 0;
int totalRows = ExcelWSheet.getLastRowNum()-ExcelWSheet.getFirstRowNum()+1;
System.out.print("\nTOTAL ROWS "+totalRows+"\n");
String a[][]=new String[totalRows][];
for(int i=0;i<totalRows;i++)
{
int ColnumForRow=ExcelWSheet.getRow(i).getLastCellNum();
a[i]=new String [ColnumForRow];
for (int j=0;j<ExcelWSheet.getRow(i).getLastCellNum();j++)
{
if(getCellData(i,j).isEmpty())
{System.out.println("\nEMPTY \n");}
else
{ a[i][j]=getCellData(i,j);
System.out.println("\nTABLE ARRAY : "+ a[i][j]);
}}
}}
return(tabArray);
}
public static String getCellData(int RowNum, int ColNum) throws Exception
{try{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
int dataType = Cell.getCellType();
String CellData = Cell.getStringCellValue();
return CellData;
}
}
}
/testClass/public class test1
{
@Test(dataProvider="access")
public void AADLoginLogoutTest(String test,String username,String pwd) throws IOException
{
System.out.println("CLAASS name AADLOGINLOGOUT"+this.getClass().getSimpleName());
}
@DataProvider
public Object[][] access() throws Exception
{
Object[][] testObjArray = ExcelUtils.getTableArray();
return (testObjArray);
}
}
【问题讨论】:
标签: java arrays selenium selenium-webdriver testng-dataprovider