本人在做接口自动化时候,因为服务器不稳定造成可能的用例失败,但这个失败表象只是在获取响应实体的json对象时为空,在后期排查问题时可能造成困扰,所以特意加了一个获取响应失败的通知,目的就是即使了解到服务器异常。暂时用的是免费的alertover,用了很久,简单可靠是它的优点,后续会加入微信提醒。分享代码,供大家参考。
下面是获取响应实体的json对象的方法(可忽略某一些封装方法):
1 /**
2 * 获取响应实体
3 * <p>会自动设置cookie,但是需要各个项目再自行实现cookie管理</p>
4 * <p>该方法只会处理文本信息,对于文件处理可以调用两个过期的方法解决</p>
5 *
6 * @param request 请求对象
7 * @return 返回json类型的对象
8 */
9 public static JSONObject getHttpResponse(HttpRequestBase request) {
10 if (!isRightRequest(request)) return new JSONObject();
11 beforeRequest(request);
12 JSONObject res = new JSONObject();
13 RequestInfo requestInfo = new RequestInfo(request);
14 if (HEADER_KEY) output("===========request header===========", Arrays.asList(request.getAllHeaders()));
15 long start = Time.getTimeStamp();
16 try (CloseableHttpResponse response = ClientManage.httpsClient.execute(request)) {
17 long end = Time.getTimeStamp();
18 long elapsed_time = end - start;
19 if (HEADER_KEY) output("===========response header===========", Arrays.asList(response.getAllHeaders()));
20 int status = getStatus(response, res);
21 JSONObject setCookies = afterResponse(response);
22 String content = getContent(response);
23 int data_size = content.length();
24 res.putAll(getJsonResponse(content, setCookies));
25 int code = iBase == null ? -2 : iBase.checkCode(res, requestInfo);
26 if (!iBase.isRight(res))
27 new AlertOver("响应状态码错误:" + status, "状态码错误:" + status, requestInfo.getUrl(), requestInfo).sendSystemMessage();
28 MySqlTest.saveApiTestDate(requestInfo, data_size, elapsed_time, status, getMark(), code, LOCAL_IP, COMPUTER_USER_NAME);
29 } catch (Exception e) {
30 logger.warn("获取请求相应失败!", e);
31 if (!SysInit.isBlack(requestInfo.getHost()))
32 new AlertOver("接口请求失败", requestInfo.toString(), requestInfo.getUrl(), requestInfo).sendSystemMessage();
33 } finally {
34 HEADER_KEY = false;
35 if (!SysInit.isBlack(requestInfo.getHost())) {
36 if (requests.size() > 9) requests.removeFirst();
37 boolean add = requests.add(request);
38 }
39 }
40 return res;
41 }
下面是alertover类的代码,比较简单:
1package com.fun.utils.message;
2
3import com.fun.frame.httpclient.FanLibrary;
4import com.fun.base.bean.RequestInfo;
5import com.fun.base.interfaces.IMessage;
6import com.fun.db.mysql.MySqlTest;
7import com.fun.config.SysInit;
8import net.sf.json.JSONObject;
9import org.apache.http.client.methods.HttpPost;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13public class AlertOver extends FanLibrary implements IMessage {
14
15 private static Logger logger = LoggerFactory.getLogger(AlertOver.class);
16
17 String title;
18
19 String content;
20
21 String murl;
22
23 RequestInfo requestInfo;
24
25 private static String system = "s-7e93ec02-1308-480c-bc11-a7260c14";//系统异常
26
27 private static String function = "s-7e3b7ea5-b4b0-4479-a0e3-bce6c830";//功能异常
28
29 private static String business = "s-466a191a-cbb8-4164-b8be-9779bb88";//业务异常
30
31 private static String remind = "s-f49ac5bc-008b-4b11-890e-6715ef89";//提醒推送
32
33 private static String code = "s-490d0fc6-35cc-4430-9f87-09cdeb05";//程序异常
34
35 private static final String testGroup = "g-4eefc0ad-19af-4b1c-9d0b-ef87be15";
36
37 public AlertOver() {
38 this("test title", "test content!");
39 }
40
41 public AlertOver(String title, String content) {
42 this.title = title;
43 this.content = content + LINE + "发送源:" + COMPUTER_USER_NAME;
44 }
45
46 public AlertOver(String title, String content, String url) {
47 this(title, content);
48 this.murl = url;
49 }
50
51 public AlertOver(String title, String content, String url, RequestInfo requestInfo) {
52 this(title, content);
53 this.murl = url;
54 this.requestInfo = requestInfo;
55 }
56
57 /**
58 * 发送系统异常
59 */
60 public void sendSystemMessage() {
61 if (SysInit.isBlack(murl)) return;
62 sendMessage(system);
63 MySqlTest.saveAlertOverMessage(requestInfo, "system", title, LOCAL_IP, COMPUTER_USER_NAME);
64 ("发送系统错误提醒,title:{},ip:{},computer:{}", title, LOCAL_IP, COMPUTER_USER_NAME);
65 }
66
67 /**
68 * 发送功能异常
69 */
70 public void sendFunctionMessage() {
71 sendMessage(function);
72 }
73
74 /**
75 * 发送业务异常
76 */
77 public void sendBusinessMessage() {
78 sendMessage(business);
79 }
80
81 /**
82 * 发送程序异常
83 */
84 public void sendCodeMessage() {
85 sendMessage(code);
86 }
87
88 /**
89 * 提醒推送
90 */
91 public void sendRemindMessage() {
92 sendMessage(remind);
93 }
94
95 /**
96 * 发送消息
97 *
98 * @return
99 */
100 public void sendMessage(String source) {
101 if (SysInit.isBlack(murl)) return;
102 String url = "https:///v1/alert";
103 String receiver = testGroup;//测试组ID
104 JSONObject jsonObject = new JSONObject();// 新建json数组
105 jsonObject.put("frame", source);// 添加发送源id
106 jsonObject.put("receiver", receiver);// 添加接收组id
107 jsonObject.put("content", content);// 发送内容
108 jsonObject.put("title", title);// 发送标题
109 jsonObject.put("url", murl);// 发送标题
110 jsonObject.put("sound", "pianobar");// 发送声音
111 logger.debug("消息详情:{}", jsonObject.toString());
112 HttpPost httpPost = getHttpPost(url, jsonObject);
113 取消发送
114 getHttpResponse(httpPost);
115 }
116}
 
 
                     
            
        













 
                    

 
                 
                    