Category:
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.
- 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.
Interface ResponseOptions 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)
// 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://hireqa.co.in/register");
// 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():"+responseTimeInSecon