简体   繁体   English

如何使用testNG dataprovider迭代Excel工作表以获取多组数据?

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

I have been working with TestNg for a while but I have a new req that I cant seem to figure out. 我已经与TestNg合作了一段时间,但我有一个新的要求似乎无法解决。

I have an excel file(sample.xls), sheet name=Dataset that has the following: 我有一个Excel文件(sample.xls),工作表名称=数据集,具有以下内容:

testdata1 ex1 ex2 ex3 ex4 testdata1 ex1 ex2 ex3 ex4

      1   2    3 4 testdata1

testdata2 ex1 ex2 ex3 ex4 testdata2 ex1 ex2 ex3 ex4

      5   6    7  8  testdata2

my script is similar to this: 我的脚本与此类似:

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

My question is: How should I modify this script so that my Script goes through both sets of data from the excel sheet. 我的问题是:我应该如何修改此脚本,以使我的脚本遍历excel工作表中的两组数据。 Right now the script goes through the first set of data (cells within testdata1). 现在,脚本将遍历第一组数据(testdata1中的单元格)。 I want to also go through testdata2. 我也想通过testdata2。 The result I am looking for is: 我正在寻找的结果是:

1 2 3 4 1 2 3 4

5 6 7 8 5 6 7 8

Thanks 谢谢

Nothing to modify in the Script. 无需在脚本中进行任何修改。 Change your excel Test data as shown in the image.. 如图所示,更改您的excel测试数据。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在带有 Java 和 TestNG 的 Selenium WebDriver 中使用 DataProvider 读取 Excel 表中的多组值 - How to read multiple sets of value in the excel sheet using DataProvider in Selenium WebDriver with Java and TestNG 使用 Apache POI 将多种数据类型数据从 excel 发送到 Testng Dataprovider 的最佳方法是什么 - What is the best way to send multiple data type data from excel to Testng Dataprovider using Apache POI TestNG中的DataProvider使用Java WebDriver从Excel传递数据 - DataProvider in TestNG to pass data from Excel using Java WebDriver 如何使用Apache POI和TestNG dataProvider编写Excel - How to write excel using Apache POI with TestNG dataProvider 使用TestNG DataProvider时,是否可以从SAME Excel工作表中读取和写入参数? - When using TestNG DataProvider is there a way to read and write parameters from the SAME Excel sheet? 如何使用Selenium WebDriver TestNG数据提供程序方法从Excel工作表中读取特定值? - How to read a specific value from Excel sheet using Selenium WebDriver TestNG Data Provider Method? 如何使用PHPExcel将多个HTML表单数据添加到Excel工作表中 - How to add multiple HTML form data into Excel sheet using PHPExcel 如何格式化Excel工作表,以允许导入大量数据集 - How to format Excel sheet, to allow import of numerous data sets 如何使用VBA将多个Excel工作表合并为一个Excel工作表 - How to combine multiple Excel sheet into one Excel sheet using VBA 使用excel VBA比较不同工作表中的两个数据集并打印其他工作表中的差异? - Compare two data sets in different sheet and print difference in other sheet using excel VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM