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:-
- If you just want to retrieve response time in milliseconds or other time units, you need to use time(), getTime(), timeIn(TimeUnit timeunit), getTimeIn( TimeUnit timeunit ) from Response interface. Response interface inherits these methods from ResponseOptions. You can not use Matchers in above methods.
- If you want to use Matchers i.e. assertion like response time is greater than a specific value, you need to use overloaded time() methods from ValidatableResponse which inherits time() method from ValidatableResponseOptions interface.
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.
Interface ResponseOptions:-
This interface contains four methods :-
- getTime() – The response time in milliseconds (or -1 if no response time could be measured)
- getTimeIn(TimeUnit timeunit) – The response time in the given time unit (or -1 if no response time could be measured)
- time() – The response time in milliseconds (or -1 if no response time could be measured)
- timeIn( TimeUnit timeunit ) – The response time in the given time unit (or -1 if no response time could be measured)
Technically, getTime() and time() both are same and getTimeIn() and timeIn() both are same. Difference is Syntactic sugar.
Rest Assured Code:-
@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 = "{\"username\" : \"admin\",\"password\" : \"password123\"}"; // 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); }
Interface ValidatableResponseOptions :-
This interface has overloaded time() methods which accepts Matcher.
- time(Matcher matcher) – Validate that the response time (in milliseconds) matches the supplied
matcher
. - time(Matcher macther, TimeUnit timeunit) – Validate that the response time matches the supplied
matcher
and time unit.
Rest Assured Example:-
@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 = "{\"username\" : \"admin\",\"password\" : \"password123\"}"; // 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); }
Complete code:-
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 = "{\"username\" : \"admin\",\"password\" : \"password123\"}"; // 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 = "{\"username\" : \"admin\",\"password\" : \"password123\"}"; // 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
How can we print the time by using ValidatableResponse , if we don’t want to just validate but print
ValidatableResponse Interface is only to validate the values. So we cannot print any value using it.
Hi Sir can you explain oAuth1 and oauth2 and beared token authentications..please sir..