首页 文章

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

提问于
浏览
1

我想使用KARATE框架的数据驱动功能为JSON中的某些元素(从文件中读取)动态设置值 . 这里有更多细节:

request.json -> { wheels : <wheel>, color: '<color>' }

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

背景:

* 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 |

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

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

1 回答

  • 1

    我认为你错过了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)' }
    

相关问题