Karate API Automation-Assertions using matcher APIs
Hi there!!
This is a continuation to my previous post where I have covered Karate framework setup, how to automate GET API and validate response.
I would recommend you to read my previous post before starting with this post.
In this post, I would be covering following topics related to Payload Assertions:
- Assert whole actual JSON against expected JSON using match equals
- To check whether particular field in response is present and not null using match !null
- To assert response by ignoring value of particular field
- Assertions using match contains, match not contains, match contains only
So, first lets understand what is response in Karate. response is a built-in variable in karate that stores HTTP API response. $ represents the response.
So below lines are equivalent:
Then match response $ == {some expected json}
Then match response == {some expected json}
Then match $ == {some expected json)
Assert whole actual JSON against expected JSON using match equals (==)
Here I am using very simple GET API
API Url: ‘https://reqres.in/api/users?page=2’
API method: GET
API response: 200 OK with a list of users
users.feature file:
Feature: Check list of users
Background:
* url 'https://reqres.in/api/users'
* header Accept = 'application/json'
Scenario: Get list of all the users
Given param page = 2
When method GET
Then status 200
Paste the url in any browser and see the raw response
- Select all the raw data and validate it using any json validator.
Copy all the raw data.
Open “https://jsonlint.com/”. Paste the raw data in textbox.
Click on “Validate JSON” button
Now if we want to validate the response as whole json, create a file named as “EResult.json” under “Karate.api.data” package (Create a separate package where all the data files will reside). Paste the raw json in it and Save it.
And to validate in feature file, write the below statement
In Background section, add:
Background:
* def expectedOutput = read(‘../data/EResult.json’)
- Here I have defined a variable “expectedOutput” with def keyword. I have read the created EResult.json and stored in the variable.
- Variables set using
def
in theBackground
will be re-set before everyScenario
- read() is the built-in JavaScript function which is used to read file in Karate.
Now to check whether expected JSON and actual JSON are same we can validate by using below statement
In Scenario section, add:
And match response == $ OR
And match response == expectedOutput
Here response/ $ is the actual output we get after running the script and expectedOutput is the expected output
Feature: Fetch list of users
Background:
* url 'https://reqres.in/api/users'
* header Accept = 'application/json'
* def expectedOutput = read('../data/EResult.json')
Scenario: Get list of all the users
Given param page = 2
When method GET
Then status 200
And match $ == expectedOutput
And match response == expectedOutput
To check whether particular field is present and not null using match !null
So, there are many instances, wherein we are required to check whether particular field in response is present and not null. For that, we can use Karate’s matcher api
So for the above response, suppose we need to check that “first_name” is not null and has a value, we can achieve that by,
And match response.data[0].first_name != null
To assert response by ignoring value of particular field in response
There would be scenarios where we would like to assert response by ignoring some of the fields which are dynamic or not necessary to be verified. e.g: UUID, timestamp, image url path, etc.
So, for the above example, we can use as below:
And match response.data[0] == {“id”: 7, “email”: ‘#ignore’, “first_name”: “Michael”, “last_name”: “Lawson”, “avatar”: ‘#ignore’}
Assertions using match contains
match contains:
So, if we want to check for existence of some fields in JSON array, we can use match
(name) contains
And match response.data[*].id contains [7, 8, 9, 10, 11, 12]
And match response.data[*].id contains [12, 9, 8, 11, 10, 7]
And match response.data[*].id contains [7, 9, 8, ]
And match response.data[*].email contains 'lindsay.ferguson@reqres.in'
And match response.data[*].first_name contains ['Lindsay', 'Tobias', 'Byron']
match (not) !contains:
If we want to check whether some field, key-value pair does not exist we can do that by match
(name) !contains
And match response.data[*].id !contains [null, 1]
And match response.data[*].id !contains 3
And match response.data[*].id !contains [1, 2, 3]
match contains only:
When we need to assert that all array elements are present but in any order we can achieve that by:
And match response.data[*].id contains only [7, 8, 9, 10, 11, 12]
And match response.data[*].id contains only [12, 9, 8, 11, 10, 7]
When inspecting a JSON array, contains
just checks if the expected items exist. The size and order of the actual array does not matter.
users.feature file:
This ends the assertion part. In the next post I would be explaining how to integrate cucumber reports in Karate framework.
Thanks for reading!