zoukankan      html  css  js  c++  java
  • MeterSphere使用Beanshell后置脚本获取Json响应数据作为下一个接口参数

    MeterSphere使用后置脚本Beanshell获取Json中的数据(MeterShpere的beanshell可参考jmeter)

    前提:

    因新增用户需要用到角色id,但列表中的角色很多,需要查到对应的角色名称获得对应的角色id

    解决方法:通过Beanshell获取json数据,并比对角色名称获得角色id

    该查询角色请求接口返回的响应体:

    {
        "code": 0,
        "data": {
            "totalCount": 23,
            "pageSize": 10,
            "totalPage": 3,
            "currPage": 1,
            "list": [
                {
                    "roleId": "1386932755329212418",
                    "roleName": "rolename68683436",
                    "roleDescribe": "角色描述",
                    "createTime": 1619505511000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386933252324876289",
                    "roleName": "rolename12237116",
                    "roleDescribe": "角色描述",
                    "createTime": 1619505630000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386937586399862786",
                    "roleName": "rolename30616863",
                    "roleDescribe": "角色描述",
                    "createTime": 1619506663000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386937804906323970",
                    "roleName": "rolename59824176",
                    "roleDescribe": "角色描述",
                    "createTime": 1619506715000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386938077972291586",
                    "roleName": "rolename38355327",
                    "roleDescribe": "角色描述",
                    "createTime": 1619506781000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386938538154549249",
                    "roleName": "rolename71155716",
                    "roleDescribe": "角色描述",
                    "createTime": 1619506890000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386938671428558850",
                    "roleName": "rolename76969316",
                    "roleDescribe": "角色描述",
                    "createTime": 1619506922000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386938740525522945",
                    "roleName": "rolename52641082",
                    "roleDescribe": "角色描述",
                    "createTime": 1619506938000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386938875733106689",
                    "roleName": "rolename62736553",
                    "roleDescribe": "角色描述",
                    "createTime": 1619506971000,
                    "permissionList": null,
                    "permissionIdList": null
                },
                {
                    "roleId": "1386939085746102274",
                    "roleName": "rolename79351680",
                    "roleDescribe": "角色描述",
                    "createTime": 1619507021000,
                    "permissionList": null,
                    "permissionIdList": null
                }
            ]
        }

    后置脚本获取json

    操作步骤:

    1、beanshell脚本中获得其他变量:

    String add_rolename=vars.get("rolename");

    2、通过beanshell后置脚本找到对应角色名称,例如:rolename的角色id,并输出变量给其他接口使用

    vars.put("roleId", roleId);

    //导入json包
    import org.json.*;
    
    //其他变量赋值到beanshell
    String add_rolename=vars.get("rolename");
    //log.info("--------------" + add_rolename);
    
    //获取接口响应数据
    String response_data = prev.getResponseDataAsString();
    //log.info(response_data);
    //转换为Object对象类型
    JSONObject data_obj = new JSONObject(response_data);
    //log.info("--------------" + data_obj.toString());
    //获取数据
    String StrData = data_obj.get("data").toString();
    //log.info("--------------" + StrData.toString());
    
    //转换为Object对象类型
    JSONObject data2_obj = new JSONObject(StrData);
    //获取list数据
    JSONArray list_array = data2_obj.getJSONArray("list");
    log.info("--------------" + list_array.toString());
    int len = list_array.length();
    String strlen = Integer.toString(len);
    for(int i=0;i < len;i++)
    {
        JSONObject jsonTemp = (JSONObject)list_array.getJSONObject(i);
        //log.info("--------------" + jsonTemp.getString("roleName"));
        String roleName=jsonTemp.getString("roleName");
        if (roleName.equals(add_rolename)){
            String roleId=jsonTemp.getString("roleId");
            log.info("--------------" + roleId);
            vars.put("roleId", roleId);
            
        }
    }

    控制台输出结果:

    获得的角色id:roleId在其他接口中的使用:

    {
        "trueName": "${true_name}",
        "roleIdList": [
            "${roleId}"
            ],
        "organizationCodeArr": [
            "0"
        ],
        "organizationCode": "0",
        "loginPassword": "${userPwd}",
        "mobile": "${mobile}",
        "userName": "${userName}",
        "userDescribe": "用户描述",
        "email": "${email}"
    }

    原文:https://www.icode9.com/content-3-946886.html

  • 相关阅读:
    设计模式---单例模式
    Linux介绍
    集合---Map
    集合---Collection
    JDK1.7中HashMap底层实现原理(转)
    算法面试题-用单向链表表示十进制整数,求两个正整数之和。1234+34=1268
    idea 常用插件
    ehcache配置log4j日志,或与spirng cache整合用注解形式,打印注解日志
    spring cache @CacheEvict 清除多个key
    tomcat源码及其下载版本
  • 原文地址:https://www.cnblogs.com/wangjunjiehome/p/15007532.html
Copyright © 2011-2022 走看看