Tutorial 10 API Testing in Cypress
Tutorial 10 API Testing in Cypress
So API is nothing but a resource through which you are getting information from any
database, but you don’t have the direct access to the db. Now makemytrip does not
have access to the database of airlines (Indigo etc). Instead, airlines expose some of their
APIs. So makemytrip is getting the information from spicejet or Air India through an API.
So if a user enters source/destination/date & searches a flight on makemytrip.com, the
latter in turn makes an API call to all the airlines and display the results in the browser.
Click the request link from left hand side and expand Yielded
Expand body and expand data. You would see the data of all the 24 employee. You can
also see Array(24)
Let us try to assert the status code
Save the file. Notice that Actual is same as Expected, see below
API Testing in cypress using cy.request() command (POST method)
Let us now see how to automate a POST method using cypress. The POST method will
create a new record in database. Let us use another dummy website https://reqres.in/
Click POST, you would see a sample Response
Let us comment the current ‘it’ method & add a new ‘it’ method. In the POST method,
we have to pass the sample json (for the new record) as 3rd argument to the cy.request()
Save the code, notice below that the test is apss & new record is inserted
Now let us add another user and validate the ‘name’ field in the body
Save.
Notice that assertion gets passed