REST-assured- API Automation : Codes

 

1. Code for >> API Automation by Assertions&Parsing(array,map,Str,Int) the JSON response in REST-

      assured||Episode#3.0

2. Code for >> Read and Write Data into Excel in REST-assured || Episode#5

3.

 

 

 

1. Code for >> API Automation by Assertions&Parsing(array,map,Str,Int) the JSON response in REST-assured||Episode#3.0

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.ss.usermodel.CellType;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.json.JSONObject;

import org.testng.ITestResult;

import org.testng.Reporter;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

import io.restassured.response.Response;

import io.restassured.specification.RequestSpecification;

import junit.framework.Assert;

 

public class AssertionsinAPITesting {

@Test(enabled=true,priority=2)

public void getUsers_parsing_assertions() {

//https://reqres.in/api/users?page=2

RestAssured.baseURI="https://reqres.in/";

RequestSpecification req=RestAssured.given();

req.header("netflix_id", "32748732jfbfhu82jsdfs")

 .header("netflix_password", " jry8newkf84r4nfek")

 .header("stream", "NETFLIX");

req.auth().basic("username", "password");

//req.auth().preemptive().basic("username", "password");

req.contentType("application/json");

req.param("page", 2);

 Response resp=req.get("api/users");

 //resp.jsonPath().

 

System.out.println("getUsers response is:" + resp.asString());

System.out.println("getUsers response code is:" +resp.statusCode());

System.out.println("getUsers page is:" +resp.jsonPath().getChar("page"));  //2

System.out.println("getUsers data in list format is:" +resp.jsonPath().getList("data")); //array

System.out.println("getUsers data as JSONobject is:" +resp.jsonPath().getJsonObject("data")); //array

System.out.println("getUsers data as MAP is:" +resp.jsonPath().getMap("data[0]")); //first map in array

System.out.println("2nd element in data for getUsers is:" +resp.jsonPath().getList("data").get(1)); //2nd map in array

System.out.println("email of 3rd element in data for getUsers is:" + resp.jsonPath().getMap("data[2]").get("email"));

System.out.println("getUsers response contenttype is:"+resp.getContentType());

//Assertions

Assert.assertEquals(200, resp.statusCode());

Assert.assertEquals(2, resp.jsonPath().getInt("page"));

int var=resp.jsonPath().getInt("page");

Assert.assertTrue("Test is failed because page is not '2'...etc:",(var==2));

//validation similar to Assertion

int expected_result=12;

int actual_result=resp.jsonPath().getInt("total");

System.out.println("actual result is:"+actual_result);

if(expected_result==actual_result) {

Reporter.log("Test is Passed:as Expected result:"+expected_result+" match with Actual result:"+actual_result);

Reporter.getCurrentTestResult().setStatus(ITestResult.SUCCESS);

}else {

Reporter.log("Test is Failed:as Expected result:"+expected_result+" doesn't match with Actual result:"+actual_result);

Reporter.getCurrentTestResult().setStatus(ITestResult.FAILURE);

//Assert.fail("Test is Failed as Expected result:"+expected_result+" doesn't match with Actual result:"+actual_result);

}

}

}

 

 

2. Code for >> Read and Write Data into Excel in REST-assured || Episode#5

 

No comments:

Post a Comment