如何在 json 中动态设置从空手道文件中读取的值
how to dynamically set an value in json read from file in Karate
我想使用 KARATE 框架的数据驱动功能为 JSON(从文件读取)中的某些元素动态设置值。以下是更多详细信息:
request.json -> { wheels : <wheel>, color: '<color>' }
功能:从文件读取 json 输入并迭代数据 table 值
背景:
* url ''
* def reqJson = read('request.json')
* print reqJson
场景大纲:读取测试文件
# 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 |
我正在使用空手道开发自动化框架,我的目的是在 JSON 文件中保存给定 API 的示例请求,然后在执行期间我希望元素值被给定的替换在 table above.I 中也不想为每个元素编写 set 语句(上面的注释行)
P.S.: 我尝试使用 table 方法调用其他功能文件。但是,我想为每个 API 保留一个功能文件,因此想知道上述方法是否有任何可能的方法
我想你错过了 embedded expressions,它在很多情况下比 set
关键字更简单,尤其是从文件中读取时。
例如:
request.json -> { wheels : '#(wheels)', color: '#(color)' }
然后这会起作用:
* def wheels = 4
* def color = 'blue'
* def reqJson = read('request.json')
* match reqJson == { wheels: 4, color: 'blue' }
如果您完成 demo examples,您会得到很多其他想法。例如:
* table rows
| wheels | color | result |
| 4 | 'blue' | 'car' |
| 2 | 'red' | 'bike' |
* call read('make-request.feature') rows
而 make-request.feature
是:
Given path ''
And request { wheels: '#(wheels)', color: '#(color)' }
When method POST
Then status 200
And match response contains { mode: '#(result)' }
我想使用 KARATE 框架的数据驱动功能为 JSON(从文件读取)中的某些元素动态设置值。以下是更多详细信息:
request.json -> { wheels : <wheel>, color: '<color>' }
功能:从文件读取 json 输入并迭代数据 table 值
背景:
* url ''
* def reqJson = read('request.json')
* print reqJson
场景大纲:读取测试文件
# 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 |
我正在使用空手道开发自动化框架,我的目的是在 JSON 文件中保存给定 API 的示例请求,然后在执行期间我希望元素值被给定的替换在 table above.I 中也不想为每个元素编写 set 语句(上面的注释行)
P.S.: 我尝试使用 table 方法调用其他功能文件。但是,我想为每个 API 保留一个功能文件,因此想知道上述方法是否有任何可能的方法
我想你错过了 embedded expressions,它在很多情况下比 set
关键字更简单,尤其是从文件中读取时。
例如:
request.json -> { wheels : '#(wheels)', color: '#(color)' }
然后这会起作用:
* def wheels = 4
* def color = 'blue'
* def reqJson = read('request.json')
* match reqJson == { wheels: 4, color: 'blue' }
如果您完成 demo examples,您会得到很多其他想法。例如:
* table rows
| wheels | color | result |
| 4 | 'blue' | 'car' |
| 2 | 'red' | 'bike' |
* call read('make-request.feature') rows
而 make-request.feature
是:
Given path ''
And request { wheels: '#(wheels)', color: '#(color)' }
When method POST
Then status 200
And match response contains { mode: '#(result)' }