karate - 空手道 - 从 json 中选择一个随机元素
问题描述
每次运行代码时,我都需要从下面的示例 json 中选择任何随机值。我怎样才能在空手道中做到这一点?我需要获取任何随机值并在另一个功能文件中使用。
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
推荐阅读
- javascript - 来自 ArrayBuffer 的普通浮点数组
- nginx - 来自 Kubernetes 中 ConfigMap 的自定义 nginx.conf
- tabulator - 在制表符中排序
- entity-framework-core - 如何获取在 HasComment 中设置的 EF Core 模型配置实体属性注释作为 html 标题属性
- python - Matplotlib 错误:可变参数关键字参数不能有默认值
- javascript - 单击时将js代码应用于多个li标签而不为每个标签编写相同的js代码(li)
- swift - 尝试删除行时,带有绑定视图的 ForEach 崩溃
- c++ - 无法在异步 lambda 中访问 std::vector*
- laravel - 我正在研究如何在 .env 文件中安全地添加数据库连接变量,但通过 laravel 中的刀片
- python - 如何使用 pytest 使发布请求异步?