![](/img/trans.png)
[英]How can I read different datatypes from excel using @dataprovider in testNG
[英]How to write TestNG DataProvider annotation for Excel sheet with different column numbers for different rows
我有一個 Excel 表,其中的數據如下
LoginPageValidation|
LoginPage_login | username1 | password1
LoginPage_login | username2 | password2
LoginPage_login | username3 | password3
我將“數組數組”返回給@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;
}
}
}
/測試類/
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); } }
重構你的代碼,試試這個,它必須工作..
/**
* @author revanthreddya
*
*/
package com.playground.excel;
public class ExcelUtils
{
private Workbook wb;
private Sheet ws;
public ExcelUtils(String file, String sheet) {
try {
if (file.indexOf("xlsx") < 0) {
wb = new HSSFWorkbook(new FileInputStream(new File(file)));
ws = wb.getSheet(sheet);
} else {
wb = new XSSFWorkbook(new FileInputStream(new File(file)));
ws = (XSSFSheet) wb.getSheet(sheet);
}
} catch (IOException io) {
System.err.println("Invalid file '" + file
+ "' or incorrect sheet '" + sheet
+ "', enter a valid one");
}
}
public int rowCount(){
return ws.getLastRowNum();
}
public String getCell(int rowIndex, int columnIndex) {
Cell cell = null;
try {
cell = ws.getRow(rowIndex).getCell(columnIndex);
} catch (Exception e) {
System.err.println("The cell with row '" + rowIndex + "' and column '"
+ columnIndex + "' doesn't exist in the sheet");
}
return new DataFormatter().formatCellValue(cell);
}
}
public class TestCase {
@Test(dataProvider="access")
public void AADLoginLogoutTest(String test, String username, String password) throws IOException
{
System.out.println(" test :"+test+" user "+username+" password:"+ password);
}
@DataProvider(name = "access")
public Object[][] access() throws Exception {
ExcelUtils userD= new ExcelUtils("input.xlsx", "Actions");
ArrayList<Object> paraList = new ArrayList<Object>();
int i = 1;
int totalRows = userD.rowCount();
System.out.println("TotalRows: "+totalRows);
while (i < totalRows) {
Object[] args = new Object[3];
args[0] = userD.getCell(i, 0);
args[1] = userD.getCell(i, 1);
args[2] = userD.getCell(i, 2);
paraList.add(args);
i++;
}
Object[][] argsData = new Object[paraList.size()][];
for (i = 0; i < paraList.size(); i++)
argsData[i] = (Object[]) paraList.get(i);
return argsData;
}
}
如果您的 excel 表包含不同的列號,我們可以在讀取 excel 表時進行處理,而不是在數據提供程序中處理它們。 在您的代碼中,替換以下部分
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]);
}
}
}
和
for(int i=0;i<totalRows;i++)
{
int ColnumForRow=ExcelWSheet.getRow(i).getPhysicalNumberOfCells();
a[i]=new String [ColnumForRow];
for (int j=0;j<ColnumForRow;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]);
}
}
}
如果我們使用getPhysicalNumberOfCells
方法而不是getLastCellNum
它將只返回包含數據的列數。 因此,無需檢查該列是否為空。
希望這可以幫助。
我想你在 @DataProvider 方法中調用 getTableArray() 並且你在 getTableArray() 方法中得到 NullPointerException 。 您可以在 getTableArray() 中執行此操作,而不是在 @DataProvider 中處理該異常,因為這是讀取 excel 工作表的所有操作的地方。
要了解在 getTableArray() 中處理異常的簡單方法,您可以按照我的博客文章中給出的腳本進行操作 - http://selenium-coding.blogspot.in/2016/05/generalized-apache-poi-script- for.html它顯示了閱讀和編寫 excelsheet 的更簡單方法。 它基本上將讀取和寫入 excel 表的代碼放在一個類中,以便其他測試也可以使用它,並且它只在這些特定方法中完成異常處理。 作為一種標准做法,我們應該讓測試遠離底層的復雜性(例如捕獲異常等),以便讓沒有編寫它們的人能夠簡單地理解它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.