SDET- QA Automation Techie

Software Testing Blog

  • Home
  • Training
    • Online
    • Self-Paced
  • Video Tutorials
  • Interview Skills
    • HR Interview Questions Videos
    • Domain Knowledge
  • Career Guidance
  • Home
  • Software Testing
    • Manual Testing Tutorials
    • Manual Testing Project
    • Manaul Testing FAQS
    • ISTQB
    • AGILE
  • Web Automation Testing
    • Java Programmng
    • Python Programmng
    • Selenium with Java
    • Selenium with Python
    • Robot Framework(Selenium with Python)
    • selenium with Cucumber
    • TestNG+IntelliJ
    • Mobile App Testing(Appium)
    • JMeter
  • API Automation Testing
    • Rest Assured API Testing (BDD)
    • Rest Assured API Testing (Java+ TestNG)
    • Robot Framework(Rest API Testing with Python)
    • Postman
    • SoapUI
    • API Testing(FAQ's)
  • SDET|DevOps
    • Continuos Integration
    • SDET Essentials
    • AWS For Testers
    • Docker
  • SQL
    • Oracle(SQL)
    • MySQL for Testers
    • NoSQL
  • Unix/Linux
    • UNIX TUTORIALS
    • Linux Shell Scripting
  • ETL Testing
    • ETL Data warehouse Tutorial
    • ETL Concepts Tools and Templates
    • ETL Testing FAQ's
    • ETL Testing Videos
  • Big Data Hadoop
  • Video Tutorials
  • ApachePOI Video Tutorials
  • Downloads
    • E-Books for Professionals
    • Resumes
  • Automation Essencials
    • Cloud Technologies
      • Docker For Testers
      • AWS For Testers
      • Sub Child Category 3
    • Java Collections
    • Selenium Locators
    • Frequently Asked Java Programs
    • Frequently Asked Python Programs
    • Protractor
    • Cypress Web Automation

API/Webservices Testing using RestAssured (Part 1)

 API/Webservices Testing using RestAssured   




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");

}

}




  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home
popup

Popular Posts

  • How To Explain Project In Interview Freshers and Experienced
    “ Describe an important project you’ve worked on ” is one of the most common questions you can expect in an interview. The purpose of a...
  • MANUAL TESTING REAL TIME INTERVIEW QUESTIONS & ANSWERS
    1. How will you receive the project requirements? A. The finalized SRS will be placed in a project repository; we will access it fr...
  • 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.you...

Facebook Page

Pages

  • Home
  • Resumes
  • Job Websites India/UK/US
  • ISTQB
  • Selenium with Java
  • E-Books for Professionals
  • Manual Testing Tutorials
  • Agile Methodology
  • Manual Testing Projects

Live Traffic

YouTube


Blog Visitors

Copyright © SDET- QA Automation Techie | Powered by Blogger
Design by SDET | Blogger Theme by | Distributed By Gooyaabi Templates