04 Basic Payload - biswajitsundara/Karate GitHub Wiki
If we want to POST data then follow this approach
Feature: Basic Payload Test Background: * def jsonPayload = read('./data/data.json') * url 'https://reqres.in/api/users' Scenario: Create user details Given request jsonPayload And header Content-Type = 'application/json' When method POST Then status=201 And print response
src\test\java\data\data.json
{ "data": { "last_name": "Weaver", "id": 2, "avatar": "https://reqres.in/img/faces/2-image.jpg", "first_name": "Janet", "email": "[email protected]" }, "support": { "text": "To keep ReqRes free, contributions towards server costs are appreciated!", "url": "https://reqres.in/#support-heading" } }
import org.junit.runner.RunWith; import com.intuit.karate.junit4.Karate; @RunWith(Karate.class) public class BasicPayLoadTest { }
- If we have multiple header information then use
headers
instead ofheader
And headers {Content-Type:'application/json', 'Accept:application/json'}
- If the header information is common to all scenarios then move it to background section and that's it
Feature: Basic Payload Test Background: * def jsonPayload = read('./data/data.json') * url 'https://reqres.in/api/users' * header Content-Type = 'application/json' Scenario: Create user details Given request jsonPayload When method POST Then status=201 And print response
Sometimes we will see the end points this way /api/users?delay=10
whatever comes after the question mark is the query parameter and then the value for the parameter. We need to use param delay = 10
to add the parameter to the end point
Scenario: Create user details with delay Given request jsonPayload And param delay = 10 When method POST Then status=201 And print response
If we want to pass multiple parameters then use And params {delay:10,cnty:ind}
etc.This can be moved to background section also if its applicable to all the scenarios.