Search code examples
seleniumtestngtestng-dataprovider

Getting first set of data as null value while using data provider in selenium


I am trying to fetch the data using data provider in selenium from an excel sheet. When the data is returned/passed on to the caller function, i get null values for the first time even though loop begins from the second row having the actual data. I am not sure why this is happening.

package pageobjectmodel;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import Utils.ReadingExcelfile;
import Utils.TestBase;
import pagebasedexecution.LinkedInloginPage;

public class LinkedInLoginTest extends TestBase 
{


    //public static ReadExcel excelfile;
    public static ReadingExcelfile excelfile;
    LinkedInloginPage loginPage;


    public LinkedInLoginTest() throws IOException
    {
        super();
    }

    @BeforeMethod
    public void Setup() throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        BrowserSetup();
        loginPage = new LinkedInloginPage();
    }


    @Test(dataProvider="TestData")
    public void LoginTest(String uname, String password) throws InterruptedException
    {
    //  System.out.println("received data is --- " +uname + " , " + password);
        loginPage.LoginIntoAccount(uname, password);
        String title = loginPage.VerifyTitle();
        Assert.assertEquals(title, "LinkedIn", "Unable to login: invalid credentials");
        Thread.sleep(5000);
    }


    /*@Test(priority=2)
    public void VerifyloginPageTitleTest() throws InterruptedException
    {
        String title = loginPage.VerifyTitle();
        Assert.assertEquals(title, "LinkedIn", "Unable to login: invalid credentials");
    }*/


    @DataProvider
    public Object[][] TestData() throws IOException 
    {
        excelfile = new ReadingExcelfile(System.getProperty("user.dir")+"\\src\\main\\java\\testData\\LinkedIn.xlsx");
        int rows = excelfile.RowCount(1);
        int colm = excelfile.TotalColm("LoginPage", 0);

        Object[][] credentials = new Object[rows][colm];

        for(int i = 1; i < rows; i++) 
        {
            for(int j = 0; j<colm; j++) 
            {
                credentials[i][j] = excelfile.getdata("LoginPage", i, j); 
                System.out.println("Fetched data from excel sheet is -- "+credentials[i][j]);
            }
        }
        return credentials;
        }

    @AfterMethod
    public void closebrowser() 
    {
        System.out.println("quitting browser");
        driver.quit();
    }
}

enter image description here

Even though i am fetching the data from second row, somehow it's fetching the data from first row(column names) and first set of data is returned as null, null. i have provided the screenshot of error console and highlighted the error portion. Any help is appreciated.

Thanks


Solution

  • This is because here

    Object[][] credentials = new Object[rows][colm];
    

    you are creating array of object with number of rows and colums present in your sheet but you are inserting value in this array from second row So In first row it is taking default null values.

    Replace code :

    for(int j = 0; j<colm; j++) 
    {
           credentials[i][j] = excelfile.getdata("LoginPage", i, j); 
           System.out.println("Fetched data from excel sheet is -- "+credentials[i][j]);
    }
    

    With

    for(int j = 0; j<colm; j++) 
    {
        credentials[i-1][j] = excelfile.getdata("LoginPage", i, j); 
        System.out.println("Fetched data from excel sheet is -- "+credentials[i-1][j]);
    }