简体   繁体   中英

I am trying to get data from excel sheet and pass those test data into the login & password field but I am getting following error

Error:

FAILED: cptoadsLogin org.testng.TestNGException: The data provider is trying to pass 2 parameters but the method com.toads.Script.LoginDemo#cptoadsLogin takes 0 at org.testng.internal.Invoker.injectParameters(Invoker.java:1225) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1118) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) at org.testng.TestRunner.privateRun(TestRunner.java:774) at org.testng.TestRunner.run(TestRunner.java:624) at org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at org.testng.SuiteRunner.run(SuiteRunner.java:261) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at org.testng.Test NG.runSuitesLocally(TestNG.java:1140) at org.testng.TestNG.run(TestNG.java:1048) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

Page object:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class ToadsLogin {

    @FindBy(how=How.XPATH,using="//input[@id='Username']")
    WebElement username;

    @FindBy(how=How.XPATH,using="//input[@id='Password']")
    WebElement password;

    @FindBy(how=How.XPATH,using="//button")
    WebElement loginbtn;

    public ToadsLogin(WebDriver driver)
    {
        PageFactory.initElements(driver, this);
    }

    public void unpwd(String un,String pwd)
    {
        username.sendKeys("un");
        password.sendKeys("pwd");
    }

    public void clikonLogin()
    {
        loginbtn.click();
    }
}

TestNG Script:

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.toads.PoM.ToadsLogin;
import com.toads.lib.ExcelDataConfig;

public class LoginDemo extends SuperTestNG {

    @Test(dataProvider="toadsLogin")
    public void cptoadsLogin() throws Exception
    {
        ToadsLogin tlogin=new ToadsLogin(driver);
        tlogin.unpwd("un", "pwd");
        tlogin.clikonLogin();
        Thread.sleep(5000);
        driver.close();
    }


    @DataProvider(name="toadsLogin")
    public Object[][] cpToadsLogin()
    {
        ExcelDataConfig config=new ExcelDataConfig("D:\\login.xlsx");
        int rows=config.getRowCount(0);


        Object[][] data1= new Object[rows][2];
        for(int i=0;i<rows;i++)
        {
            data1[i][0]=config.getData(0, i, 0);
            data1[i][1]=config.getData(0, i, 1);

        }
        return data1;
    }

}

The dataProvider expects method cptoadsLogin to get parameters.

The data provider is trying to pass 2 parameters but the method cptoadsLogin takes 0

You need to change the method signature to receive 2 parameters

@Test(dataProvider = "toadsLogin")
public void cptoadsLogin(String user, String pwd) throws Exception
{
    ToadsLogin tlogin=new ToadsLogin(driver);
    tlogin.unpwd(user, pwd);
    tlogin.clikonLogin();
    Thread.sleep(5000);
    driver.close();
}

user will have the value from data1[i][0] and pwd will have the value from data1[i][1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM