如何将所有参数传递给每个方法并一次运行每个参数

时间:2015-04-18 01:18:59

标签: java selenium testng

好的,我正在使用DataProvider将数据传递给单个@Test方法。该数据来自外部数据库。在我的@Test方法看起来像这样之前:

@Test(enabled = true, dataProvider = DataProviderUtils.REGRESSION, dataProviderClass = DataProviderUtils.class) 
public void use_cases(String env, String browser, String title, String id, String orderType, String productType, String isFreeShipping, String isTaxState,
                      String billingCountry, String shippingCountry, String promoType, String isPromoFree,
                      String isPromoCode, String noOfPromoCodes, String email, Object[] customSteps){



    driverUtils.setUp(browser);
    String[] testType = title.split(" ");
    switch(testType[2].trim()){
        case "Standard_Order":
            useCases.standardOrder(id, env, productType, isFreeShipping, isTaxState, billingCountry, customSteps);
            break;

        case "Warranty_Order":
            useCases.warrantyOrder(id, env, productType, isFreeShipping, isTaxState, billingCountry, customSteps);
            break;

如果有一些测试可行,但是当它达到100时,我会有一个额外的长开关语句,根本不可维护。

所以我决定使用反射来处理它,然后我的代码就是:

@Test(enabled = true, dataProvider = DataProviderUtils.REGRESSION, dataProviderClass = DataProviderUtils.class)
public void test(String env, String browser, String title, String id, String orderType, String productType, String isFreeShipping, String isTaxState,
                      String billingCountry, String shippingCountry, String promoType, String isPromoFree,
                      String isPromoCode, String noOfPromoCodes, String email, Object[] customSteps){

    driverUtils.setUp(browser);
    String[] testType = title.split(" ");

    Class[] classes = {UseCases.class};
    for(Class clazz : classes){
        Method[] methods = clazz.getDeclaredMethods();
        for(Method method : methods){
            try {
                method = clazz.getMethod(method.getName(), new Class[]{String.class, String.class, String.class, String.class, String.class, String.class, Object[].class});
                Object invoke = method.invoke(new UseCases(), id, env, productType, isFreeShipping, isTaxState, billingCountry, customSteps);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

这是我的UseCases类:

public class UseCases {

OrderData orderData = new OrderData();

static String testRailURL;
static String localURL;


//New Standard Order Creation
public void standardOrder(String id, String env, String productType, String isFreeShipping, String taxState, String billingCountry, Object[] customSteps){

    testRailURL = TestRailUtil.getEnvironment(env);
    localURL = DriverUtils._targetURL;

    DriverFactory df = new DriverFactory(DriverUtils.getDriver());

    if(TestRailUtil.TEST_GET_DATA.equalsIgnoreCase("local")){
        df.navigateToURL(localURL);
    }else {
        df.navigateToURL(testRailURL);
    }
    df.loginPage.login();
    df.homePage.verifyAdminHomePageDisplay(id, customSteps);
    df.serviceHomePage.navigateToServiceHomePage();
    df.serviceHomePage.beginOrderCreation(id, productType, TestRailEnum.STANDARD_ORDER.getId(), billingCountry, customSteps);
    df.editCreatePage.verifyOrderEditPageDisplay(id, customSteps);
    df.editCreatePage.createOrder(id, productType, TestRailEnum.STANDARD_ORDER.getId(), false, isFreeShipping, taxState, customSteps);
    df.editCreatePage.review();
    df.orderConfirmChangesPage.verifyOrderConfirmChangesPageDisplay(id, customSteps);
    df.orderConfirmChangesPage.verifyConfirmChangesPageElementsDisplay(id, customSteps);
    //df.orderConfirmChangesPage.verifyTaxAndTotal(orderType, isFreeShipping);
    if (testRailURL.contains("qa") || DriverUtils._targetURL.contains("qa")){
        df.orderConfirmChangesPage.confirm();
    }
}

//New Warranty Order Creation
public void warrantyOrder(String id, String env, String productType, String isFreeShipping, String taxState, String billingCountry, Object[] customSteps){

    testRailURL = TestRailUtil.getEnvironment(env);
    localURL = DriverUtils._targetURL;

    DriverFactory df = new DriverFactory(DriverUtils.getDriver());

    if(TestRailUtil.TEST_GET_DATA.equalsIgnoreCase("local")){
        df.navigateToURL(localURL);
    }else {
        df.navigateToURL(testRailURL);
    }
    df.loginPage.login();
    df.homePage.verifyAdminHomePageDisplay(id, customSteps);
    df.serviceHomePage.navigateToServiceHomePage();
    df.serviceHomePage.beginOrderCreation(id, productType, TestRailEnum.Warranty_Order.getId(), billingCountry, customSteps);
    df.editCreatePage.verifyOrderEditPageDisplay(id, customSteps);
    df.editCreatePage.createOrder(id, productType, TestRailEnum.Warranty_Order.getId(), false, isFreeShipping, taxState, customSteps);
    df.editCreatePage.review();
    df.orderConfirmChangesPage.verifyOrderConfirmChangesPageDisplay(id, customSteps);
    df.orderConfirmChangesPage.verifyConfirmChangesPageElementsDisplay(id, customSteps);
    //df.orderConfirmChangesPage.verifyTaxAndTotal(orderType, isFreeShipping);
    if (testRailURL.contains("qa") || DriverUtils._targetURL.contains("qa")){
        df.orderConfirmChangesPage.confirm();
    }
}

}

问题是我并行运行测试,当我使用反射时,它会在第二个方法之前调用第一个方法'n'次。因此,如果我有两个单独的测试(标准订单测试和保修订单测试),数据将正确地从DataProvider传入,但两个测试的数据将被传递到UseCases类中的第一个方法,因为循环的方式是结构

我的问题是,有没有办法传递数据进入正确方法并同时运行(并行)的信息,而不是循环遍历每个方法,每个方法多次运行数据?

由于

1 个答案:

答案 0 :(得分:0)

我建议您使用典型的测试模式:

  1. 方案,如“标准订单测试”和“保修订单测试”是使用@Test注释的测试方法。
  2. 数据提供程序提供测试参数,例如env。不需要重新编译,因为场景在3种环境中很常见,只有运行时参数不同。
  3. 您可以将方案分组为测试套装,以获得少量固定方案集。
  4. 我认为典型的模式简单而有力。如果还不够,那就提出问题。

    您的第一个解决方案,使用方法use_cases作为调度程序看起来很奇怪,因为通常TestNG是调度程序。你反思的第二个解决方案让我看起来很奇怪。

相关问题