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
describe('API Testing with Cypress', () => {
    var result
    
    it('Validate the header', () => {
       result = cy.request('http://services.groupkt.com/country/get/iso2code/AU')
      
       result.its('headers')
             .its('content-type')
             .should('include', 'application/json')
        
    })

    it('Validate the status', () => {
        result = cy.request('http://services.groupkt.com/country/get/iso2code/AU')
       
        result.its('status')
              .should('equal',200);
     })

     it('Validate the body ', () => {
        result = cy.request('http://services.groupkt.com/country/get/iso2code/AU')
       
        result.its('body')
              .its('RestResponse.messages')
              .should('include', 'Country found matching code [AU].');
      
     })
}) 

Open Cypress by running node_modules/.bin/cypress open inside cypress root folder. This will open up Cypress.

Run newly created test.

APITestingWithCypress

Results of test execution will look like below.

[APITestingWithCypress]

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.

[APITestingWithCypress]

Comments