As a part of End to End REST Assured Tutorial , in this post We will learn about “Get and assert response time of Request in Rest Assured”.
When a request is sent to a server, it responds with a response. The amount of time taken between sending a request to server and retrieving a response back form a server is called Response Time. An API must be faster. As a part of API testing, we must check the response time as well.
REST Assured provides a convenient way to retrieve and assert response time. Rest Assured provides method to get response time in milliseconds by default or the time unit we want. We can also validate if response time is less than , greater than or in between expected value as well.
There is a tricky part here:-
Getting confused? I feel understanding hierarchy is very much important otherwise you may not use correct method from correct class. Anyway I will explain with examples.
This interface contains four methods :-
Technically, getTime() and time() both are same and getTimeIn() and timeIn() both are same. Difference is Syntactic sugar.
@Test public void mesaureResponseTimeUsingResponseOptionsMethods() < // There is no need to add escape character manually. Just paste string within double // quotes. It will automatically add escape sequence as required. String jsonString = ""; // Create a request specification RequestSpecification request= RestAssured.given(); // Setting content type to specify format in which request payload will be sent. // ContentType is an ENUM. request.contentType(ContentType.JSON); //Adding URI request.baseUri("https://restful-booker.herokuapp.com/auth"); // Adding body as string request.body(jsonString); // Calling POST method on URI. After hitting we get Response Response response = request.post(); // By default response time is given in milliseconds long responseTime1 = response.getTime(); System.out.println("Response time in ms using getTime():"+responseTime1); // we can get response time in other format as well long responseTimeInSeconds = response.getTimeIn(TimeUnit.SECONDS); System.out.println("Response time in seconds using getTimeIn():"+responseTimeInSeconds); // Similar methods long responseTime2 = response.time(); System.out.println("Response time in ms using time():"+responseTime2); long responseTimeInSeconds1 = response.timeIn(TimeUnit.SECONDS); System.out.println("Response time in seconds using timeIn():"+responseTimeInSeconds1); >
This interface has overloaded time() methods which accepts Matcher.
@Test public void mesaureResponseTimeUsingValidatableResponseOptionsMethods() < // There is no need to add escape character manually. Just paste string within // double // quotes. It will automatically add escape sequence as required. String jsonString = ""; // Create a request specification RequestSpecification request = RestAssured.given(); // Setting content type to specify format in which request payload will be sent. // ContentType is an ENUM. request.contentType(ContentType.JSON); // Adding URI request.baseUri("https://restful-booker.herokuapp.com/auth"); // Adding body as string request.body(jsonString); // Calling POST method on URI. After hitting we get Response Response response = request.post(); // Getting ValidatableResponse type ValidatableResponse valRes = response.then(); // Asserting response time is less than 2000 milliseconds // L just represent long. It is in millisecond by default. valRes.time(Matchers.lessThan(2000L)); // Asserting response time is greater than 2000 milliseconds valRes.time(Matchers.greaterThan(2000L)); // Asserting response time in between some values valRes.time(Matchers.both(Matchers.greaterThanOrEqualTo(2000L)).and(Matchers.lessThanOrEqualTo(1000L))); // If we want to assert in different time units valRes.time(Matchers.lessThan(2L), TimeUnit.SECONDS); >
package RestAssuredConcepts; import java.util.concurrent.TimeUnit; import org.hamcrest.Matchers; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; public class MeasuringResponseTimeInRestAssured < @Test public void mesaureResponseTimeUsingResponseOptionsMethods() < // There is no need to add escape character manually. Just paste string within double // quotes. It will automatically add escape sequence as required. String jsonString = ""; // Create a request specification RequestSpecification request= RestAssured.given(); // Setting content type to specify format in which request payload will be sent. // ContentType is an ENUM. request.contentType(ContentType.JSON); //Adding URI request.baseUri("https://restful-booker.herokuapp.com/auth"); // Adding body as string request.body(jsonString); // Calling POST method on URI. After hitting we get Response Response response = request.post(); // By default response time is given in milliseconds long responseTime1 = response.getTime(); System.out.println("Response time in ms using getTime():"+responseTime1); // we can get response time in other format as well long responseTimeInSeconds = response.getTimeIn(TimeUnit.SECONDS); System.out.println("Response time in seconds using getTimeIn():"+responseTimeInSeconds); // Similar methods long responseTime2 = response.time(); System.out.println("Response time in ms using time():"+responseTime2); long responseTimeInSeconds1 = response.timeIn(TimeUnit.SECONDS); System.out.println("Response time in seconds using timeIn():"+responseTimeInSeconds1); > @Test public void mesaureResponseTimeUsingValidatableResponseOptionsMethods() < // There is no need to add escape character manually. Just paste string within // double // quotes. It will automatically add escape sequence as required. String jsonString = ""; // Create a request specification RequestSpecification request = RestAssured.given(); // Setting content type to specify format in which request payload will be sent. // ContentType is an ENUM. request.contentType(ContentType.JSON); // Adding URI request.baseUri("https://restful-booker.herokuapp.com/auth"); // Adding body as string request.body(jsonString); // Calling POST method on URI. After hitting we get Response Response response = request.post(); // Getting ValidatableResponse type ValidatableResponse valRes = response.then(); // Asserting response time is less than 2000 milliseconds // L just represent long. It is in millisecond by default. valRes.time(Matchers.lessThan(2000L)); // Asserting response time is greater than 2000 milliseconds valRes.time(Matchers.greaterThan(2000L)); // Asserting response time in between some values valRes.time(Matchers.both(Matchers.greaterThanOrEqualTo(2000L)).and(Matchers.lessThanOrEqualTo(1000L))); // If we want to assert in different time units valRes.time(Matchers.lessThan(2L), TimeUnit.SECONDS); > >
You can clone/download example repo here.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here