Skip to main content

API TESTING IN CYPRESS

 Tutorial 10 – API Testing in Cypress


Adding the PDF link as the images are not fully loaded:


https://docs.google.com/document/d/1WymhdDurkCURxLHceazF3ozGTDtm4bPMr3gDUcXFfBg/edit?usp=sharing


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

Clickseen 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!

Comments

Popular posts from this blog

Add, remove, search an item in listview in C#

Below is the C# code which will help you to add, remove and search operations on listview control in C#. Below is the design view of the project: Below is the source code of the project: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Treeview_control_demo {     public partial class Form2 : Form     {         public Form2()         {             InitializeComponent();             listView1.View = View.Details;                   }         private void button1_Click(object sender, EventArgs e)         {             if (textBox1.Text.Trim().Length == 0)...

display files and directories in Listview

Below is the C# code which displays all files and directories in listview control with their file size and creation date. If it is file then it also displays the extension of the file e.g. .txt, .jpg etc Below is the design view of the project: Listview to display files and directories with size and date created Below is the source code of the project: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace search_in_listview {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();                   }         private void button1_Click(object sender, EventArgs ...

Add worklog in Jira using Python

 Below is the Python code to add the worklog in Jira. You need to install a request library for this. Here is the code: import requests from requests.auth import HTTPBasicAuth import json url = "https://your jira address here/rest/api/2/issue/ticket_number/worklog" auth = HTTPBasicAuth("username", "jira access token") headers = {     "Accept": "application/json",     "Content-Type": "application/json" } payload = json.dumps({     "comment": {         "content": [             {                 "content": [                     {                         "text": "This is for QA Testing",                         "type": "text"                     } ...