1

I am facing an issue wherein I have created a parameterized method that uses dataprovider. Both DataProvider and method and created in same class. Now, I want to call this method from another class but it requires parameters to be passed which I cannot since they are being read from dataProvider.

I have tried declaring the dataProvider in different class too but that doesn't work. Please suggest some workaround for it.

NOTE: I have a restriction that I cannot use TestNG.xml to implement this scenario.

Please find the code below:

DataProvider:

@DataProvider(name = "TestSuite")
    public Object[][] dataSheetTraverser() {
        String SheetName = "ProgLang";
        datatable = new Xls_Reader(TestDataSheetPath_ProgLang);
        int rowcount = datatable.getRowCount(SheetName);
        Object result[][] = new Object[rowcount - 1][3];
        for (int i = 2; i < rowcount + 1; i++) {
            result[i - 2][0] = SheetName;
            result[i - 2][1] = i;
            result[i - 2][2] = datatable.getCellData(SheetName, "caseType", i);

        }
        return result;
    }

Test Method:

@Test(dataProvider="TestSuite_ProgLang",priority =2)
    public void TC_Verify_EditProgLang(String SheetName,int i, String caseType)
    {

        String test1= datatable.getCellData(SheetName, "Skills", i);
        String test2= datatable.getCellData(SheetName, "Version", i);
        String test3= datatable.getCellData(SheetName, "LastUsed", i);
        String test4= datatable.getCellData(SheetName, "ExperienceYr", i);
        String ExperienceMn = datatable.getCellData(SheetName, "ExperienceMn", i);

        proglang.FillForm_ProgLang(Skills, Version, LastUsed, ExperienceYr, ExperienceMn);

    }

I want to call the above function TC_Verify_EditProgLang from another class. Please suggest.

Swati Khandelwal
  • 195
  • 2
  • 2
  • 14

1 Answers1

1

You can use the dataProviderClass attribute for calling from other class
in @Test and The Provider method Must be static :

public class StaticProvider {
      @DataProvider(name = "create")
      public static Object[][] createData() {
        return new Object[][] {
          new Object[] { new Integer(42) }
        };
      }
    }
    //different Class
    public class MyTest {
      @Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
      public void test(Integer n) {
        // ...
      }
    }

please check the Documentation :dataProviders

Raju
  • 459
  • 5
  • 23
  • the problem is that I have to keep DataProvider and test method inside same class and call the test method from another class (i.e., I have to call **MyTest** class from somewhere else) – Swati Khandelwal Feb 12 '18 at 09:52
  • you need create Object for MyTest Class in Other Class call method .in your scenario you need pass the data to method.new MyTest().TC_Verify_EditProgLang(....pass Parameters); – Raju Feb 13 '18 at 11:12