有没有一种方法可以随机化get [] myJson中的jsonPath数组编号? [英] Is there a way to randomize the jsonPath array number in get[] myJson?

查看:363
本文介绍了有没有一种方法可以随机化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 ?

这篇关于有没有一种方法可以随机化get [] myJson中的jsonPath数组编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆