2016-02-26 68 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

相關問題