2016-02-26 67 views
-1

Excel Reader:无法使用数据提供者在测试方法中提取参数

package com.Excel.Read;

public class ReadFile { static Sheet excelWSheet;

static Workbook excelWBook; 

static Row row; 

public static String[][] getExcelData(String fileName, String sheetName, int startRow,int targetRow, int startCol, int targetCol) throws EncryptedDocumentException, InvalidFormatException, IOException { 
     String[][] arrayExcelData = null; 
     try { 
      FileInputStream fs = new FileInputStream(fileName); 
      excelWBook = WorkbookFactory.create(fs); 
      int rowCount = excelWBook.getSheet(sheetName).getLastRowNum(); 
      int colCount; 
      System.out.println(rowCount); 
      arrayExcelData = new String[targetRow-startRow+1][targetCol-startCol+1]; 

      for(int i = startRow ; i < targetRow; i++) { 
       colCount = excelWSheet.getRow(i).getLastCellNum(); 
       System.out.println(colCount); 
       for (int j = startCol; j <= targetCol; j++) { 
        arrayExcelData[i-startRow][j-startCol] = row.getCell(j).getStringCellValue(); 
       } 
      } 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     return arrayExcelData; 
    } 

} 

识别TestClass:

包com.Excel.Read;

import org.testng.annotations.DataProvider; import org.testng.annotations.Test;

公共类TestDataProvider {

@Test(dataProvider="getlogincred") 
public void setcred(String sUserName, String sPassword, String sexpectedMsg) 
{ 
    System.out.println("UserName : " + sUserName); 
    System.out.println("Password : " + sPassword); 
    System.out.println("Msg : " + sexpectedMsg); 
    System.out.println(); 
} 

@DataProvider(name="getlogincred") 
public String[][] getlogincred() throws Exception 
{ 
    String[][] testObjArray = ReadFile.getExcelData("C:\\Users\\u6035997\\Desktop\\Book1.xlsx", "Sheet1",2,5,2,4); 

    return (testObjArray); 
} 

}`

回答

0

数据提供回报的对象阵列。所以你必须通过加载testObjArray来返回对象数组。下面是简单的例子可以帮助你

@Test(dataProvider="my data") 
public void GoogelSeacrhTest(Hashtable<String, String> h) throws InterruptedException{ 

    //my code here 

} 

@DataProvider(name="my data") 
public Object[][] testdata(){ 

    Hashtable<String, String> h=new Hashtable<>(); 
    h.put("search", "murali selenium"); 

    Object[][] ob={{h}}; 

    return ob; 

} 

所以,你需要改变

public static String[][] getExcelData(String fileName, String sheetName, int startRow,int targetRow, int startCol, int targetCol) throws EncryptedDocumentException, InvalidFormatException, IOException { } 

返回对象的对象[] [],而不是一个String [] [],然后,简直比物体通过返回在您的数据提供所需的参数

@DataProvider(name="getlogincred") 
public Object[][] getlogincred() throws Exception 
{ 
    return ReadFile.getExcelData("C:\\Users\\u6035997\\Desktop\\Book1.xlsx", "Sheet1",2,5,2,4); 
} 

请参考文档here以获取更多信息。

谢谢, Murali

相关问题