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 を実行するときに、データのファイル パス、シート名、テーブル名を動的に渡す必要があります。これにより、同じデータ プロバイダーを使用でき、作業するデータの異なるセットが得られます。

注: データ プロバイダーは、Excel で指定されたデータのハッシュ テーブルの形式で、テーブル名を含む表形式で返します。したがって、ワークシート パス、シート名、およびテーブル名がデータ プロバイダーに渡されると、DP はそのテーブルを読み取り、データ テーブル全体をハッシュ テーブルの形式で返します。

4

3 に答える 3

0

「 testng.xml のパラメーター」で説明されているように、XML を介してパラメーターを提供することをお勧めします。

于 2015-10-12T12:23:17.223 に答える
0

次の方法を使用して行うことができます。

アプローチ 1: 1. Path、SheetName、TableName のクラス変数を作成します。2. コンストラクターを使用してこれらの変数を初期化します。 3. これらの変数をデータ プロバイダー関数に直接呼び出します。

アプローチ 2: 1. System クラスを使用してシステム プロパティ env に Path、SheetName、TableName を設定するか、maven、gradle ビルド ツールを使用して行うことができます。2. System getProperty メソッドを使用して、データ プロバイダー クラスでこれらのプロパティ値を取得します。

于 2015-10-13T09:30:34.263 に答える
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 に答える