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

RGB Color Demo in C# using scrollbar as a slider horizontally

Below is the project which contains 3 horizontal scrollbars if we slides the scrollbars the background color of the form is changing accordingly. first scrollbar is adding red color, second adding green and third one adding blue upon scrolling or sliding. Take 3 horizontal scrollbars and assign them maximum value of 255(from 0) to each one. Below is the designer 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 Color_RGB_demo {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();                   }         public...

MULTIPLEXER , Design & Implement the given 4 variable function using IC74LS153. Verify its Truth-Table

TITLE: MULTIPLEXER   AIM: Design & Implement the given 4 variable function using IC74LS153. Verify its Truth-Table.   LEARNING OBJECTIVE: ·        To learn about IC 74153 and its internal structure. ·        To realize 8:1 MUX and 16:1 MUX using IC 74153.   COMPONENTS REQUIRED: IC 74153, IC 7404, IC 7432, CDS, wires, Power supply. IC PINOUT:            1)     IC 74153 2)      IC 7404:                                              3) IC 7432 THEORY:   ·        Multiplexer is a combinational circuit that is one of the most widely used in digital design. ·        The multiplexer is a data selector which gates one out of several inputs to a sin...

Get/Access JIRA/JIRA API data using python

 Below is the program which will guide you on how to access the JIRA API data using python. First, you need to create a token for JIRA. You need to install the following libraries using the following commands: pip install requests pip install json-excel-converter And the code is: # Import the required libraries import requests from requests.auth import HTTPBasicAuth import json import pandas as pd from json_excel_converter import Converter from json_excel_converter.xlsx import Writer # URL to Search all issues. url = "YOUR JIRA URL" # Create an authentication object,using # registered emailID, and, token received. auth = HTTPBasicAuth("email-d",                      "jira token created") # The Header parameter, should mention, the # desired format of data. headers = {     "Accept": "application/json" } # Mention the JQL query. # Here, all issues, of a project, are # fetched,as,no criteria is mentioned. query = { ...