【问题标题】:how to dynamically set an value in json read from file in Karate如何动态设置从空手道文件读取的json中的值
【发布时间】:2018-04-29 15:26:09
【问题描述】:

我想使用 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 | 

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

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

【问题讨论】:

    标签: karate


    【解决方案1】:

    我认为您错过了embedded expressions,它在许多情况下比set 关键字更简单,尤其是在从文件中读取时。

    例如:

    request.json -&gt; { 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)' }
    

    【讨论】:

    • 感谢彼得的帮助,这两种方法都运行良好。空手道框架丰富而酷炫的功能给我们留下了深刻的印象!
    • 嗨,彼得,我投了赞成票,但可以看到这条消息“感谢您的反馈!声望低于 15 人的投票会被记录,但不要更改公开显示的帖子得分。”跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多