自动化测试框架中的jenkins构建控制

上一篇 / 下一篇  2016-07-08 16:36:37 / 个人分类:开源自动化框架

  自动化的框架设计中,少不了跟自动构建工具jenkins的配合,谈到jenkins,很多人首先想到的是jenkins 提供的API接口来控制构建。其实自动化框架控制jenkins自动构建有更简单的办法,如果用jenkins API的话,有点感觉杀鸡用了牛刀。下面我们就来看看怎么更简单的控制jenkins构建。

  首先,jenkins页面的构建按钮点击,都是一个get请求,知道了这一点我们就好办了。


第二步,就是用JAVA代码处理发送请求
/**
     * 向指定URL发送GET方法的请求
     * 发起构建请求
     * @param buildname
     *            jenkins中的构建名称
     * @param param
     *            延迟多少秒进行构建
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendBuilding(String buildname, int param) {
        String result = "";
        BufferedReader in = null;
        try {
       final String jenkinsurl = "http://xx.xxx.xx.xx:18080/jenkins/job/";
       
            String urlString = jenkinsurl+buildname + "/build?delay="+param+"sec";
                      
            URL realUrl = new URL(urlString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                JavaBaseTest.LogUtil.APP.info(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            JavaBaseTest.LogUtil.APP.error("发送构建请求(GET)时出现异常!", e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }


第三步,发送完GET请求,我们就要知道当前构建有没有成功呢,请往下看
/**
     * 向指定URL发送GET方法的请求
     * 判断最后一次构建有没有成功
     * @param buildname
     *            jenkins中的构建名称
     * @return URL 所代表远程资源的响应结果
     * 
     * alt="Success"  alt="In progress"  alt="Failed"
     */
    public static String BuildingResult(String buildname) {
        String result = "";
        BufferedReader in = null;
        try {
       final String jenkinsurl = "http://xx.xxx.xx.xx:18080/jenkins/job/";
       
            String urlString = jenkinsurl+buildname + "/lastBuild/";
                      
            URL realUrl = new URL(urlString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                JavaBaseTest.LogUtil.APP.info(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            JavaBaseTest.LogUtil.APP.error("发送构建请求(GET)时出现异常!", e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

TAG:

 

评分:0

我来说两句

日历

« 2024-03-21  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 22580
  • 日志数: 13
  • 建立时间: 2011-08-15
  • 更新时间: 2017-11-22

RSS订阅

Open Toolbar