有没有一种方法可以随机化get [] myJson中的jsonPath数组编号?

问题描述:

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

I have a list of values that I can use for the title field in my json request. I would like to store a function in the common.feature file which randomizes the title value when a scenario is executed.

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

I have attempted using the random number function provided on the commonly needed utilities tab on the readme. I have generated a random number successfully, the next step would be using that randomly gernerated number within the jsonpath line in order to retrieve a value from my data list which is in 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?

我不确定您想要什么,但是不能仅将get[randomNumber] myJson.title.enum中的randomNumber替换为0吗?

I'm not sure of what you want, but can't you just replace 0 by randomNumber in get[randomNumber] myJson.title.enum ?