使用@Dataprovider从多个Excel工作表中提取数据并将其传递给@test

时间:2016-06-15 12:46:23

标签: java selenium-webdriver collections testng testng-dataprovider

我遇到与@Dataprovider@Test

相关的问题

场景:我需要借助@dataprovider注释从多个工作表(sheet1,sheet2,sheet3,.....)中获取数据,我有一个web应用程序,其中有多个网页我必须传递数据。

我已经使用POM实现了它,我需要立即从多张表中获取数据并将其传递给@test。

下面的

是片段:

 @DataProvider
    public Object[][] getHrwsIdentityData() throws IOException 
    {   
        List<Object[]> DataList = null;
        loggerManager.logger.info("Reading the position testdata from excel sheet");

        DataList = Excel.excelRead(".\\resources\\data\\HR Workwise Automation -New Hires.xlsx", "Identity Details", "Execute Flag", "Y");
        return DataList.toArray(new Object[DataList.size()][]);
    }

    @DataProvider
    public Object[][] getHrwsPersonalData() throws IOException
    {
        List<Object[]> DataList = null;
        loggerManager.logger.info("Reading the position testdata from excel sheet");
        DataList = Excel.excelRead(".\\resources\\data\\HR Workwise Automation -New Hires.xlsx", "Personal Information", "Execute Flag", "Y");
        return DataList.toArray(new Object[DataList.size()][]);
    }


 @Test(priority = 1,enabled = true, dataProvider="getHrwsPersonalData", description="Add New Employee in Manage Employees Page; Coverage: AddNewEmployee")
    public void updateEmployeePersonalInfo(LinkedHashMap<String, String> DataSet) throws IOException
    {
        hrws.addEmployeePersonalInfo(DataSet);
    }

    @Test(priority = 2,enabled = true, dataProvider="getHrwsJobInformation", description="Add New Employee in Manage Employees Page; Coverage: AddNewEmployee")
    public void updateEmployeeJobInfo(LinkedHashMap<String, String> DataSet) throws IOException
    {
        hrws.addEmployeeJobInfo(DataSet);
    }

我尝试为每个@DataProviders使用不同的单@Test,但没有运气!!

以下是readExcel文件

public static List<Object[]> excelRead(String sExcelPath, String sSheetName, String sCondCol, String sCondVal) throws IOException{

        String[] sHeaderKey = new String[0];
        String[] sValue = new String[0];
        LinkedHashMap<String, String> RowData;
        List<Object[]> DataList = new ArrayList<>();

        try {
            FileInputStream oFis = new FileInputStream(sExcelPath);

            Workbook workbook = null;

            // Using XSSF for xlsx format, for xls use HSSF

            workbook = new XSSFWorkbook(oFis);

            Sheet sheet = workbook.getSheet(sSheetName);

            Iterator<Row> rowIterator = sheet.iterator();

            DataFormatter formatter = new DataFormatter(Locale.US);

            while (rowIterator.hasNext()) {
                Boolean bHeaderRow = false;
                sValue = new String[0];
                Row row = rowIterator.next();
                if (row.getRowNum() == 0) {
                    bHeaderRow = true;
                }

                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    if (bHeaderRow && (cell.getCellType() != Cell.CELL_TYPE_BLANK)) {
                        sHeaderKey = Arrays.copyOf(sHeaderKey, sHeaderKey.length + 1);
                        sHeaderKey[cell.getColumnIndex()] = formatter.formatCellValue(cell);
                    } else if ((!bHeaderRow) && (sHeaderKey[cell.getColumnIndex()] != null)) {
                        sValue = Arrays.copyOf(sValue, sValue.length + 1);
                        sValue[cell.getColumnIndex()] = formatter.formatCellValue(cell);
                    }
                }

                if ((sHeaderKey.length != 0) && (sValue.length != 0)) {
                    RowData = new LinkedHashMap<String, String>();

                    for (int i = 0; i < sHeaderKey.length; i++) {
                        RowData.put(sHeaderKey[i], sValue[i]);
                    }

                    if(RowData.get(sCondCol).trim().toLowerCase().equals(sCondVal.trim().toLowerCase())){
                        DataList.add(new Object[]{RowData});
                    }

                }
            }
            workbook.close();
            oFis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw e;
        }
        return DataList;

    }

我需要将这两个@dataproviders合并为一个!!

1 个答案:

答案 0 :(得分:1)

您不需要为不同的测试提供多个数据提供程序。您需要一种机制来识别哪个测试正在调用数据提供程序。 TestNg数据提供程序过载,您可以创建这样的数据提供程序方法请不要对此方法中的代码至关重要,因为它仅用于演示

    @DataProvider(name = "AllTestData")
public static Object[][] GetDataForEveryOne(ITestNGMethod testContext)
{
    if(testContext.getMethodName().equals("LoginWithMultipleUsersTest"))
    {
        String[][] usernamePassArray = { { "testType1", "pass1" },
                                         { "testType2", "pass1" }};

        return usernamePassArray;
    }
    else if(testContext.getMethodName().equals("LoginWithMultipleDataTest"))
    {
        String[][] usernamePassArray = { { "user1", "pass1" },
                 { "user2", "pass2" },
                 { "user3", "pass3" } };

        return usernamePassArray;
    }
    return null;
}

我刚试图展示如何在数据提供程序中获取测试上下文,这反过来可以让您获得测试方法名称。此测试方法名称可用作从与此测试对应的Excel中提取数据的键。