Learn about API Testing in Cypress (Step by step)

Tutorial 10 – API Testing in Cypress

What you will Learn :

  • What is API?

  • API Testing​​ in cypress​​ using cy.request() command​​ (GET​​ method)

  • API Testing in cypress using cy.request() command (POST​​ method)​​ 

What is API​​ (Application Programming Interface)

Look at below figure.​​ When you download some application (example Zoho app,​​ on your mobile or desktop)​​ and​​ when you try to​​ signup, it​​ might ask​​ you if you want to​​ Login with​​ your Gmail/Facebook/Twitter​​ credentials.​​ So when you​​ login​​ using any of these apps,​​ you are​​ successfully​​ logged in. Here,​​ Zoho​​ and Facebook/Gmail​​ are different applications,​​ but they communicate​​ eahc other using​​ APIs.​​ So​​ facebook app exposed some of it’s APIs​​ to​​ provide facility to login​​ Zoho.

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. ​​ 

 

API Testing​​ in cypress​​ using cy.request command​​ (GET)

We will be using​​ http://dummy.restapiexample.com/​​ to test an API using cypress. Let us first see the GET method

If you click the url (shown under Full route), you would see​​ all the employee data

Click the Details link

You would see a sample of how the result will be dispayed when we request this api

Lets create a new​​ spec.js​​ file

cy.request() command accepts 2 arguments: the first one is type of HTTP method (here GET), the second is the request url​​ 

Save the file & run, you see below

Press F12​​ and go to Console

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

Click​​ seen on LHS.

On the RHS, you would see​​ ‘Actual’​​ is same as​​ ‘Expected’

Now from the previous snapshot we know that the response body data has length of 24

Let us validate this as well (see​​ line 7​​ below)

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

So this is how we can test basic APIs using cypress.

Thanks for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 1st Nov 2024