npm install cypress
npm install axios
Sign up weather API
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.
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,
Adding API function into Cypress
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!
Adding inside index.js of Plugins folder
Once you have added the function, you may need import it inside plugins as below,
Making API call inside spec file
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,