【问题标题】:Select random value from the response json with a validation从带有验证的响应 json 中选择随机值
【发布时间】:2021-01-25 15:41:35
【问题描述】:

当我向 api 端点发送请求时,我的响应中会出现以下 json。

{
    "entities": {
        "practitioners": {
            "f1d26a4b-c489-493d-bccf-7b9c8b92ecac": {
                "fullAvatarUrl": null,
                "id": "f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "1",
                "type": "Dentist",
                "isActive": true,
                "isHidden": false
            },
            "ee87642d-c9a6-4a9d-b99a-a96501f27a7b": {
                "fullAvatarUrl": null,
                "id": "ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "2",
                "type": "Hygienist",
                "isActive": true,
                "isHidden": false
            },
            "d0aeb9eb-f267-45ad-8cdf-eada1155c274": {
                "fullAvatarUrl": null,
                "id": "d0aeb9eb-f267-45ad-8cdf-eada1155c274",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "3",
                "type": "Dentist",
                "isActive": true,
                "isHidden": false
            },
            "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441": {
                "fullAvatarUrl": null,
                "id": "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441",
                "accountId": "ef757dba-f0d5-4464-a338-4a810e02bf47",
                "pmsId": "4",
                "type": "Hygienist",
                "isActive": true,
                "isHidden": false
            }
        }
    },
    "result": [
        "f1d26a4b-c489-493d-bccf-7b9c8b92ecac",
        "ee87642d-c9a6-4a9d-b99a-a96501f27a7b",
        "d0aeb9eb-f267-45ad-8cdf-eada1155c274",
        "2f641e8e-c5d6-4fdf-8fbe-f99fe837f441"
    ]
}

问题陈述:我想从以上 4 个随机值中选择任何一个(想选择从业者 id 并且位于从业者下的第一个元素),验证为 isActive应该是trueisHidden 应该是false

我尝试使用 JSON 提取器,使用表达式 $.entities.practitioners 和匹配号 0

但它不是选择任何一个,而是选择所有。

【问题讨论】:

    标签: jmeter


    【解决方案1】:
    1. JSR223 PostProcessor 添加为返回此 JSON 的请求的子项

    2. 将以下代码放入“脚本”区域:

      def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
      def ids = []
      json.entities.practitioners.each { practitioner ->
          if (practitioner.value.isActive && !practitioner.value.isHidden) {
              ids.add(practitioner.key)
          }
      }
      
      def randomId = ids.get(org.apache.commons.lang3.RandomUtils.nextInt(0, ids.size()))
      vars.put('randomId', randomId)
      
    3. 就是这样,您应该可以在需要的地方以${randomId} 访问随机ID。

    更多信息:

    【讨论】:

      【解决方案2】:

      我想这种方法可以帮助你,

      在您的 http 请求中添加 JSR223 PostProcessor,然后将以下 groovy 脚本添加到 PostProcessor

      Note: this is only to select the random PractitionersId 用于验证只需使用类似的逻辑。

      import groovy.json.*
      
      def response = prev.responseDataAsString ;
      def json = new JsonSlurper().parseText(response) ;
      def sizeResultPractitioners = json.result.size();
      Random rnd = new Random()
      def randomResultPractitioners = rnd.nextInt(sizeResultPractitioners);
      log.info("---------->"+randomResultPractitioners);
      log.info("---------->"+json.result[randomResultPractitioners]);
      

      【讨论】:

      • 感谢 Jyoti,这真的很有帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      相关资源
      最近更新 更多