51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2525|回复: 0

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

[复制链接]
  • TA的每日心情
    奋斗
    2018-8-27 15:56
  • 签到天数: 322 天

    连续签到: 1 天

    [LV.8]测试军长

    发表于 2016-7-8 17:19:53 | 显示全部楼层 |阅读模式
    自动化的框架设计中,少不了跟自动构建工具jenkins的配合,谈到jenkins,很多人首先想到的是jenkins 提供的API接口来控制构建。其实自动化框架控制jenkins自动构建有更简单的办法,如果用jenkins API的话,有点感觉杀鸡用了牛刀。下面我们就来看看怎么更简单的控制jenkins构建。
      首先,jenkins页面的构建按钮点击,都是一个get请求,知道了这一点我们就好办了。


    第二步,就是用JAVA代码处理发送请求
    1. /**
    2.      * 向指定URL发送GET方法的请求
    3.      * 发起构建请求
    4.      * @param buildname
    5.      *            jenkins中的构建名称
    6.      * @param param
    7.      *            延迟多少秒进行构建
    8.      * @return URL 所代表远程资源的响应结果
    9.      */
    10.     public static String sendBuilding(String buildname, int param) {
    11.         String result = "";
    12.         BufferedReader in = null;
    13.         try {
    14.                 final String jenkinsurl = "http://xx.xx.xx.xx:18080/jenkins/job/";
    15.                
    16.             String urlString = jenkinsurl+buildname + "/build?delay="+param+"sec";
    17.                      
    18.             URL realUrl = new URL(urlString);
    19.             // 打开和URL之间的连接
    20.             URLConnection connection = realUrl.openConnection();
    21.             // 设置通用的请求属性
    22.             connection.setRequestProperty("accept", "*/*");
    23.             connection.setRequestProperty("connection", "Keep-Alive");
    24.             connection.setRequestProperty("user-agent",
    25.                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    26.             // 建立实际的连接
    27.             connection.connect();
    28.             // 获取所有响应头字段
    29.             Map<String, List<String>> map = connection.getHeaderFields();
    30.             // 遍历所有的响应头字段
    31.             for (String key : map.keySet()) {
    32.                 JavaBaseTest.LogUtil.APP.info(key + "--->" + map.get(key));
    33.             }
    34.             // 定义 BufferedReader输入流来读取URL的响应
    35.             in = new BufferedReader(new InputStreamReader(
    36.                     connection.getInputStream()));
    37.             String line;
    38.             while ((line = in.readLine()) != null) {
    39.                 result += line;
    40.             }
    41.         } catch (Exception e) {
    42.             JavaBaseTest.LogUtil.APP.error("发送构建请求(GET)时出现异常!", e);
    43.             e.printStackTrace();
    44.         }
    45.         // 使用finally块来关闭输入流
    46.         finally {
    47.             try {
    48.                 if (in != null) {
    49.                     in.close();
    50.                 }
    51.             } catch (Exception e2) {
    52.                 e2.printStackTrace();
    53.             }
    54.         }
    55.         return result;
    56.     }
    复制代码


    第三步,发送完GET请求,我们就要知道当前构建有没有成功呢,请往下看
    1. /**
    2.      * 向指定URL发送GET方法的请求
    3.      * 判断最后一次构建有没有成功
    4.      * @param buildname
    5.      *            jenkins中的构建名称
    6.      * @return URL 所代表远程资源的响应结果
    7.      *
    8.      * alt="Success"  alt="In progress"  alt="Failed"
    9.      */
    10.     public static String BuildingResult(String buildname) {
    11.         String result = "";
    12.         BufferedReader in = null;
    13.         try {
    14.                 final String jenkinsurl = "http://xx.xx.xx.xx:18080/jenkins/job/";
    15.                
    16.             String urlString = jenkinsurl+buildname + "/lastBuild/";
    17.                      
    18.             URL realUrl = new URL(urlString);
    19.             // 打开和URL之间的连接
    20.             URLConnection connection = realUrl.openConnection();
    21.             // 设置通用的请求属性
    22.             connection.setRequestProperty("accept", "*/*");
    23.             connection.setRequestProperty("connection", "Keep-Alive");
    24.             connection.setRequestProperty("user-agent",
    25.                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    26.             // 建立实际的连接
    27.             connection.connect();
    28.             // 获取所有响应头字段
    29.             Map<String, List<String>> map = connection.getHeaderFields();
    30.             // 遍历所有的响应头字段
    31.             for (String key : map.keySet()) {
    32.                 JavaBaseTest.LogUtil.APP.info(key + "--->" + map.get(key));
    33.             }
    34.             // 定义 BufferedReader输入流来读取URL的响应
    35.             in = new BufferedReader(new InputStreamReader(
    36.                     connection.getInputStream()));
    37.             String line;
    38.             while ((line = in.readLine()) != null) {
    39.                 result += line;
    40.             }
    41.         } catch (Exception e) {
    42.             JavaBaseTest.LogUtil.APP.error("发送构建请求(GET)时出现异常!", e);
    43.             e.printStackTrace();
    44.         }
    45.         // 使用finally块来关闭输入流
    46.         finally {
    47.             try {
    48.                 if (in != null) {
    49.                     in.close();
    50.                 }
    51.             } catch (Exception e2) {
    52.                 e2.printStackTrace();
    53.             }
    54.         }
    55.         return result;
    56.     }
    复制代码



    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 支持分享~

    查看全部评分

    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-19 09:40 , Processed in 0.077385 second(s), 28 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表