0

I tried searching for relevant information for this question but wasn't able to find. Here is the question: There are 2 sheets in same excel. I want to print the data from both sheets using TestNG DataProvider only once in the same class.

Sheet1:

UserName Password

administrator1 password1

administrator2 password2

Sheet2:

FirstName LastName

abc def

pqr xyz

String path=<filePath>;
String sheetName;
@Test (dataProvider="loginInfo")
public void loginVerification(String username, String password) throws InterruptedException
{
    sheetName="Sheet1";
    System.out.println("Username:"+username+" "+"Password:"+password);
    System.out.println("************");
}

@Test (dataProvider="loginInfo")
public void userNames(String firstName, String lastName)
{
    sheetName="sheet2";
    System.out.println("FirstName:"+firstName+" LastName:"+lastName);
    System.out.println("*******");
}


@DataProvider(name="loginInfo")
public Object[][] userFormData() throws BiffException, IOException
{
    Object[][] data = testData(path, sheetName);
    return data;
}

public Object[][] testData(String path, String sheetName) throws BiffException, IOException
{
    <code to read excel data>
        }
    }
    return inputData;
}

When I run the code, it just skips execution with Null Pointer exception. If I declare sheetName at the beginning as I did for filePath, only data from 1st sheet is picked. Any help on how to proceed here is appreciated.!

user0450
  • 15
  • 1
  • 5

1 Answers1

0

The issue is that dataProvider gets the data before running the test, but you set the sheetname inside the test. So the sheetname is in that case null. This is why you get a nullpointer. The sheetname would get the value after running the test.

So if you want to use the same dataprovider no matter what. Then I would add the sheet switch to after the first test.

So currently the following code will work like this.

  1. Dataprovider gets initated with the sheetName = "Sheet1"
  2. loginVerification test is ran and after the test it changes the sheetName value to "Sheet2".
  3. Dataprovider gets initated again for the userNames test, but this time the sheetname is Sheet2.
  4. userNames test is ran with the data gotten from Sheet2.

    String path=<filePath>;
    String sheetName = "SheetName1";
                @Test (dataProvider="loginInfo")
                public void loginVerification(String username, String password) throws InterruptedException
    {
    
        System.out.println("Username:"+username+" "+"Password:"+password);
        System.out.println("************");
        sheetName="Sheet2";
    }
    
    @Test (dataProvider="loginInfo")
    public void userNames(String firstName, String lastName)
    {
    
        System.out.println("FirstName:"+firstName+" LastName:"+lastName);
        System.out.println("*******");
    }
    
    
    @DataProvider(name="loginInfo")
    public Object[][] userFormData() throws BiffException, IOException
    {
        Object[][] data = testData(path, sheetName);
        return data;
    }
    
    public Object[][] testData(String path, String sheetName) throws BiffException, IOException
    {
        <code to read excel data>
            }
        }
        return inputData;
    }
    

    With this solution the tests are error prone. If loginVerification test fails then the sheetname doesn't get updated and it will use Sheetname1 still.

    I prefer to create 2 different dataproviders if they take data from different sheets.

Madis Kangro
  • 293
  • 3
  • 12