Rest Assured

download dependecny 


org.testng:testng

io.restassured 


----------------------------------------------------------------------------


Response response = RestAssured.given().when().get("posts").then().extract().response();

String value =  response.jsonPath().getList("title").get(0).toString();

System.out.println(value);



----------------------------------------------------------------------------


Authentication 


        given().auth().basic("","");

        given().auth().digest("","");

        given().auth().preemptive().basic("","");


        given().auth().oauth2("access token");


        given().header("Authorization","Bearer "+"sdasdasdsadas");

        given().queryParam("","");




----------------------------------------------------------------------------


given()

contentType, set cookies, add auth, param, set headsers 


when()

get, post,put,delete 


then()

validate status code, extract response, extract header cookies & response body 



----------------------------------------------------------------------------


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

    RestAssured.given().get("users?page=2").then().log().all()

            .assertThat().statusCode(200)

            .body("data[1].id", Matchers.equalTo(8));



----------------------------------------------------------------------------


    Response response = RestAssured.given().get("users?page=2");

    JsonPath jsonpath = new JsonPath(response.asString());


    int arr[] = {7,8,9,10,11,12};


    for(int i=0;i<jsonpath.getInt("data.size()");i++){

        int l = ((Integer) jsonpath.getInt("data[" + i + "].id"));

        Assert.assertEquals(l,arr[i]);

    }

----------------------------------------------------------------------------


1st way 


        Map<String,Object> payload = new HashMap<>();

        payload.put("name","morpheus");

        payload.put("job","leader");



=> 1 Response response = given().when().body(payload).post("users");

        System.out.println(response.asString());


=>2         Response response = given()

                .contentType("application/json")

                .body(payload)

                .when()

                .post("users")

                .then()

                .extract().response();


        System.out.println(response.asString());



2nd Way 


Create a pojoRequest Class, create variable (String name and String designation)

ALT+Insert to generate getter and setter for variables 


Create object of pojo in the test class and set value using setter method 


pojoRequest payload = new pojoRequest();

        payload.setName("raman");

        payload.setJob("leader");


3rd way 


Json.org 


JsonObject payload = new JsonObject;

obj.put("name","raman");


Response response = given().when().body(payload.toString).post("users");


4th Way 

External Json File 


----------------------------------------------------------------------------

Json Array 


        JSONArray jsary = new JSONArray(response.asString());

        System.out.println(jsary.getJSONObject(0).get("userId"));




        given()

                .when().get("posts")

                .then().statusCode(200)

                .body("userId[0]",equalTo(1))

                .body("title[0]",equalTo("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"));


------------------------------------------------------------------------------


API Chaining 


    @Test

    void testOne(ITestContext context) throws JSONException {


        int value =1;

        context.setAttribute("value",value);



another class 



context.setAttribute


create testng.xml file and execute 


-----------------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

Sort (Bubble) - Java

Rotate the String from position - Java