有没有办法使用反射Java一次调用一组方法?

时间:2015-04-18 13:03:41

标签: java selenium

基本上我使用DataProvider和一个基本方法运行selenium测试,这是调用所有测试的起点。测试方法属于不同的类,我希望能够立即调用所有方法,因为我是并行运行测试而不是一次运行(串行)。有没有办法做到这一点?以下是主要代码:

 @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);

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

谢谢

2 个答案:

答案 0 :(得分:0)

试试这个:

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

    driverUtils.setUp(browser);

    Class[] classes = { UseCases.class };
    for (final Class clazz : classes) {

        Runnable runnableClass = new Runnable() {

            @Override
            public void run() {
                Method[] methods = clazz.getDeclaredMethods();
                for (final Method method : methods) {
                    Runnable runnableMethod = new Runnable() {

                        @Override
                        public void run() {
                            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();
                            }
                        }
                    };
                    Thread threadMethod = new Thread(runnableMethod);
                    System.out.println("Start Thread Class: " + clazz + " Method: " + method);
                    threadMethod.start();
                }
            }
        };
        Thread threadClass = new Thread(runnableClass);
        System.out.println("Start Thread Class: " + clazz);
        threadClass.start();
    }
}

在这个示例中,我使用两个线程并行运行Classes和方法。

答案 1 :(得分:0)

您还可以使用java.util.concurrent包的CountDownLatch类。创建一个Threads池,并使用CountDownLatch对象立即启动它们。