top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

How to Perform API Test Automation using Rest Assured


API stands for Application Programming Interface. It serves as an interface between different software systems and establishes their interaction and data exchange.


What is API testing?

API testing is the process of sending requests to an API and monitoring the responses to ensure its behaving as expected. API testing is designed to assess the functionality, reliability, performance, and security of an API, and is therefore an essential part of the API development lifecycle.



Types of API Testing:

There are multiple API testing approaches, including:


Functional testing: These API tests are designed to check that an API returns the right   response for a given request.

Load testing: This type of API test gauges how an API handles a large volume of requests over a short period.

Runtime and error detection testing: These API tests are designed to evaluate the actual running of the API and typically focus on monitoring, execution errors, resource leaks, or error detection.

Security testing: These tests assess how an API responds to and resists cyberattacks.

Penetration Testing: Penetration tests involve users with limited API knowledge trying to attack the API, which enables testers to assess the threat vector from an outside perspective.

Validation testing: Validation tests are run late in the testing stage to verify the behavior and efficiency of the API.


What is REST ASSURED and How it works?


REST stands for Representational State Transfer. REST Assured is a java library used for testing and validating the Restful Web Services. It supports Behavior-Driven Development (BDD) syntax like Given, When, and Then notations. It helps us to integrate with testing frameworks like Junit or TestNG.         

   

Let us understand how the Rest Assured Works


 1.    Create an HTTP request with all the details

2.    Send the request over the network

3.    Validate the received Response


HTTP Request

An HTTP request consists of the following     


URL

The Request URL is the unique address used to make a request. URL consists of the Base URL, Resource, Query, or Path Parameters.

Example: 

                       

Base URL

It is the Base Address for the particular API.

Resource:

A Resource represents the collection that can be accessed from the server.

Path Parameter:

The Path parameter is the variable part of a URL. It is used to point to a particular resource within a collection, such as a user with a specific ID. In Path Parameter we move to the sub resource.

HTTP Methods:

GET Method: This method is used for retrieving the data from the server as per the requested API.

Example: Example, we used the reqres.in site for reference.

                


POST Method: This method is used for sending/creating the data to the server.

Example :

                           

PUT Method: This method is used for updating the existing data. 

Example:

                    

DELETE Method: This method is used for deleting the data from the server.

Example:

                         

PATCH Method: This method is for updating the existing data partially instead of the complete data update.

Example:                          

Creating a REST Assured Project


In order to create the framework first we need to set up an IDE, install java and then create a maven project.

Steps to create a Maven Project:

Go to eclipse > navigate to File > new > Project > select maven project > Next >Provide group & Artifactid to create a project.

Steps to install TestNG in maven project: Go to help in eclipse> go to eclipse marketplace> now search TestNg and install TestNG in maven project.

In pom.xml add the maven-compiler-plugin and maven-surefire-plugin to run the testng.xml file


Automate GET request & Validate Status Code

In this example, we will send a GET request to the API and check the status code of the response. We expect the status code to be 200, which indicates that the request was successful.


package tests; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response;  public class GetTest { @Test public void get_test        { Response response = RestAssured.get("https://reqres.in/api/users?page2");  System.out.println(response.getStatusCode());         

 System.out.println(response.getTime());            

   } }

The statusCode() method is used to determine the response’s status code.

The Response is shown in the below picture:

STATUS CODE : 200

If the status code is 200 then the server has successfully processed the request.

 Conclusion

 Rest Assured is a powerful and valuable API automation tool. It provides a simple and easy-to-use DSL for writing RESTful web service tests. Rest Assured is compatible with a wide range of HTTP methods, authentication schemes, and testing frameworks. We discussed Rest Assured and how to use it for API automation in this blog post.

Thank you..

97 views1 comment

1 comentário

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
Convidado:
10 de jan.

Nice Explanation...

Curtir
bottom of page