Parameter error when running selenium webdriver test case in Java

Deadly 提交于 2019-12-05 19:42:15

The TestNGException is coming because your @Test method requires 2 parameters (i.e. PrimaryBorrowerBaseName1 and PrimaryBorrowerBaseName) but you are not passing them.

To solve this you need to supply these parameters when TestNG invokes your @Test method (i.e. PopulateBorrower). TestNG provides 2 ways to do the same.

Supplying parameters in TestNG

  1. Parameters from testng.xml
  2. Parameters with DataProviders

You can try any one of below approach to solve your issue.

Solution 1: Parameters from testng.xml

The testng.xml file:

<suite name="My Suite">
    <parameter name="primary-borrower-base-name1" value="value1" />
    <parameter name="primary-borrower-base-name"  value="value"/>
    <test name="My Test">
        <classes>
            <class
                name="yourpackage.PopulateBorrowerTestClass2" />
        </classes>
    </test>
</suite>

Your test class:

package yourpackage;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class PopulateBorrowerTestClass2 {

    @Parameters({ "primary-borrower-base-name1", "primary-borrower-base-name" })
    @Test
    public static void PopulateBorrower(String PrimaryBorrowerBaseName1, String PrimaryBorrowerBaseName)
    {       
        System.out.println(PrimaryBorrowerBaseName1 + " " + PrimaryBorrowerBaseName);
       //your all code goes here
     }
}

Solution 2: Parameters with DataProviders

The testng.xml file:

<suite name="My Suite">
    <test name="My Test">
        <classes>
            <class name="yourpackage.PopulateBorrowerTestClass1" />
        </classes>
    </test>
</suite>

Your test class:

package yourpackage;

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

public class PopulateBorrowerTestClass1 {

    //This method will provide data to any test method that declares that its Data Provider
    //is named "test1"
    @DataProvider(name = "test1")
    public Object[][] createData1() {
     return new Object[][] {
       { "PrimaryBorrowerBaseName1Value", "PrimaryBorrowerBaseNameValue" }
     };
    }

    //This test method declares that its data should be supplied by the Data Provider
    //named "test1"
    @Test(dataProvider = "test1")
    public static void PopulateBorrower(String PrimaryBorrowerBaseName1, String PrimaryBorrowerBaseName)
    {       
        System.out.println(PrimaryBorrowerBaseName1 + " " + PrimaryBorrowerBaseName);
       //your all code goes here
     }
}

Why do you use static function as a test? It should not be static!

Does your unit test use the @DataProvider annotation (@Parameterized in JUnit 4.11)? If so, and you have defined a data provider method, then your @Test annotation needs to recognize it as such like so:

@Test
@Parameters(value="number", value2="digit")
public void parameterIntTest(int number, int digit) {
    System.out.println("Parameterized Number is : " + number + " and " + digit);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!