Sunday, April 11, 2021

Rest API Automation | Rest API | How to automate API using Rest Assured with JAVA wih GET method

 API in simple words is bridge between 2 applications. an simple example of an API is when we book an rail ticket from any platform (Amazon or Paytm) we have noticed that after entering the user details it redirect us to the IRCTC website. so here that process which negivate us to the IRCTC is done threw the API.

There are  types of API:

1) SOAP API

2) Rest API

now, to validate any API there are many tools available in market like postman, or swagger. but here i am showing you an example how can we automate Rest API using Rest Assured.

To  create a setup there is 3 steps which we need to follow

1) Create and Maven Project (File > New > Others > Maven project)


Step 2 : After creating Maven project. go to the pom.xml file and add these dependencies in it

Add these dependencies in pom.xml file in dependencies tag

password for this file : 11223344

Step 3 : After creating maven project and adding these dependencies in project, create an TestNG class.

Now, after creating setup first thing we need to do is to setup and base URI of the API

@Test
	
	public void getData()
	{
		
		RestAssured.baseURI="https://restcountries.eu/rest/v2/alpha/";

here i have added a dummy API which you can use too.

After creating the baseURI, now we need to create 2 objects: Request object and Response object

//creating request object
		
	RequestSpecification request = RestAssured.given();
	
	//creating response object
	
	Response response = request.request(Method.GET,"ind");

while creating response object we need to select a method and the path of the API.

Now, we have created the request object and response object now we can fetch the response.

	String responsedata=response.getBody().asString();
	
	System.out.println("Request data is==> "+responsedata);
	
	int status_code=response.getStatusCode();
	
	System.out.println("response code is==>"+status_code);

here, we can play with the response object and perform any validation which we need.


I hope i am able to help you with this. please share with your friends and collogues as well :)  

No comments:

Post a Comment