簡體   English   中英

如何使用testNG dataprovider迭代Excel工作表以獲取多組數據?

[英]How to Iteratethrough excel sheet using testNG dataprovider for multiple sets of data?

我已經與TestNg合作了一段時間,但我有一個新的要求似乎無法解決。

我有一個Excel文件(sample.xls),工作表名稱=數據集,具有以下內容:

testdata1 ex1 ex2 ex3 ex4

      1   2    3 4 testdata1

testdata2 ex1 ex2 ex3 ex4

      5   6    7  8  testdata2

我的腳本與此類似:

import com.thoughtworks.selenium.*;

import org.junit.AfterClass;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;

import java.io.File;
import jxl.*; 


public class DataProviderExample extends SeleneseTestCase{


    @DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception{
        Object[][] retObjArr=getTableArray("test\\Resources\\Data\\sample.xls",
                "DataPool", "testdata1");
        return(retObjArr);
    }

    @Test (dataProvider = "DP1")
    public void testDataProviderExample(String ex1, 
            String ex2, String ex3, String ex4) throws Exception {    
       ///Extensive coding here but for simplicity I will just print 
            System.out.println(ex1);
            System.out.println(ex2);
            System.out.println(ex3);
            System.out.println(ex\4);
    }

    @AfterClass
    public void tearDown(){
        selenium.close();
        selenium.stop();
    } 

    public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
        String[][] tabArray=null;

            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName); 
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();

            Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                    "startCol="+startCol+", endCol="+endCol);
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }


        return(tabArray);
    }


}//end of class

我的問題是:我應該如何修改此腳本,以使我的腳本遍歷excel工作表中的兩組數據。 現在,腳本將遍歷第一組數據(testdata1中的單元格)。 我也想通過testdata2。 我正在尋找的結果是:

1 2 3 4

5 6 7 8

謝謝

無需在腳本中進行任何修改。 如圖所示,更改您的excel測試數據。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM