0

基类中的数据提供者

@DataProvider(name = "AAA")
public Iterator<Object[]> AAA()
        throws DataDrivenFrameworkException {
    String excelResource = System.getProperty("user.dir")
            + System.getProperty("file.separator") + "resources"
            + System.getProperty("file.separator") + "ExcelTestData.xlsx";
    ExcelFile excelFile = new ExcelFile(excelResource);
    excelFile.setSheetName("SheetNameAAA");
    excelFile.setTestCaseHeaderName("TestCaseNameColumn");
    List<List<String>> TestData = excelFile
            .getDataUsingTestCaseName("TestCase1");
    return SeleniumTestNGHelper
            .toObjectArrayIterator(TestData);
}

@DataProvider(name = "BBB")
public Iterator<Object[]> BBB()
        throws DataDrivenFrameworkException {
    String excelResource = System.getProperty("user.dir")
            + System.getProperty("file.separator") + "resources"
            + System.getProperty("file.separator") + "ExcelTestData.xlsx";
    ExcelFile excelFile = new ExcelFile(excelResource);
    excelFile.setSheetName("SheetNameBBB");
    excelFile.setTestCaseHeaderName("TestCaseNameColumn");
    List<List<String>> TestData = excelFile
            .getDataUsingTestCaseName("TestCase2");
    return SeleniumTestNGHelper.toObjectArrayIterator(TestData);
}

测试班

@Test(description = "AAA Tests", dataProvider = "AAA")
public void testAAA(){
}

@Test(description = "BBB Tests", dataProvider = "BBB")
public void testBBB(){
}

测试NG XML

<test name="AAA Test Using Chrome" preserve-order="true">
    <parameter name="browser" value="chrome" />
    <classes>
        <class name="com.tests.TestClass" />
    </classes>
</test>

想知道是否可以只使用 1 个 DataProvider 而不是 2 个?我需要将“setSheetName”作为参数传递。想知道如何做到这一点?任何反馈都会有很大帮助。我需要从文件中的不同工作表中读取数据。

谢谢。

***** 更新 ****

基类上的数据提供者

@DataProvider(name = "TestType")
public static Iterator<Object[]> TestType(Method Sheet)
        throws DataDrivenFrameworkException {
    String SheetName = Sheet.getName();
    String excelResource = System.getProperty("user.dir")
            + System.getProperty("file.separator") + "resources"
            + System.getProperty("file.separator") + "ExcelTestData.xlsx";
    ExcelFile excelFile = new ExcelFile(excelResource);
    excelFile.setSheetName(SheetName);
    excelFile.setTestCaseHeaderName("TestCaseNameColumn");
    List<List<String>> PensionPlanTestData = excelFile
            .getDataUsingTestCaseName("PensionPlanPodTestCase");
    return SeleniumTestNGHelper.toObjectArrayIterator(PensionPlanTestData);
}

测试班

@Test(dataProvider = "TestType")
public void PensionPlanPodTests(String UserName, String Password,
        String AccountPageTitle, String FullName, String Address,
        String DateOfBirth, String PhoneNumber, String ClientNumber,
        String InstitutionName, String InstitutionAddress,
        String PositionTitle, String Beneficiaries,
        String TotalLifeInsuranceAmount) throws InterruptedException {

    HomePage HomePG = PageFactory.initElements(driver, HomePage.class);
    MainPage MainPG = PageFactory.initElements(driver, MainPage.class);

    Map<String, String> logInData = new HashMap<String, String>();
    logInData.put("userName", UserName);
    logInData.put("password", Password);
    HomePG.SignIn(logInData);
    // MainPG.CloseTabNotifier();

    if (AccountPageTitle.length() > 1) {
        MainPG.VerifyPageTitle(AccountPageTitle);
    } else {
        System.out.println("Not Verifying Page Title ...");
        Reporter.log("Not Verifying Page Title ...");
    }

    Map<String, String> UserInfoData = new HashMap<String, String>();
    UserInfoData.put("fullname", FullName);
    UserInfoData.put("address", Address);
    UserInfoData.put("DOB", DateOfBirth);
    UserInfoData.put("Phone", PhoneNumber);
    UserInfoData.put("ClientNum", ClientNumber);
    MainPG.UserPersonalInformation(UserInfoData);

    if (PositionTitle.length() > 1) {
        Map<String, String> InstitutionData = new HashMap<String, String>();
        InstitutionData.put("institutionName", InstitutionName);
        InstitutionData.put("institutionAddress", InstitutionAddress);
        InstitutionData.put("positionTitle", PositionTitle);
        MainPG.UserInstitutionInformation(InstitutionData);
    } else {
        System.out
                .println("Not Verifying Any Institution Data Because User Do Not Hold Any Position ...");
        Reporter.log("Not Verifying Any Institution Data Because User Do Not Hold Any Position ...");
    }

    if (Beneficiaries.length() > 1) {
        MainPG.VerifyLifeInsuranceBeneficiaries(Beneficiaries);
    } else {
        System.out
                .println("Not Verifying Life Insurance Beneficiaries ...");
        Reporter.log("Not Verifying Life Insurance Beneficiaries ...");
    }

    if (TotalLifeInsuranceAmount.length() > 1) {
        MainPG.TotalLifeInsuranceAmount(TotalLifeInsuranceAmount);
    } else {
        System.out.println("Not Verifying Total Life Insurance Amount ...");
        Reporter.log("Not Verifying Total Life Insurance Amount ...");
    }
    MainPG.LogOut();
}

我现在得到的最新错误:

失败:PensionPlanPodTests org.testng.TestNGException:数据提供者正在尝试传递 21 个参数,但方法 com.tests.DataProviderParametersIntegrationExample#PensionPlanPodTests 需要 13 个

4

1 回答 1

0

您可以拥有一个以 java.lang.reflect.Method 作为参数的 DataProvider。TestNG 将传递此参数的当前测试方法。因此,假设您有 2 个名为 AAA 和 BBB 的 @test 方法,您可以读取这些方法名称并将它们用作参数

public class SampleTest {

    @Test(dataProvider = "myDP")
    public void AAA(String a) {

    Assert.assertTrue(true);

    }

    @Test(dataProvider = "myDP")
    public void BBB(String a) {
    Assert.assertTrue(true);

    }

    @DataProvider(name = "myDP")
    public Object[][] myDP(Method m) {

    String sheetName = m.getName();
    return new String[][] { { "a" } };

    }

}
于 2015-04-14T19:15:13.547 回答