API testing with Cypress

Make API call with Cypress

When I getting started with Cypress, it was very difficult to call API from Cypress spec file. When we migrate from one testing framework to another we may feel very difficult to make use of already existing API call functions which we have written in Javascript. I thought of sharing a way which we can reuse the same Javascript code in Cypress. Lets get started!!

To demonstrate this API call, I have made use of public weather API and you can sign up here.

Once you are done, you will get a secret API access key which you can use it to access the data. Try to call the same API with POSTMAN manually to confirm whether it is working fine.

I got 200 status code from postman

It’s time to create a API function to capture the response from the GET request. I am using axios to make GET request. Below is the piece of code created by me for making API call,

When you execute above JavaScript file with node command, you will get the temperature of the provided city inside query.

Once you installed Cypress, lets create a new folder inside Cypress folder called helper and add the same function inside it but don’t forget to export it!

folder structure

Once you have added the function, you may need import it inside plugins as below,

Now our goal will be executing this function from spec file and performing assertion by expecting temperature to be greater than 5. We will call function with cy.task() function then creating an alias name for it with .as() function to make use of response in other part of spec file. Refer below piece of spec file

That’s it!! You are done :-) Now you will get response if you execute the Cypress spec file,

Successful execution of testcase



Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siddharth Murugan

Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs