如何反序列化传递给接受多个参数的其他功能文件的 json 有效载荷

How to deserialize json payload passed to other feature file which accept multiple arguments

我正在向 .feature 文件发送多个参数,其中一个参数是请求 json 使用空手道 table 生成的负载。如何遍历请求负载以便 post 请求一次获得一个负载。

Scenario: post booking

* table payload
| firstname | lastname | totalprice | depositpaid | 
| 'foo'    | 'IN'      | 10     | true      |
| 'bar'    | 'out'     | 20     | true          | 

#date will calculate using js function in background and baseURL is configured in karate.config.js file

* set payload[*].bookingdates = { checkin: '#(date())', checkout: '#(date())' }
* def result = call read('createrecord.feature') {PayLoad: #(payload) , URL: #(baseURL)}

######################################
createrecord.feature file will have 

@ignore
Feature: To create data

Background: 
* header Accept = 'application/json'

Scenario: 
Given url __arg.URL
And path 'booking'
And request __arg.PayLoad
When method post
Then status 200

在 createrecord.feature 文件中,我如何遍历传递的有效负载,以便将单个有效负载传递给 post 请求。

您缺少的简单规则是,如果 call 的参数是一个 JSON 数组(包含 JSON 个对象),它将自动迭代。

请仔细阅读文档:https://github.com/intuit/karate#data-driven-features

因此进行此更改:

* def result = call read('createrecord.feature') payload

并且baseURL将在createrecord.feature中可用,因此您无需担心通过它。

请注意,这可能不起作用:* set payload[*].bookingdates 参考此答案: