如何在空手道中从文件中读取的json中动态设置值 [英] how to dynamically set an value in json read from file in Karate
问题描述
我想使用KARATE框架的数据驱动功能为JSON(从文件中读取)中的某些元素动态设置值.这里是更多详细信息:
I want to dynamically set value for some elements in JSON(read from a file) using data driven feature of KARATE framework. Here are more details:
request.json -> { wheels : <wheel>, color: '<color>' }
功能:从文件读取json输入并遍历数据表值
Feature: Read json input from file and iterate over data table values
背景:
* url ''
* def reqJson = read('request.json')
* print reqJson
方案概述:读取测试文件
Scenario Outline: Test file read
# 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 |
我正在使用Karate开发自动化框架,我的意图是将示例请求保存在给定API的JSON文件中,然后在执行过程中我希望将元素值替换为上表中给出的值.为每个元素编写set语句(上面的注释行)
I am developing automation framework using Karate, my intention is to save sample request in JSON file for a given API and then during execution I want element values to be replaced with the ones given in the table above.I don't want to write set statement for each element either(commented lines above)
P.S .:我尝试使用表格方法调用其他功能文件.但是,我想为每个API保留一个功能文件,因此想知道上述方法是否有可能
P.S.: I tried with calling other feature file using table approach. However, I want to keep one feature file per API, hence want to know if there is any way possible for the above approach
推荐答案
我认为您错过了嵌入式表达式,在许多情况下,它比set
关键字简单,尤其是从文件中读取时.
I think you have missed embedded expressions which is simpler than the set
keyword in many cases, especially when reading from files.
例如:
request.json -> { wheels : '#(wheels)', color: '#(color)' }
然后这将起作用:
* def wheels = 4
* def color = 'blue'
* def reqJson = read('request.json')
* match reqJson == { wheels: 4, color: 'blue' }
如果您通过演示示例,您将获得很多其他想法.例如:
If you go through the demo examples you will get plenty of other ideas. For example:
* 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)' }
这篇关于如何在空手道中从文件中读取的json中动态设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!