Thursday, June 26, 2014

REST API Testing using REST-assured

This is my second post about doing Automation of REST API Testing, few days back I write the post to do REST API Testing using Frisby.js ( a Javascript tool), today i will post about how to do this in JAVA using REST-assured API.

Installation
Please down and put following JAR files in your build path. 
rest-assured
json-schema-validator

Test Case. 
For this demo purpose, I am going to use freegeoip.net site which return simple JSON response. 
steps: 
1. visit http://freegeoip.net:80/json/yahoo.com site 

verification:
1. verify that it returns 200 OK 
2. verify it return correct JSON 

Test Script. 
/**
 * @author Gaurang_Shah
 *
 */

//import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.baseURI;
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.useRelaxedHTTPSValidation;
import static com.jayway.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Test;

import com.jayway.restassured.authentication.FormAuthConfig;

public class TestATP {

 
 @Test
 public void testDemo(){
  baseURI="http://freegeoip.net:80/";
  when().
    get("/json/yahoo.com"). 
  then().
    assertThat().statusCode(200).
    log().all().
    body("country_code",equalTo("US"),
      "country_name",equalTo("United States"),
      "region_code",equalTo("CA"),
      "region_name",equalTo("California"),
      "city",equalTo("Sunnyvale")
      
     );
 
 }
}

Friday, May 30, 2014

REST API Testing using Frisby.js

Couple of days back I was searching ways to Automate REST API, I was searching for tool which is open source, free and has good support. The first thing which I came across is Frisby.js

Frisby.Js  is a framework for testing REST API built on node.js. (Yes, that means you will have use javascript)

Pros:
  • It's fast and easy to use. 
  • It generated JUnit XML report.
Cons
  • Doesn't have much facilities
  • very limited support group. 
However't that's what I feel, you can explore this and can comment if feels other wise. Now let me show you how to install it. 

Installation 

  • First install the node.js from following location
    http://nodejs.org/download/
  • Now go the command line and install the frisby.js using npm (node package manager)
    npm install -g frisby
  • You will also require to install jasmine as it is being used by frisby.js, Goto the command line and install jasmine using following command.
    npm install -g jasmine-node
  • Now all is set, you can go ahead and create the test case. 

Write Test Cases

  • Now firsby.js is using jasmine, name of the test case file must end with spec.js.
  • You also need to create the folder named spec and then folder names api inside that. 
  • Now copy the following file in spec/api.
  • Please not that, the first line is the location of the code may change depends where frisby module is located on your local machine. 
getHostDetails_specs.js.

In following test cases, we will get the details for yahoo.com site using freegeoip.com and then verify the JSON reponse. 
var frisby = require('C:/Users/gaurang_shah/AppData/Roaming/npm/node_modules/frisby');

frisby.create('Get IP From Host Name')
  .get('http://freegeoip.net/json/yahoo.com')
  .expectStatus(200)
  .expectHeaderContains('content-type', 'application/json')
  .inspectJSON()
  .expectJSON({
  country_code:"US",
  country_name:"United States",
  region_code: 'CA',
  region_name: 'Californ',
  city: 'Sunnyvale',
  zipcode: '94089',
  latitude: 37.4249,
  longitude: -122.0074,
  metro_code: '807',
  area_code: '408' 
  })
 .toss();

Run the Test Cases

  • open the command prompt and make sure you are in the parent directory of spec/api folder. 
  • Enter the following command
    jasmine-node spec\api\getHostDetails_spec.js
  • If you have multiple spec file and if you want to run all, then you can run using following command
    jasmine-node spec/api

Results & Reports 

  • Above test case will fail, as we are expecting region_name to be Californ and actual is California.
  • To Generate the Junit report, you just need to run the test cases using following command
    jasmine
    -node spec/api/ --junitreport