简体   繁体   中英

Need help using DataProvider with Testng

Data Provider in Base Class

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

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

Wondering if it's possible to use just 1 DataProvider instead of 2? I need to pass "setSheetName" as an argument. Would like to know how to accomplish this? Any feedback will be great help. I need to read data from different sheets in the file.

Thanks.

***** Update ****

Data Provider On Base Class

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

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

Latest Error I am getting now:

FAILED: PensionPlanPodTests org.testng.TestNGException: The data provider is trying to pass 21 parameters but the method com.tests.DataProviderParametersIntegrationExample#PensionPlanPodTests takes 13

You can have a single DataProvider that takes java.lang.reflect.Method as the parameter .TestNG will pass the current test method for this parameter. So lets say, if you have 2 @test methods with name AAA and BBB, you can read these method names and use them as parameter

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

    }

}

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