首页 > 解决方案 > 有没有办法随机化 get[] myJson 中的 jsonPath 数组编号?

问题描述

我有一个值列表,可用于我的 json 请求中的标题字段。我想在 common.feature 文件中存储一个函数,该函数在执行场景时随机化标题值。

我尝试使用自述文件中常用实用程序选项卡上提供的随机数功能。我已经成功生成了一个随机数,下一步将在 jsonpath 行中使用该随机生成的数字,以便从我的 json 数据列表中检索一个值。


    * def myJson =
"""
{
  "title" : {
               "type" : "string",
               "enum" : [
                  "MR",
                  "MRS",
                  "MS",
                  "MISS"
                  [...]
               ]
            }
}
"""

    * def randomNumber = random(3)
    * def title = get[0] myJson.title.enum


    * print title```

The code above works but I would like to randomize the number within the get[0]. How is this possible in Karate?

标签: jsonkarate

解决方案


我不确定你想要什么,但你不能用 randomNumber 替换 0get[randomNumber] myJson.title.enum吗?


推荐阅读