Shekhar Patil
Shekhar Patil

Reputation: 88

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

I am sending multiple arguments to .feature file one of the argument is request json payload generated by using karate table. How to iterate through request payload so that post request will get one payload at a time.

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

Here in createrecord.feature file how I can iterate through passed payload so that single payload will be passed to post request.

Upvotes: 1

Views: 503

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

The simple rule you are missing is that if the argument to call is a JSON array (of JSON objects) it will iterate automatically.

Read the docs carefully please: https://github.com/intuit/karate#data-driven-features

So make this change:

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

And baseURL will be available in createrecord.feature so you don't need to worry about passing it.

Note that this may not work: * set payload[*].bookingdates refer this answer: https://stackoverflow.com/a/54928848/143475

Upvotes: 1

Related Questions