Running API Test Using Cypress
Cypress is not just UI automation tool . It can be used for testing APIs as well . Even though we have other tools like Postman, Newman, Rest Assured, SOAP UI etc for testing APIs, I believe cypress is a good alternative for testing API. It will help to use same tool for both UI and API test automation.
Demo
Let us look at a sample API test case. In below example, we trigger a API call to http://services.groupkt.com/country/get/iso2code/AU
and validate below in the response.
- Status code of response is 200.
- Header include ‘application/json’.
- Body contain “Country found matching code [AU].”
We can then extend this to do any further checks if needed.
Create a new file inside Integration
folder of cypress and copy below code into that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Open Cypress by running node_modules/.bin/cypress open
inside cypress root folder. This will open up Cypress.
Run newly created test.
Results of test execution will look like below.
Expand each of them and right click on the asserts and inspect the element. This will open up chrome developer tool. Select the console tab , which will list down details of calls made, request received and assertions performed. It will help to write additional assertions, investigate any failure etc.