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

MySQL practical Tutorials part 9- SQL not operator, SQL Not Like, SQL greater than, SQL less than greater than operator

 ========================================================================= Not Equal SELECT title FROM books WHERE released_year = 2017;   SELECT title FROM books WHERE released_year != 2017;   SELECT title, author_lname FROM books;   SELECT title, author_lname FROM books WHERE author_lname = 'Harris';   SELECT title, author_lname FROM books WHERE author_lname != 'Harris'; ========================================================================= Not Like SELECT title FROM books WHERE title LIKE 'W';   SELECT title FROM books WHERE title LIKE 'W%';   SELECT title FROM books WHERE title LIKE '%W%';   SELECT title FROM books WHERE title LIKE 'W%';   SELECT title FROM books WHERE title NOT LIKE 'W%'; ========================================================================= Greater Than SELECT title, released_year FROM books ORDER BY released_year;   SELECT title, released_year FROM books  WHERE released_year > 2000 ORDER BY release...

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