首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java测试框架如何执行groovy脚本文件

java测试框架如何执行groovy脚本文件

作者头像
FunTester
发布2019-10-29 16:32:23
9330
发布2019-10-29 16:32:23
举报
文章被收录于专栏:FunTesterFunTester

本人在写基于httpclient的测试框架时,用到了groovy脚本作为测试用例的脚本语言,自然就需要java执行上传的测试脚本,在看过实例之后,自己进行了封装,总体来说跟java反射执行java方法类似。但又有一些不兼容的情况,部分已经写了博客做记录了,以后会陆续更新。分享代码,供大家参考。

其中一个比较大的区别时,在获取groovy类加载器的时候必须是非静态的。

package com.fission.source.source;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import org.codehaus.groovy.tools.GroovyClass;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ExcuteGroovy extends SourceCode {
    private String path;
    private String name;
    private String[] args;//参数,暂时不支持参数
    private List<String> files = new ArrayList<>();//所有脚本
    private GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader());
    private GroovyObject groovyObject;//groovy对象
    private Class<?> groovyClass;//执行类


    public ExcuteGroovy(String path, String name) {
        this.path = path;
        this.name = name;
        getGroovyObject();
    }

    /**
     * 执行一个类的所有方法
     */
    public void excuteAllMethod(String path) {
        getAllFile(path);
        if (files == null)
            return;
        files.forEach((file) -> new ExcuteGroovy(file, "").excuteMethodByPath());
    }

    /**
     * 执行某个类的方法,需要做过滤
     *
     * @return
     */
    public void excuteMethodByName() {
        if (new File(path).isDirectory()) {
            output("文件类型错误!");
        }
        try {
            groovyObject.invokeMethod(name, null);
        } catch (Exception e) {
            output("执行" + name + "失败!", e);
        }

    }

    /**
     * 根据path执行相关方法
     */
    public void excuteMethodByPath() {
        Method[] methods = groovyClass.getDeclaredMethods();//获取类方法,此处方法比较多,需过滤
        for (Method method : methods) {
            String methodName = method.getName();
            if (methodName.contains("test") || methodName.equals("main")) {
                groovyObject.invokeMethod(methodName, null);
            }
        }
    }

    /**
     * 获取groovy对象和执行类
     */
    public void getGroovyObject() {
        try {
            groovyClass = loader.parseClass(new File(path));//创建类
        } catch (IOException e) {
            output(e);
        }
        try {
            groovyObject = (GroovyObject) groovyClass.newInstance();//创建类对象
        } catch (InstantiationException e) {
            output(e);
        } catch (IllegalAccessException e) {
            output(e);
        }
    }

    /**
     * 获取文件下所有的groovy脚本,不支持递归查询
     *
     * @return
     */
    public List<String> getAllFile(String path) {
        File file = new File(path);
        if (file.isFile()) {
            files.add(path);
            return files;
        }
        File[] files1 = file.listFiles();
        int size = files1.length;
        for (int i = 0; i < size; i++) {
            String name = files1[i].getAbsolutePath();
            if (name.endsWith(".groovy"))
                files.add(name);
        }
        return files;
    }
}

在获取groovy脚本的时候,并未用到递归,以后随着需求增加应该会增加递归。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FunTester 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档