Is there a way to randomize the jsonPath array number in get[] myJson?

岁酱吖の 提交于 2019-12-11 12:25:18

问题


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.

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?

回答1:


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



来源:https://stackoverflow.com/questions/57309503/is-there-a-way-to-randomize-the-jsonpath-array-number-in-get-myjson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!