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

在selenium中使用数据提供程序时将第一组数据获取为空值,selenium,testng,testng-dataprovider,Selenium,Testng,Testng Dataprovider,我正在尝试使用selenium中的数据提供程序从excel工作表中获取数据。当数据返回/传递给调用者函数时,我第一次得到空值,即使循环从具有实际数据的第二行开始。我不知道为什么会这样 package pageobjectmodel; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import org.testng.Assert; import org.testng.annotat

我正在尝试使用selenium中的数据提供程序从excel工作表中获取数据。当数据返回/传递给调用者函数时,我第一次得到空值,即使循环从具有实际数据的第二行开始。我不知道为什么会这样

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();
    }
}
packagepageobjectmodel;
导入java.io.IOException;
导入java.lang.reflect.InvocationTargetException;
导入org.testng.Assert;
导入org.testng.annotations.AfterMethod;
导入org.testng.annotations.BeforeMethod;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Test;
导入Utils.readingexcel文件;
导入Utils.TestBase;
导入pagebasedexecution.LinkedInloginPage;
公共类LinkedInLoginTest扩展了TestBase
{
//公共静态读取文件;
公共静态读取excelfile excelfile;
LinkedIn登录页面登录页面;
public LinkedInLoginTest()引发IOException
{
超级();
}
@预处理法
public void Setup()引发IOException、NoSuchMethodException、SecurityException、IllegalAccessException、IllegalArgumentException、InvocationTargetException
{
浏览器设置();
loginPage=newlinkedinloginpage();
}
@测试(dataProvider=“TestData”)
public void LoginTest(字符串uname,字符串password)抛出InterruptedException
{
//System.out.println(“收到的数据是----”+uname+“,”+密码);
loginPage.LoginToAccount(uname,密码);
字符串title=loginPage.VerifyTitle();
Assert.assertEquals(标题,“LinkedIn”,“无法登录:无效凭据”);
睡眠(5000);
}
/*@测试(优先级=2)
public void VerifyloginPageTitleTest()引发InterruptedException
{
字符串title=loginPage.VerifyTitle();
Assert.assertEquals(标题,“LinkedIn”,“无法登录:无效凭据”);
}*/
@数据提供者
公共对象[][]TestData()引发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);
对象[][]凭据=新对象[行][colm];
对于(int i=1;i
Object[][] credentials = new Object[rows][colm];
您正在创建一个对象数组,该数组的行数和列数在工作表中,但您正在从第二行开始在此数组中插入值,因此在第一行中,它将采用默认的空值

替换代码:

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;jc)您可以共享excel内容吗?
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]);
}