在Testng上使用DataProvider需要帮助

时间:2015-04-14 18:05:06

标签: java selenium-webdriver automation testng testng-dataprovider

基类中的数据提供者

@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(){
}

TestNG 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个?我需要传递&#34; setSheetName&#34;作为一个论点。想知道怎么做到这一点?任何反馈都会有很大的帮助。我需要从文件中的不同表格中读取数据。

感谢。

*****更新****

基类上的数据提供者

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

1 个答案:

答案 0 :(得分: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" } };

    }

}