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


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

    1.什么是BeanShell ?

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

    下面来介绍如何使用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的值是否与预期一致,需要怎么做呢?

     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响应数据:

     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数据:

     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();

     

  • 相关阅读:
    codevs 1164 统计数字
    codevs 2597 团伙
    codevs 1472 体检
    Openjudge 1.13-21:最大质因子序列
    Openjudge 1.13-23:区间内的真素数
    codevs 1388 砍树
    codevs 1536 海战
    codevs 3110 二叉堆练习3
    codevs 2879 堆的判断
    Openjudge 1.13.37:乒乓球
  • 原文地址:https://www.cnblogs.com/lwjnicole/p/9383776.html
Copyright © 2020-2023  润新知