jmeter之beanshell断言---数据处理

在作接口测试时,对响应数据的校验是很是重要的部分;在使用Jmeter进行接口测试时,有多种respone校验方式,好比响应断言、BeanShell断言等等,BeanShell断言能够自定义断言,自由灵活的用脚本实现断言。java

1.什么是BeanShell ?shell

小型嵌入式Java源代码解释器,具备对象脚本语言特性,可以动态地执行标准JAVA语法 
运行其内部的脚本处理Java应用程序,还能够在运行过程当中动态执行你java应用程序执行java代码,由于BeanShell是用java写的,运行在同一个虚拟机的应用程序,所以能够自由地引用对象脚本并返回结果。json

 

下面来介绍如何使用beanshell来进行断言和数据处理,假如咱们有以下的response数据:数组

1 {
2     "message": "不能发送小于当前时间点的定时任务",
3     "statusCode": 200
4 }

(1).咱们使用JSONObject对象来获取json数据,首先须要下载org.json的jar包,而后在测试计划中导入该jar包,并在jmeter的lib目录下放入该jar包,下面验证statusCode的值是否等于200测试

 1 import org.json.*;
 2 
 3 //获取上一个请求的返回
 4 String jsonString = prev.getResponseDataAsString();
 5 JSONObject responseJson = new JSONObject(jsonString);
 6 
 7 //判断返回值是否和预期一致
 8 if (responseJson.getInt("statusCode") != 200) {
 9     //把断言失败置为真,即用例失败,并在结果树中显示FailureMessage
10     Failure = true;
11     FailureMessage = "statusCode的返回值有误";
12 }

(2).若是要验证respone中message的值是否与预期一致,须要怎么作呢?spa

 1 import org.json.*;
 2 
 3 //获取上一个请求的返回
 4 String jsonString = prev.getResponseDataAsString();
 5 JSONObject responseJson = new JSONObject(jsonString);
 6 
 7 String fbpcontent = responseJson.getString("message");
 8 if (!fbpcontent.equals("不能发送小于当前时间点的定时任务")) {
 9     //把断言失败置为真,即用例失败,并在结果树中显示FailureMessage
10     Failure = true;
11     FailureMessage = "message与实际值不一致";
12 }

 

假如咱们有以下的response响应数据:code

 1 {
 2     "statusCode": 200,
 3     "data": [
 4         {
 5             "i": "50356",
 6             "n": "项目一",
 7             "v": "2.0",
 8             "iconUrl": "",
 9         },
10         {
11             "i": "45280",
12             "n": "项目二",
13             "v": "3.0",
14             "iconUrl": "",
15         },
16         {
17             "i": "14656",
18             "n": "项目三",
19             "v": "2.6",
20             "iconUrl": "",
21         },
22         {
23             "i": "66213",
24             "n": "项目四",
25             "v": "5.0",
26             "iconUrl": "",
27         }
28     ]
29 }

(3).咱们须要解析数组data的值,如何去解析呢?对象

 1 import org.json.*;
 2 import java.util.Arrays;
 3 
 4 //获取上一个请求的返回
 5 String jsonContent = prev.getResponseDataAsString();  
 6 
 7 JSONObject response = new JSONObject(jsonContent);  
 8 JSONArray groups = response.getJSONArray("data");  
 9 String strData= groups.toString(); 
10 log.info(strData)

 

如今有更加复杂格式的respone数据:blog

 1 {  
 2   "priorityGroups": {  
 3     "proId": 1234,  
 4     "name": "项目一",  
 5     "groups": [  
 6       {  
 7         "id": "50356",  
 8         "items": [  
 9           {  
10             "proId": 1360,  
11             "n": "PC端",  
12             "index": 1  
13           },  
14           {  
15             "proId": 1361,  
16             "n": "iOS端",  
17             "index": 2  
18           },  
19           {  
20             "proId": 1362,  
21             "n": "安卓端",  
22             "index": 4  
23           }  
24         ]  
25       }  
26     ]  
27   },  
28   "promotion": {  
29     "proId": 1364,  
30     "cusId": 84,  
31     "name": "项目二",  
32     "from": 1470821215,  
33     "to": 1470907615,   
34     "status": 1,  
35     "objectId": 1069,  
36     "createBy": 394,  
37     "eff": 1470821215000,  
38     "createTime": 1470821155000  
39   }  
40 }

(4).咱们须要解析groups中的数据,须要怎么实现呢?接口

 

1 import org.json.JSONArray;  
2 import org.json.JSONException;  
3 import org.json.JSONObject;  
4 
5 String jsonContent = prev.getResponseDataAsString();  
6 
7 JSONObject response = new JSONObject(jsonContent);  
8 JSONArray groups = response.getJSONObject("priorityGroups").getJSONArray("groups");  
9 String strGroups = groups.toString();
相关文章
相关标签/搜索