Introduction
In this article, we will learn about other concepts such as how to send query param, path param, and logging.
Prerequisite
Rest Assured API Test automation setup needs to be done.
Query Param
QueryParam can be passed in the api request in the given section using the query param method.
Code example
String endpoint = "http://example.com/building";
var response = given()
.queryParam("globalDates", "{\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90}")
.queryParam("startFrom", "0").queryParam("limitTo", "6").queryParam("loadTvData", true)
.queryParam("startFrom", "0").queryParam("userId", "5834fb36981baacb6a876427")
.when().get(endpoint).then();
Path param
Path param can be sent for the api request using the path param method, which accepts the parameters.
Code example
Response response = RestAssured.given()
.pathParam("userId", userId)
.get(endpoint);
Authentication
if you have authentication for your API Request, then use auth method and handle your authentication.
//basic auth
Response response = RestAssured.given()
.auth()
.basic("username", "password")
.get("/endpoint");
//oauth authentication
Response response = RestAssured.given()
.auth()
.oauth2("your_token_here")
.get("/endpoint");
//bearer token and jwt token authentication
RestAssured.baseURI = "https://api.example.com";
// JWT token
String jwtToken = "your_jwt_token_here";
// Send a GET request with the JWT token
Response response = RestAssured.given()
.header("Authorization", "Bearer " + jwtToken)
.get("/endpoint");
Request headers
We can send the request headers in the given section of the api request
Request headers may contain path param, query param, authentication json payload, authentication, etc.
Response response = RestAssured.given()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer your_token_here")
.get("/endpoint");
Logging
Suppose we use log().all() after given, it will log all the request headers if we use log().all() after then section, it will log all responses.
Code example
RestAssured.baseURI="https://reqres.in/";
String requestBody = "{\n" + "\"email\": \"[email protected]\",\n"+
"\"password\": \"cityslicka\"\n"
+ "}";
Response res=given().
contentType(ContentType.JSON).
body(requestBody).
when().
post("api/login").
then().log().all()
.extract().
response();
Assert.assertEquals(200, res.statusCode());
}
Request Spec builder
RequestSpecBuilder is a class in Rest Assured, which contains methods to set cookies, headers, multipart details, body, authentication, form parameters, query parameters, path parameters, base path, base URI, proxy, etc. These all are required to construct a requestspecification. After adding all the required details, we need to use the “build()” method of RequestSpecBuilder class to get a RequestSpecification reference.
RequestSpecification helps us to reuse the request headers.
ResponseSpecBuilder
ResponseSpecBuilder class provides you with a builder to create a ResponseSpecification.
We can put the set of all the assertions that you are going to apply for all the api which you are going to test.
- Requestspec builder: It can be used if you have common request headers to be set for all the APIs which you are testing.
- Responsespec builder: It can be used if you want to apply common assertions to all the APIs which you are testing.
Code example with comments
@Test
public void getapi() {
// Creating an object of RequestSpecBuilder
RequestSpecBuilder reqBuilder = new RequestSpecBuilder();
// Setting Base URI
reqBuilder.setBaseUri("https://reqres.in/api/users/2");
// Getting RequestSpecification reference using builder() method
RequestSpecification reqSpec = reqBuilder.build();
// Create a ResponseSpecification using ResponseSpecBuilder
ResponseSpecBuilder responseSpecBuilder = new ResponseSpecBuilder();
responseSpecBuilder.expectStatusCode(200);
ResponseSpecification responseSpecification = responseSpecBuilder.build();
//refering the request spec object in the spec method in the given section
Response response = given()
.spec(reqSpec)
.when()
.get()
.then()
//refering the response spec object in the spec method in the then section
.spec(responseSpecification)
.extract().response();
//uisng the assert class in the testng i am putting assertion
Assert.assertEquals(200, response.statusCode());
Summary
I hope this article will be helpful in understanding how to write API automation tests using the rest assured library and different methods available in order to set query param path param, authentication, logging, request spec builder, and response spec builder.
Thanks, Happy learning.........