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 */
       }

我的问题:我想要一些逻辑,所以在执行@Test 时,我应该动态传递数据文件路径、SheetName、TableName,因此可以使用相同的数据提供程序,这将为我提供不同的数据集来处理。

注意:数据提供者将以哈希表的形式返回在 excel 中以表格格式指定的数据,并带有表名。因此,如果将工作表路径、工作表名称和表名传递给数据提供者,那么我的 DP 将读取该表并以哈希表的形式返回整个数据表。

4

3 回答 3

0

您可以使用以下方式:

方法 1: 1. 为 Path、SheetName、TableName 创建类变量。2. 使用构造函数初始化这些变量 3. 将这些变量直接调用到您的数据提供程序函数中。

方法 2: 1. 使用 System 类在系统属性环境中设置 Path、SheetName、TableName,或者您可以使用 maven、gradle 构建工具进行设置。2. 使用 System getProperty 方法在您的数据提供者类中获取这些属性值。

于 2015-10-13T09:30:34.263 回答
0

我建议您通过 XML 提供参数,如来自 testng.xml 的参数中的说明

于 2015-10-12T12:23:17.223 回答
0

如果您只是希望能够重用获取测试数据的复杂逻辑,为什么不将其移至辅助函数并在单独的数据提供者中传递您的参数呢?

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

}
于 2015-10-12T14:00:21.803 回答