0
    @DataProvider (name=getData)
                public static Object[][] getData(){
                    Excelreader excel = new Excelreader("C:\\WorkspaceExcel\\Datasource.xlsx"); private static
        String sheetName="SmokeTest"; // Test Data stored in a a table with
        columns and rows. Ex. UserNmae and password are the columns and data
        for them in rows  private static String tableName="CreatePolicy";

        // Some logic to read the data table store the data in a 2
        dimentional array.

        // Some logic by which I store the columns as Keys and data for them
        as values in a HASHTABLE  Return the hashtable 
    }




       @Test (dataProvider="getData") 
        public void testData(Hashtable<String, String> data){
        /* Logic to read an .xlsx file which has Snenario name, TestCase Name  and its corespnding runflag. If the runflag is true i need to read the scenario name and testcase name from the .xlsx file.  */

/* I need some logic to pass the ScenarioName (equivalent to Sheetname in DP) and TestCase Name (equivalent to datatable name in DP) which i am getting by reading the excelfile to DataProvider, so i can get the data which i need to execute the test case */

/*I am doing the above as part of Hybridframework, where i have Scenario, TC, Test step details in one excel sheet and data for each testcase in one more sheet */
       }

My Question: I want some logic, so when @Test is executed, I should dynamically pass the data File Path, SheetName, TableName, so the same data provider can be used and that will give me different set of data to work upon.

Note: The data provider will return in the form of a hashtable for the data specified in excel in a tabularformat with a table name. So if worksheet path, sheetname and table name is passed to the dataprovider, then my DP will read that table and return the whole data table in the form of a hash table.

user3660045
  • 1
  • 1
  • 3

3 Answers3

0

I suggest you to provide parameters through XML as explain in Parameters from testng.xml

LoganMzz
  • 1,597
  • 3
  • 18
  • 31
  • I am wondering how we can achive this through XML parameterization. Here in the excel sheet each data table has multiple columns. As in case of dataprovider we have to use matching number (here its multiple) of parameters in @Test method so to avoid that i am using hashtable. So irespective of howmany parameter the data provider retuns, my test method does not need to add those parameters. My DP will return the data irespecive of the number of columns and rows in the data table. But my only concern is how can i pass dynamically the sheetname and datatablename for which the DP return the data. – user3660045 Oct 12 '15 at 14:05
0

If you just want to be able to reuse complex logic of fetching test data, why not just move it to helper function and pass your parameters in separate data providers?

public class ReusableDataprovider {
    @Test(dataProvider = "data_from_table1")
    public void test1(Hashtable<String, String> data) {
        Assert.assertEquals(data.get("Username"), "user_table1", "Wrong username");
        Assert.assertEquals(data.get("Password"), "pass_table1", "Wrong password");
    }

    @Test(dataProvider = "data_from_table2")
    public void test2(Hashtable<String, String> data) {
        Assert.assertEquals(data.get("Username"), "user_table2", "Wrong username");
        Assert.assertEquals(data.get("Password"), "pass_table2", "Wrong password");
    }

    @DataProvider
    protected Object[][] data_from_table1() {
        return fetchData("file1", "sheet1", "table1");
    }

    @DataProvider
    protected Object[][] data_from_table2() {
        return fetchData("file2", "sheet2", "table2");
    }

    protected Object[][] fetchData(String filePath, String sheetName, String tableName) {

        final Hashtable<String, String> data = new Hashtable<String, String>();
        // Do all the complex excel logic here
        data.put("Username", "user_" + tableName);
        data.put("Password", "pass_" + tableName);

        return new Object[][] {{data}};
    }

}
botchniaque
  • 4,698
  • 3
  • 35
  • 63
  • My Test metheod reads an .xlsx file with TCname, Runflag & it loops for the number of testcases with yes flag. for each TC it needs data which is there in .xlsx file and my DP will returns the data if i can dynamically pass the sheet and table name. As my # of TC will varry, so i would not be knowing howmnay DP i may need. so i need a solution which will use the same DP and provide different set of data which i need for different Test case. – user3660045 Oct 12 '15 at 14:24
  • Hiw many tast cases will you have? You say 'dynamic', but what would be their signatures (accepted parameters)?I don't fully understand. Maybe you could give some example data. – botchniaque Oct 12 '15 at 17:35
  • I have testcase names, Scenarionames and its runflag in a .xlsx file and the number is equal to the number of entry i made in the .xlsx file before the execution. As i am using hashtable as parameter in @ Test mathod and my DP also returns a hash table, so i would not be worried about the number of parameter i need to pass in my @test method which uses the DP. Please refer the @ Test method in my question, where i have wrote what operation i am going to do in that test method. – user3660045 Oct 13 '15 at 09:25
  • I am sorry, i really tried hard, but I find it very difficult to understand your problem. I don't know why would you like to call DP from your test. DP is supposed to _inject_ parameters, so make sure that DP provides **all** necessary arguments to the test. – botchniaque Oct 13 '15 at 13:54
  • This I am doing as part of my hybridframework. So in my driver script i am reading an .xlsx file to get scenario name and testcase name which i want ot execute. So i want some logic to pass those values to DP to get the test data which i will be using to execute the test stpes. So i will have my driver script and DP which I will be using to execute all the test cases. – user3660045 Oct 13 '15 at 15:20
0

You can do using following way:

Approach 1: 1. Create class variables for Path, SheetName, TableName. 2. initialize these variables using constructor 3. Direct call these variables into your data provider function.

Approach 2: 1. Setup Path, SheetName, TableName in system property env using System class or you can do using maven, gradle build tool. 2. Get these property values in your data provider class using System getProperty method.

Sadik Ali
  • 1,129
  • 10
  • 26
  • Approach 1 sounds good to me,however i am not figureout how i can implement that, it would be great if you can give one example. Actually I am reading one .xlsx file to get the path, sheetname and data tablename(TC name) and for each @test method i want to call the DP with those parameters to get the data. So if its posible please give one example around this. – user3660045 Oct 13 '15 at 14:38