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("\r\n"); bw.write("---------" + "可爱的分割线" + "----------" + TimeString); bw.write("\r\n"); bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } }
差不多一些用到的方法记录于此
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。