0

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

Barett
  • 5,826
  • 6
  • 51
  • 55
RME
  • 1
  • 2

1 Answers1

0

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

    }

}
Gosaka
  • 181
  • 7
  • @Gasoka, thank you so much for the reply. Now I am facing another issue. I have updated my code above. I am getting parameter number mismatch. Can you please let me know what I am doing wrong? Thanks – RME Apr 15 '15 at 02:40
  • The issue is self explanatory, your test method only has 13 arguments ,but the data provider is passing 21 parameters. You can either increase your method arguments to accept 21 parameters or reduce the number of parameters you pass from DataProvider to 13 – Gosaka Apr 15 '15 at 04:08
  • Thanks for the reply. The problem is, different sheets has different number of fields & I would like to use 1 @DataProvider for all tests. – RME Apr 15 '15 at 12:24
  • Some how you need to consolidate your parameters in your test to be accepted by all data sheets – Gosaka Apr 15 '15 at 16:02