空手道-从json中选择一个随机元素 [英] Karate - Select a random element from json

查看:53
本文介绍了空手道-从json中选择一个随机元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次运行代码时,我都需要从下面的示例json中选择任意随机值.我如何在空手道中实现这一目标?我需要获取任何随机值并在另一个功能文件中使用.

I need to select any random value from the below sample json every time i run the code. How can i achieve this in Karate? I need to get any random value and use in another feature file.

def myJson = 
"""
{
      "sampleJson": {
        "random1": "1",
        "random2": "2",
        "random3": "3",
        "random4": "4",
        "random5": "5"
      }
  }
"""

推荐答案

另请参阅: https://github.com/intuit/karate#commonly-needed-utilities

* def random = function(max){ return Math.floor(Math.random() * max) + 1 }
* def myJson = 
"""
{
  "sampleJson": {
    "random1": "1",
    "random2": "2",
    "random3": "3",
    "random4": "4",
    "random5": "5"
  }
}
"""
* def key = 'random' + random(5)
* def result = myJson.sampleJson[key]
* print result

这篇关于空手道-从json中选择一个随机元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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