【问题标题】:Gatling : condition to get a specific value from a JSONGatling :从 JSON 中获取特定值的条件
【发布时间】:2022-01-22 19:50:00
【问题描述】:

我是 Gatling 新手,在这方面有点挣扎。

我有这个来自 HTML 的 JSON 对象:

<div id="DATA--DECL-DATA">{"isCompany":false,"accommodations":[{"id":"00000000031000000067","isChecked":false,"name":"5 JULI 2017","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","nightsDeclared":0,"schoolNightsDeclared":0,"schoolNightsAttached":0,"taxableNights":0.0,"totalPayment":0.0,"isInProgress":false,"isLate":false,"isPayed":"false","deadline":"2021-12-31","initialAmount":0.0,"remainingAmount":0.0},{"id":"00000000031000006362","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":false},{"id":"00000000031000006380","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":true},{"id":"00000000031000006390","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":true}]}</div>

如果美化了,渲染这个:

{
  "isCompany": false,
  "accommodations": [
    {
      "id": "00000000031000000067",
      "isChecked": false,
      "name": "5 JULI 2017",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "isInProgress": false
    },
    {
      "id": "00000000031000006362",
      "isChecked": false,
      "name": "BELLEVIE",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York"
      "isInProgress": false
    },
    {
      "id": "00000000031000006380",
      "isChecked": false,
      "name": "BELLEVIE",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "isInProgress": true
    },
    {
      "id": "00000000031000006390",
      "isChecked": false,
      "name": "BELLEVIE",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "isInProgress": true
    }
  ]
}

为了从那个 div 中获取这个 JSON 数组并将其保存到 Gatling 中的会话变量中,我写了这个“检查”:

.check(css("div#DATA--DECL-DATA").saveAs("myJsonObj"))

然后在脚本执行后在控制台中打印结果,我写了这个:

.exec { session => println("json = " + session("myJsonObj").as[String]); session }.exitHereIfFailed

这将在控制台中打印出您在上面看到的完整的美化 JSON 数组。


现在,在那个住宿 JSON 数组中,我们可以看到当 "isInProgress" 为 false 时有多个 id

那么我的问题是,当 "isInProgress" 为假时,如何获取住宿的第一个 id

所以:如果 "IsInProgress" 为 false => 获取该数组中的第一个住宿 id。

【问题讨论】:

  • 您尝试过哪些解决方案,哪些不起作用?
  • 我首先尝试使用正则表达式获取该数组中的住宿 id,它起作用了,.check(regex(""""accommodations":[\{"id":"(.*? )"""").saveAs("accommodationId"))。但是,它总是会呈现第一个住宿 id。当“isInProgress”为假时,目标是能够在该 json 数组中获得第一个住宿。因此,每次在表单中提交一个 id 并将“isInProgress”变为“true”时,它将获得一个“isInProgress”为“false”的现有 id。

标签: regex scala gatling


【解决方案1】:

以下代码是用 Javascript 编写的,但应该很容易转换为 scala。

var json = {
  "isCompany": false,
  "accommodations": [
    {
      "id": "00000000031000000067",
      "isChecked": false,
      "name": "5 JULI 2017",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "isInProgress": false
    },
    {
      "id": "00000000031000006362",
      "isChecked": false,
      "name": "BELLEVIE",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "isInProgress": false
    },
    {
      "id": "00000000031000006380",
      "isChecked": false,
      "name": "BELLEVIE",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "isInProgress": true
    },
    {
      "id": "00000000031000006390",
      "isChecked": false,
      "name": "BELLEVIE",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "isInProgress": true
    }
  ]
}

var FilteredJson = json.accommodations.filter((value) => {if (value.isInProgress === false) {return value}});

//just take the first value of FilteredJson

console.log(FilteredJson[0]);

【讨论】:

    【解决方案2】:

    根据我之前的answer,您需要check 带有条件的json-path:

    .check(jsonPath("$.accommodations[?(@.isInProgress==false)].id").find.saveAs("id"))
    

    解释:

    @.isInProgress==false - 这是数组accommodations 中每个对象的条件。 更多关于 json-path 的细节可以查看here

    Gatling 的方法find 返回第一个匹配项。还存在返回所有值的方法findAll...

    【讨论】:

    • 您错过了“我有这个来自 HTML 的 JSON 对象”。如果此信息正确,则负载不是 JSON,您不能使用 jsonPath 或 jmesPath。
    • @StéphaneLANDELLE 是的,但在另一个问题中,我回答了如何仅使用 json 进行转换工作stackoverflow.com/a/70436671/7512201 我看到了你的答案,但我认为没有必要使用杰克逊......
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2019-03-30
    • 2017-11-22
    • 2017-09-19
    相关资源
    最近更新 更多