在 selenium 中使用数据提供程序时获取第一组数据作为空值

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

我正在尝试使用 selenium 中的数据提供程序从 excel sheet 中获取数据。当数据 returned/passed 到调用函数时,我第一次得到空值,即使循环从具有实际数据的第二行开始。我不确定为什么会这样。

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();
    }
}

尽管我正在从第二行获取数据,但它以某种方式从第一行(列名)获取数据并且第一组数据返回为 null,null。我提供了错误控制台的屏幕截图并突出显示了错误部分。任何帮助表示赞赏。

谢谢

这是因为这里

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

您正在创建对象数组,其中行数和列数存在于您的 sheet 中,但您是从第二行在此数组中插入值,因此在第一行中它采用默认空值。

替换代码:

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]);
}

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]);
}