简体   繁体   English

如何在testNG中使用@dataprovider从excel读取不同的数据类型

[英]How can I read different datatypes from excel using @dataprovider in testNG

I am trying to get input from excel for my testcase . 我试图从excel的测试用例中获取输入。 My input contains both string and numeric values. 我的输入包含字符串和数字值。 I know how to retrieve value for single datatype using the below code 我知道如何使用以下代码检索单个数据类型的值

public Object[][]  readnumericvalue() throws IOException {
    File src = new File("filepath");    
    FileInputStream fis = new FileInputStream(src);
    XSSFWorkbook    wb = new XSSFWorkbook(fis);
    XSSFSheet sheet1 = wb.getSheetAt(1);
    XSSFRow row = sheet1.getRow(0);
    int rowcount = sheet1.getLastRowNum();
    int columnCount = row.getLastCellNum();
    Object data1[][]=new Double[rowcount+1][columnCount];
    columnCount = columnCount-1;

    for(int i=0;i<=rowcount;i++) {
        for(int j=0;j<=columnCount;j++) {
            data1[i[j]= sheet1.getRow(i).getCell(j).getNumericCellValue();
        }
    }
    return data1;
}

I tried to read both the datatypes in single dataprovider but I don't know how to initialize 2D-array for multiple datatypes 我试图在单个dataprovider中读取两个数据类型,但是我不知道如何为多个数据类型初始化2D数组

File src = new File("");

FileInputStream fis = new FileInputStream(src);
XSSFWorkbook    wb = new XSSFWorkbook(fis);
XSSFSheet sheet1 = wb.getSheetAt(0);
XSSFRow row = sheet1.getRow(0);
int rowcount = sheet1.getLastRowNum();
int columnCount = row.getLastCellNum();     
Object data1[][]=new String[rowcount+1][columnCount];
columnCount = columnCount-1
for(int i=0;i<=rowcount;i++) {
    for(int j=0;j<=columnCount;j++) {
        Cell cell = row.getCell(j);
        switch (cell.getCellTypeEnum()) {
            case STRING:
                data1[i][j]=sheet1.getRow(i).getCell(j).getStringCellValue();
                break;

            case NUMERIC:               
                data1[i][j]=sheet1.getRow(i).getCell(j).getNumericCellValue();
                break;                              
        }
    }   
}
return data1;

If you would like to accommodate both the data types in the same data provider, then you can basically define the array as an Object array. 如果要在同一个数据提供程序中容纳这两种数据类型,则可以将数组基本上定义为Object数组。

Here's a sample. 这是一个样本。

Lets say the excel workbook looks like below 可以说excel工作簿如下所示

| Name | Age |
|------|-----|
| Jack | 24  |
| Jill | 23  |
| Bob  | 30  |

Here's a sample code, that reads this data 这是读取此数据的示例代码

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

public class SampleTestClass {

    @Test(dataProvider = "dp")
    public void testMethod(String name, int age) {
        System.err.println("Name :" + name + ", Age :" + age);
    }

    @DataProvider(name = "dp")
    public Object[][] readnumericvalue() throws IOException {
        File src = new File("src/test/resources/47036541.xlsx");
        FileInputStream fis = new FileInputStream(src);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet1 = wb.getSheetAt(0);

        int rowcount = sheet1.getPhysicalNumberOfRows();
        int columnCount = sheet1.getRow(0).getLastCellNum();
        Object objects[][] = new Object[rowcount-1][columnCount];
        int rowCounter = 0;

        Iterator<Row> rowIterator = sheet1.iterator();
        boolean firstRow = true;
        while (rowIterator.hasNext()) {
            Row currentRow = rowIterator.next();
            if (firstRow) {
                firstRow = false;
                continue;
            }
            Iterator<Cell> cellIterator = currentRow.iterator();
            int colCounter = 0;
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        objects[rowCounter][colCounter] = cell.getStringCellValue();
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        objects[rowCounter][colCounter] =  new Double(cell.getNumericCellValue()).intValue();
                        break;
                }
                colCounter++;
            }
            rowCounter++;
        }
        return objects;
    }
}

Here's the output 这是输出

Name :Jack, Age :24
Name :Jill, Age :23
Name :Bob, Age :30

===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

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

相关问题 使用TestNG DataProvider时,是否可以从SAME Excel工作表中读取和写入参数? - When using TestNG DataProvider is there a way to read and write parameters from the SAME Excel sheet? 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数据提供程序检索特定的数据行? - How do I retrieve a specific row of data from a testng dataprovider? 如何为不同行的不同列号的 Excel 工作表编写 TestNG DataProvider 注释 - How to write TestNG DataProvider annotation for Excel sheet with different column numbers for different rows 在Testng中使用DataProvider从文件读取数据 - Reading data from file using DataProvider in testng 如何从Java中的ResultSetMetaData获取不同的数据类型? - How can I get different datatypes from ResultSetMetaData in Java? 如何使用 TestNG 和 DataProvider 顺序执行测试 - How to execute tests sequentially using TestNG and DataProvider 如何使用TestNG DataProvider传递&gt; 10个参数? - how to pass > 10 parameters using TestNG DataProvider? 如何从Testng中的多个文件中馈送数据提供者 - How to feed dataprovider from multiple files in testng
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM