In this article, we will discuss how we can perform API testing using cypress. To further deep dive, let’s automate the below scenario:

1. Perform a GET request to the endpoint https://randomuser.me/api/?results=1
2. Validate that the response code is 200
3. Check that the response body has the property ‘info’
4. Check that the response body has a property as ‘version’ and its corresponding value is ‘1.3’

Step 1: Using any API tool (I recommend Postman), perform a GET request to the endpoint https://randomuser.me/api/?results=1 and check the response status and response body. We will then use this response to create validations in our tests.

GET request in Postman

Response Body:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{
    "results": [
        {
            "gender": "female",
            "name": {
                "title": "Ms",
                "first": "Amy",
                "last": "Stanley"
            },
            "location": {
                "street": {
                    "number": 336,
                    "name": "Church Street"
                },
                "city": "St Davids",
                "state": "Hertfordshire",
                "country": "United Kingdom",
                "postcode": "XV3 3WL",
                "coordinates": {
                    "latitude": "8.9788",
                    "longitude": "163.0111"
                },
                "timezone": {
                    "offset": "+7:00",
                    "description": "Bangkok, Hanoi, Jakarta"
                }
            },
            "email": "amy.stanley@example.com",
            "login": {
                "uuid": "1dbd02c7-bd76-442f-ad02-abd256dd3617",
                "username": "blueostrich755",
                "password": "686868",
                "salt": "ZDyrl7P2",
                "md5": "e8632cd7e236145f849b01b21bf961f1",
                "sha1": "e027eb9356c8978743dbe66f8166ecc1f22ea228",
                "sha256": "f469a148947e00f6ebc3512bdf1396fba705d743c6b5e038c860f2af944063c5"
            },
            "dob": {
                "date": "1988-12-19T00:46:16.496Z",
                "age": 32
            },
            "registered": {
                "date": "2004-09-10T21:28:01.868Z",
                "age": 16
            },
            "phone": "017684 68676",
            "cell": "0736-101-209",
            "id": {
                "name": "NINO",
                "value": "WN 16 65 98 Y"
            },
            "picture": {
                "large": "https://randomuser.me/api/portraits/women/32.jpg",
                "medium": "https://randomuser.me/api/portraits/med/women/32.jpg",
                "thumbnail": "https://randomuser.me/api/portraits/thumb/women/32.jpg"
            },
            "nat": "GB"
        }
    ],
    "info": {
        "seed": "b17f20d21b09c92c",
        "results": 1,
        "page": 1,
        "version": "1.3"
    }
}

 
Step 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
describe('Example to demonstrate API Testing in cypress', function () {

    it('Hit an API End point and validate its response status code and body', () => {
        cy.request({
            method: 'GET',
            url: 'https://randomuser.me/api/',
            qs: 'results=1'
        }).then((response) => {
            expect(response.status).to.eq(200)
            expect(response.body).to.have.property('info')
            expect(response.body.info).to.have.property('version', '1.3')
        })
    })
})

cypress api test script

cy.request() is used for making HTTP requests in cypress. Now inside cy.request we will pass three options object; method as ‘GET‘, URL as ‘https://randomuser.me/api/‘ and qs(Query parameters) as ‘results=1‘.

expect(response.status).to.eq(200) checks that the response status code is 200.

expect(response.body).to.have.property(‘info’) checks that the response body contains the property ‘info‘.

expect(response.body.info).to.have.property(‘version’, ‘1.3’) check that inside ‘info‘ we have the key ‘version‘ and its corresponding value as ‘1.3‘.
 
Step 3: After successful execution:

cypress api test execution

Do check out 🙂

Github: https://github.com/alapanme/Cypress-Automation
All Cypress Articles: https://testersdock.com/cypress-tutorial/