如何在从空手道文件读取的json中动态设置值

nag*_*pai 3 karate

我想使用 KARATE 框架的数据驱动功能动态设置 JSON 中某些元素的值(从文件中读取)。以下是更多详细信息:

request.json -> { wheels : <wheel>, color: '<color>' }
Run Code Online (Sandbox Code Playgroud)

功能:从文件中读取 json 输入并迭代数据表值

背景:

* url ''
* def reqJson = read('request.json') 
* print reqJson
Run Code Online (Sandbox Code Playgroud)

场景大纲:测试文件读取

# I want to avoid writing below set statements for each element in request
#* set reqJson.wheels = <wheel>
#* set reqJson.color = '<color>'

Given path ''
And request reqJson
When method POST
Then status 200
And match response contains {mode: '<result>'}

Examples:

| wheel | color | result  |
| 4     | red   | car     |
| 2     | any   | bicycle | 
Run Code Online (Sandbox Code Playgroud)

我正在使用 Karate 开发自动化框架,我的目的是在 JSON 文件中保存给定 API 的示例请求,然后在执行期间我希望元素值替换为上表中给出的值。我不想写 set每个元素的声明(上面的注释行)

PS:我尝试使用表格方法调用其他功能文件。但是,我想为每个 API 保留一个功能文件,因此想知道上述方法是否有任何可能

Pet*_*mas 7

我认为您错过了在许多情况下比关键字更简单的嵌入式表达式set,尤其是在从文件中读取时。

例如:

request.json -> { wheels : '#(wheels)', color: '#(color)' }

然后这将起作用:

* def wheels = 4
* def color = 'blue'
* def reqJson = read('request.json')
* match reqJson == { wheels: 4, color: 'blue' }
Run Code Online (Sandbox Code Playgroud)

如果您浏览演示示例,您将获得许多其他想法。例如:

* table rows
| wheels | color  | result |
|      4 | 'blue' | 'car'  |
|      2 | 'red'  | 'bike' |

* call read('make-request.feature') rows
Run Code Online (Sandbox Code Playgroud)

哪里make-request.feature是:

Given path ''
And request { wheels: '#(wheels)', color: '#(color)' }
When method POST
Then status 200
And match response contains { mode: '#(result)' }
Run Code Online (Sandbox Code Playgroud)