Question

I've got an Excel Spreadsheet in which I store all of the test Credit Cards. These credit cards have different types. Some of these are VISA, others are MasterCard, Amex etc...

I have got a test case in which I sometimes want to use VISA cards, and sometimes MasterCard cards.

Is it possible to pass parameters to the @DataProvider?

Here is my code for @DataProvider:

@DataProvider(name="dpCreditCards")
public Object[][] getCreditCards() {
    Object[][] testData = null;
    try {
        FileInputStream fis = new FileInputStream(dir);
        XSSFWorkbook workbook = new XSSFWorkbook(fis);

        XSSFSheet worksheet = workbook.getSheet("Credit Cards");
        String type = "";
        String cardNumber = "";
        int numOfRows = worksheet.getPhysicalNumberOfRows();
        int j = 0;
        if (numOfRows > 0) {

            for (int i = 1; i < numOfRows; i++) {
                XSSFRow r = worksheet.getRow(i);                
                if (r.getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    type = Integer.toString((int)r.getCell(0).getNumericCellValue());
                } else if (r.getCell(0).getCellType() == Cell.CELL_TYPE_STRING) {
                    type = r.getCell(0).getStringCellValue();       
                }

                if (type.equalsIgnoreCase("visa"))
                    j++;
            }

            testData = new Object[j][1];
        }

        for (int i = 1; i < numOfRows; i++) {
            XSSFRow r = worksheet.getRow(i);                
            if (r.getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC) {
                type = Integer.toString((int)r.getCell(0).getNumericCellValue());
            } else if (r.getCell(0).getCellType() == Cell.CELL_TYPE_STRING) {
                type = r.getCell(0).getStringCellValue();       
            }

            if (type.equalsIgnoreCase("visa")) {
                if (r.getCell(1).getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    cardNumber = Integer.toString((int)r.getCell(1).getNumericCellValue());
                } else if (r.getCell(1).getCellType() == Cell.CELL_TYPE_STRING) {
                    cardNumber = r.getCell(1).getStringCellValue();     
                }

                testData[i-1][0] = cardNumber;
            }               
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }   

    return testData;
}

I've checked this link: http://testng.org/doc/documentation-main.html#parameters-dataproviders but couldn't find anything that would work for me. It suggests to pass Method m as parameter to data provider, but I couldn't find a useful method that m has.

Thanks in advance

Was it helpful?

Solution

One way could be to read the parameters in the listeners and then set a property which can be used in the dataprovider.

Implement ITestListener or ISuiteListener, depending upon how you are structuring your tests. Set the global card property or threadlocal property (again depending upon how you are running your tests sequentially/parallely) in the onStart methods of any.

Read this property in your dataprovider.

OTHER TIPS

  1. If you just want to test a subset of your data you could filter it out in your data provider.

  2. If you want to apply different tests to different subsets of data, then you could use different data providers for each of the test methods. Or you could use tbe Method parameter to decide what data to return from your data provider.

  3. If you want to be able to configure the type of data to be loaded for your test at runtime, you could use a env variable/system property for this.

Bottom line:

  1. If you run the same test against different subsets of data, you have multiple ways to specify the subset you want to check

  2. If you want to run different tests for different subsets of data then you should probably go with multiple data providers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top