Sunday, August 22, 2021

Post#139-API#8 Rest Assured GET Test

 👇

Before we create our REST Assured tests, make sure that you have added Rest Assured library and TestNG library in your Maven project.

In this tutorial I am using below URL for different request.

https://reqres.in/

Below code snippet shows how to call GET method and test response JSON elements.

package testcase2;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

import io.restassured.response.Response;

 public class GETDemo {

  @Test

  public void simple_get_test() {

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

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

                             System.out.println("Response status code - "+response.getStatusCode());

                             System.out.println("Response - "+response.prettyPrint());

  }

}

O/P:

Response status code - 200

{

    "page": 2,

    "per_page": 6,

    "total": 12,

    "total_pages": 2,

    "data": [

        {

            "id": 7,

            "email": "michael.lawson@reqres.in",

            "first_name": "Michael",

            "last_name": "Lawson",

            "avatar": "https://reqres.in/img/faces/7-image.jpg"

        },

        {

            "id": 8,

            "email": "lindsay.ferguson@reqres.in",

            "first_name": "Lindsay",

            "last_name": "Ferguson",

            "avatar": "https://reqres.in/img/faces/8-image.jpg"

        }

    ],

    "support": {

        "url": "https://reqres.in/#support-heading",

        "text": "To keep ReqRes free, contributions towards server costs are appreciated!"

    }

}

 

From this above json response we can validate our expected data. I will post how to parse json response on my later posts.

 

Code Explanation:

RestAssured.baseURI

RestAssured – Class

baseURI – static field

The base URI that's used by REST assured when making requests if a non-fully qualified URI is used in the request. Default value is "http://localhost".

RestAssured.get() -

Perform a GET request to a path. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as https://reqres.in/api/users?page=2. In this case it's enough to use /api/users?page=2. The GET method will concatenate baseURI and given Path.

getStatusCode() method is used to get the http status code returned  by the server.

Respnse - Interface

prettyPrint() - Method

Pretty-print the response body if possible and return it as string. Mainly useful for debug purposes when writing tests. Pretty printing is possible for content-types JSON, XML and HTML.


🙏

No comments:

Post a Comment