API/Webservices Testing using RestAssured (Part 1)


Rest Assured : Is an API designed for automating REST services/Rest API's

Pre-Requisites

Java
Free videos: https://www.youtube.com/watch?v=ms3NggvNW40&list=PLUDwpEzHYYLv9v8aRuNi67vZ81cW2ksze
Eclipse

TestNG : Framework implemented on top of Java used for organizing test cases, test suites.
Free videos: https://www.youtube.com/playlist?list=PLUDwpEzHYYLsWENabqeETzgPLbmwqhM45
Maven

Setup

1) Creating Maven project in Eclipse
2) We need to update pom.xml with required dependencies

- RestAssured  https://mvnrepository.com/artifact/io.rest-assured/rest-assured
- TestNG       https://mvnrepository.com/artifact/org.testng/testng
- Json-simple  https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
- apache poi   https://mvnrepository.com/artifact/org.apache.poi/poi
       https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml



Test Case 1) Weather API - Validate status code & Status line

http://restapi.demoqa.com/utilities/weather/city/

Request Type: GET

http://restapi.demoqa.com/utilities/weather/city/Hyderabad

SUCCESS RESPONSE
{
“City”: “Hyderabad”,
“Temperature”: “31.49 Degree celsius”,
“Humidity”: “62 Percent”,
“Weather Description”: “scattered clouds”,
“Wind Speed”: “3.6 Km per hour”,
“Wind Direction degree”: “270 Degree”
}

STATUS CODE : 200
Status Line: HTTP/1.1 200 OK"


Code Snippet

import org.testng.Assert;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class TC001_GET_Request {

@Test
void getweatherDetails()
{
//Specify base URI
RestAssured.baseURI="http://restapi.demoqa.com/utilities/weather/city";
//Request object
RequestSpecification httpRequest=RestAssured.given();
//Response object
Response response=httpRequest.request(Method.GET,"/Hyderabad");
//print response in console window
String responseBody=response.getBody().asString();
System.out.println("Response Body is:" +responseBody);
//status code validation
int statusCode=response.getStatusCode();
System.out.println("Status code is: "+statusCode);
Assert.assertEquals(statusCode, 200);
//status line verification
String statusLine=response.getStatusLine();
System.out.println("Status line is:"+statusLine);
Assert.assertEquals(statusLine, "HTTP/1.1 200 OK");
}
}


Test Case 2) Register Customer API

Request Type: POST
http://restapi.demoqa.com/customer/register

BODY

{
   “FirstName” : “value”
   “LastName” : “value”,
   “UserName” : “value”,
   “Password” : “value”,
   “Email”        : “Value”
 }

SUCCESS RESPONSE

{
“SuccessCode”: “OPERATION_SUCCESS”,
“Message”: “Operation completed successfully”
}

STATUS CODE : 201

Code Snippet

import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class TC002_POST_Request {

@Test
void RegistrationSuccessful()
{

//Specify base URI
RestAssured.baseURI="http://restapi.demoqa.com/customer";

//Request object
RequestSpecification httpRequest=RestAssured.given();


//Request paylaod sending along with post request
JSONObject requestParams=new JSONObject();

requestParams.put("FirstName","JohnXYZ");
requestParams.put("LastName","XYZJohn");
requestParams.put("UserName","JohnXYZ");
requestParams.put("Password","JohnXYZxyx");
requestParams.put("Email","JohnXYZ@gmail.com");

httpRequest.header("Content-Type","application/json");

httpRequest.body(requestParams.toJSONString()); // attach above data to the request

//Response object
Response response=httpRequest.request(Method.POST,"/register");


//print response in console window

String responseBody=response.getBody().asString();
System.out.println("Response Body is:" +responseBody);

//status code validation
int statusCode=response.getStatusCode();
System.out.println("Status code is: "+statusCode);
Assert.assertEquals(statusCode, 201);

//success code validation
String successCode=response.jsonPath().get("SuccessCode");
Assert.assertEquals(successCode, "OPERATION_SUCCESS");

}


}

Test Case 3) Google Map API - Validating Headers

https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=-33.8670522,151.1957362&radius=1500&type=supermarket&key=AIzaSyBjGCE3VpLU4lgTqSTDmHmJ2HoELb4Jy1s

SUCCESS RESPONSE : Returns list of super markets

"Headers:
Content-Encoding →gzip
Content-Type →application/xml; charset=UTF-8
Server →scaffolding on HTTPServer2
"

Code Snippet

import org.testng.Assert;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class TC003_GET_Request {

@Test
void googleMapTest()
{

//Specify base URI
RestAssured.baseURI="https://maps.googleapis.com";

//Request object
RequestSpecification httpRequest=RestAssured.given();

//Response object
Response response=httpRequest.request(Method.GET,"/maps/api/place/nearbysearch/xml?location=-33.8670522,151.1957362&radius=1500&type=supermarket&key=AIzaSyBjGCE3VpLU4lgTqSTDmHmJ2HoELb4Jy1s");

//print response in console window
String responseBody=response.getBody().asString();
System.out.println("Response Body is:" +responseBody);

//validating headers
String contentType=response.header("Content-Type");// capture details of Content-Type header
System.out.println("Content Type is:"+contentType);
Assert.assertEquals(contentType, "application/xml; charset=UTF-8");

String contentEncoding=response.header("Content-Encoding");// capture details of Content-Encoding  header
System.out.println("Content Encoding is:"+contentEncoding);
Assert.assertEquals(contentEncoding, "gzip");

}

}




Followers