3

我正在尝试在 Selenium webdriver 中运行此方法,但我继续收到此错误:

org.testng.TestNGException: 
Method PopulateBorrower requires 2 parameters but 0 were supplied in the @Test annotation.
    at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:198)
    at org.testng.internal.Parameters.createParameters(Parameters.java:134)
    at org.testng.internal.Parameters.createParameters(Parameters.java:370)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:447)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1384)
    at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

--------->我做错了什么?代码如下。我在 SetUpBase 单独的类中设置了它。

@Test
public static void PopulateBorrower(String PrimaryBorrowerBaseName1,String PrimaryBorrowerBaseName)
{       
    String fName= PrimaryBorrowerInputDictionary.get(BorrowerFirstNameElement);
    String lName=PrimaryBorrowerInputDictionary.get(BorrowerLastNameElement);
    String suffix=PrimaryBorrowerInputDictionary.get(BorrowerSuffixElement);
    String social=PrimaryBorrowerInputDictionary.get(BorrowerSocialSecurityNumberElement);
    String homePhone=PrimaryBorrowerInputDictionary.get(BorrowerHomePhoneNumberElement);
    String schoolYears=PrimaryBorrowerInputDictionary.get(BorrowerYearsInSchoolElement);

    driver.findElement(By.id(PrimaryBorrowerBaseName1 + BorrowerFirstNameElement)).sendKeys(fName);
    driver.findElement(By.id(PrimaryBorrowerBaseName1 + BorrowerLastNameElement)).sendKeys(lName);
    driver.findElement(By.id(PrimaryBorrowerBaseName1 + BorrowerSuffixElement)).sendKeys(suffix);
    driver.findElement(By.name(PrimaryBorrowerBaseName + BorrowerHomePhoneNumberElement)).sendKeys(social);

    driver.findElement(By.name(PrimaryBorrowerBaseName + BorrowerHomePhoneNumberElement)).sendKeys(homePhone);
    driver.findElement(By.name(PrimaryBorrowerBaseName + BorrowerYearsInSchoolElement)).sendKeys(schoolYears);
    driver.findElement(By.name(PrimaryBorrowerBaseName +BorrowerMarriageStatusElement )).click();


    assertEquals(driver.findElement(By.id(PrimaryBorrowerBaseName + BorrowerFirstNameElement))
                .getText(), fName);

    assertEquals(driver.findElement(By.id(PrimaryBorrowerBaseName + BorrowerLastNameElement))
            .getText(), lName);

    assertEquals(driver.findElement(By.id(PrimaryBorrowerBaseName + BorrowerSuffixElement))
            .getText(), suffix);

    assertEquals(driver.findElement(By.id(PrimaryBorrowerBaseName + BorrowerHomePhoneNumberElement))
            .getText(), homePhone);

    assertEquals(driver.findElement(By.id(PrimaryBorrowerBaseName + BorrowerYearsInSchoolElement))
            .getText(), schoolYears);

    assertTrue(driver.findElement(By.id(PrimaryBorrowerBaseName + BorrowerMarriageStatusElement))
            .isSelected());
 }
4

3 回答 3

4

即将TestNGException到来是因为您的@Test方法需要 2 个参数(即PrimaryBorrowerBaseName1PrimaryBorrowerBaseName),但您没有传递它们。

为了解决这个问题,您需要在TestNG调用您的@Test方法(即PopulateBorrower)时提供这些参数。TestNG提供 2 种方法来做同样的事情。

提供参数_TestNG

  1. 来自 testng.xml 的参数
  2. 带有 DataProviders 的参数

您可以尝试以下任何一种方法来解决您的问题。

解决方案 1:来自 testng.xml 的参数

testng.xml文件_

<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>

你的测试课

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
     }
}

解决方案 2:带有 DataProviders 的参数

testng.xml文件_

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

你的测试课

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
     }
}
于 2016-09-16T18:52:09.423 回答
2

为什么使用静态函数作为测试?它不应该是静态的!

于 2013-06-28T19:48:41.383 回答
1

您的单元测试是否使用 @DataProvider 注释(JUnit 4.11 中的 @Parameterized)?如果是这样,并且您已经定义了一个数据提供者方法,那么您的 @Test 注释需要像这样识别它:

@Test
@Parameters(value="number", value2="digit")
public void parameterIntTest(int number, int digit) {
    System.out.println("Parameterized Number is : " + number + " and " + digit);
}
于 2013-06-28T18:48:01.407 回答