• httpclient + TestNG 接口自动测试 第三章


    此后就是数据准备与逻辑实现,就不在赘述了

    此次自动化脚本并未太多用到TestNG,仅仅用于他调试起来方便;

    本章记录一些用到的辅助方法:

    1.将返回值打印出来的get请求,用于测试或查看接口返回内容

    public static void get(HashMap<String, String> params, String HOST, String PATH) {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                // 创建httpget请求.  
                HttpGet httpget = new HttpGet(getUrl(getFormparams(params), HOST, PATH));
                System.out.println("executing request " + httpget.getURI());
                // 执行get请求 
                CloseableHttpResponse response = httpclient.execute(httpget);
                try {
                    // 将响应放入entity载体
                    HttpEntity entity = response.getEntity();
                    System.out.println("--------------------------------------");
                    // 打印返回状态栏  
                    System.out.println(response.getStatusLine());
                    if (entity != null) {
                        // 打印返回内容长度  
                        System.out.println("Response content length: " + entity.getContentLength());
                        // 打印返回内容�  
                        System.out.println("Response content: " + EntityUtils.toString(entity));
                    }
                    System.out.println("------------------------------------");
                } finally {
                    response.close();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 断开连接
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    2.去掉长数组中短数组含有的元素

    public static String[] unlockedSeatArray(String[] longerarray, String[] shorterarray) {
        LinkedList<String> list = new LinkedList<>();
        for(String str : longerarray) {
            list.add(str);
        }
        for (String str : shorterarray) {
            if(list.contains(str)) {
                list.remove(str);
            }
        }
        String[] result = {};
        return list.toArray(result);
    }

    3.数组中每前n个值合并,并形成一个新的数组

    public static String[] seatArray(String[] targetArray, int index) {
        String[] seatArray = null;
        int len = targetArray.length;
        int len2 = 0;
        int j = 0; //座位数循环变量
        //计算数组的行长度
         if(0==len%index){   
         len2 = len/index; 
        }else{
         len2 = (int)(len/index) + 1 ;
         }
         //初始化数组
        seatArray = new String[len2];
        len2 = 0; j = 0;
        for(int i=0; i<len; i++) {
            if(index == j) {
                j = 0;
                seatArray[len2] = seatArray[len2].substring(4, seatArray[len2].length()-1);
                len2++;
            }
            seatArray[len2] = seatArray[len2] + targetArray[i] + ",";
            j++;
        }
        seatArray[len2] = seatArray[len2].substring(4, seatArray[len2].length()-1); //处理最后一个数据
        return seatArray;
    }

    4.简单生成日志方法

    public static void writefile(String response, String path) {
            try {
                SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String TimeString = time.format(new java.util.Date());
                
                File file = new File(path);
                if (!file.exists()) {file.createNewFile();}
                FileWriter fw = new FileWriter(file, true);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.append(response);
                bw.write("
    ");
                bw.write("---------" + "可爱的分割线" + "----------" + TimeString);
                bw.write("
    ");
                bw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }

    差不多一些用到的方法记录于此

  • 相关阅读:
    ASP.Net Cms
    GetHashCode函数所存在的陷阱
    NH的系统架构
    Icomparable<T> 和 Icomparaer<T> 实现顺序关系
    《创业法典》:用流程图告诉你如何创业
    创业之前,网站先行
    杨卫华:新浪微博的架构发展历程
    技术开发人员为什么会无奈苦逼?
    使用面向对象的、完整的单点登录功能
    SilverLight之我见
  • 原文地址:https://www.cnblogs.com/mayibanjiah/p/4211013.html
Copyright © 2020-2023  润新知