在 Jmeter 中使用 Groovy 从 JSON 响应中提取随机值

问题描述 投票:0回答:3

我正在尝试将两个变量

Latitude
Longitude
设置为在 JSON 响应中找到的随机纬度/经度值。 JSON 响应包含一个
Locations
数组,其中包含纬度/经度值列表。每次运行此脚本时,我希望将变量设置为 Locations 数组中的随机纬度/经度值。 Groovy 脚本是一个嵌套在 POST HTTP 请求中的 JSR223 后处理器。下面我将发布一个示例响应和 Groovy 代码来实现此目的。

{
  "Result": {
    "Locations": [
      {
        "Latitude": 38.657,
        "Longitude": -120.377997
      },
      {
        "Latitude": 38.6566,
        "Longitude": -120.3791
      },
      {
        "Latitude": 38.658399,
        "Longitude": -120.3804
      },
      {
        "Latitude": 38.655499,
        "Longitude": -120.38496
      },
      {
        "Latitude": 38.654,
        "Longitude": -120.3819
      },
      {
        "Latitude": 38.6537,
        "Longitude": -120.3897
      },
      {
        "Latitude": 38.6544,
        "Longitude": -120.382604
      },
      {
        "Latitude": 38.655602,
        "Longitude": -122.386402
      }
    ]
  }
}

我使用的 Groovy 脚本是:

import groovy.json.*

//Parse JSON
def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parseText(prev.getResponseDataAsString())
def LocationsList = JsonOutput.toJson(response.Result.Locations)

//Get the size of Locations Array and Generate Random Number based on array size
Random random = new Random()
int max = LocationsList.size()
int randomNumber = random.nextInt(max)

//Get Latitude
def GetLatitude (number) {
    return LocationsList[number].Latitude
}
def Latitude = GetLatitude(randomNumber)

//Get Longitude
def GetLongitude (number) {
    return LocationsList[number].Longitude
}
def Longitude = GetLongitude(randomNumber)

//Set the env vars
vars.put("Latitude", Latitude)
vars.put("Longitude", Longitude)

我收到的回复很长,所以我将发布重要部分。

     Problem in JSR223 script, CurrentGPS
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: LocationsList for class: Script18
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-all-2.4.13.jar:2.4.13]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.13.jar:2.4.13]
    at javax.script.CompiledScript.eval(CompiledScript.java:92) ~[java.scripting:?]
json groovy jmeter load-testing
3个回答
3
投票

如果 JSON 路径表达式返回多个匹配项,您可以使用 JSON Extractor 获得随机匹配项,无需任何脚本。该组件自 JMeter 3.0 起可用,并且如果您提供

0
作为“匹配否”,它将从您的 JSON 路径表达式匹配的任何内容返回随机值。配置类似于:

文字表示:

  • 创建的变量名称:
    Latitude;Longitude
  • JSON 路径表达式:
    $..Latitude;$..Longitude
  • 比赛编号:
    0
  • 默认值:
    NA;NA

2
投票

您的脚本中有几个问题:

  • LocationsList
    错误地获取值。
  • LocationsList
    正在这两种方法中被访问,这不是范围。您可以使用
    Closure
    来代替。

这是固定脚本:

def json = new groovy.json.JsonSlurper().parseText(prev.getResponseDataAsString())
def locations = json.Result.Locations
def randomNumber = new Random().nextInt(locations.size())
def latitude = locations[randomNumber].Latitude
def longitude = locations[randomNumber].Longitude

您可以快速在线试用演示


0
投票

如果您想从随机数组项中获得匹配的经度和纬度,则使用 JSON Extractor 和

$..Latitude;$..Longitude
的解决方案将不起作用 - 该模式为您提供随机纬度和随机经度。

如果您想从同一随机对中获得两者并且更喜欢使用 JSON Extractor,那么您必须使用 3 个实例:

  1. 首先,在主样本上使用

    $.Result.Locations.*
    模式提取随机对: extract random pair

  2. 然后使用上述变量作为源和模式提取纬度

    $.Latitude
    extract latitude

  3. 对经度执行相同操作。

© www.soinside.com 2019 - 2024. All rights reserved.