In the previous blog, we saw how to make GET and POST calls using karate DSL. In this blog, we will see how we can use karate DSL by reading data from external files and also how we can change the values of the request body.
- Firstly we will need to create a simple JSON file from which we will be reading the body for our request.
{
"firstName":"James",
"lastName":"White",
"username":"jamesWhite",
"email":"james@user.com"
}
- Now we will create a .feature file. Which will make a POST call reading the request body from an external file using this line of code
And def BodyOfRequest = read('/home/ankur/Desktop/KARATE/new/karateBlog/src/test/java/examples/users/dataFiles/userData.json')
And request BodyOfRequest
- In this line, we are defining a variable BodyOfRequest, reading a an external file userData.json which is stored locally in our system using the read function and store the content of the file it the variable.
- Request function is used to give an input body to our request.
Feature: POST API call.
Background:
* url 'https://43db3005-4ed9-4bab-b0a3-bb066e79e816.mock.pstmn.io'
Scenario: create a user from given details.
Given path '/POSTUser'
And def BodyOfRequest = read('/home/ankur/Desktop/KARATE/new/karateBlog/src/test/java/examples/users/dataFiles/userData.json')
And request BodyOfRequest
When method post
Then status 201
And match $.Status == '#present'
And match $.Status == 'OK'
karate.properties[‘user.dir’]
- As we know according to best practices of testing we should avoid using hard-coded values in our test script. But in the above example, we are giving the path to file on our local system. If we want to execute the same code on some other system the code will give us an error.
- So, to overcome this issue we can use an inbuilt function which is karate.properties. It gets the value of any Java system-property by name.
- We will use karate.properties[‘user.dir’] which will automatically pick user’s working directory and then append it to the path of our project files.
And def BodyOfRequest = read(karate.properties['user.dir'] + '/src/test/java/examples/users/dataFiles/userData.json')
Changing request body in test script.
- A single data file can be used by multiple test cases. So, some times we need to change the values of the request body.
- For this, we will use the variable in which our request body is stored and we can input the value one at a time.
And set BodyOfRequest.firstName = 'Peter'
- set is used for setting the value of field firstName. As our data file is .JSON format, we are using the key-value pair to define the values.
Feature: POST API calls.
Background:
* url 'https://43db3005-4ed9-4bab-b0a3-bb066e79e816.mock.pstmn.io'
Scenario: create a user from given details.
Given path '/POSTUser'
And def BodyOfRequest = read(karate.properties['user.dir'] + '/src/test/java/examples/users/dataFiles/userData.json')
And request BodyOfRequest
And set BodyOfRequest.firstName = 'Peter'
And set BodyOfRequest.lastName = 'Parker'
And set BodyOfRequest.email = 'spider@gmail.com'
And set BodyOfRequest.username = 'spiderman'
When method post
Then status 201
And match $.Status == '#present'
And match $.Status == 'OK'
- In the execution, we can see that the request body is the same which we have given in our in code above.
So, this was a short blog on karate DSL by Reading data from external files. And also how we can change the values of request body using.
References : https://github.com/intuit/karate