How to get the response time of a request in Rest Assured?

Rest AssuredDynamic ProgrammingProgramming

We can get the response time of a request in Rest Assured. The time elapsed after a request is sent to the server and then receiving the response is known as the response time.

The response time is obtained in milliseconds by default. However, we can also obtain in other time units. The below methods of the ResponseOptions interface can be used to get the response time −

  • getTime - it gets the response time in milliseconds.
  • getTimeIn(time unit) - it gets the response time in the time unit passed as a parameter to this method.
  • time() - it gets the response time in milliseconds.
  • timeIn(time unit) - it gets the response time in the time unit passed as a parameter to this method.

Example

Code Implementation

import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class NewTest {
   @Test
   public void responsetime() {
      //base URI with Rest Assured class
      RestAssured.baseURI ="https://www.tutorialspoint.com/index.htm";
      //input details
      RequestSpecification r = RestAssured.given();
      // GET request
      Response res = r.get();
      //obtain Response as string
      String j = res.asString();
      //get response time
      long c = res.getTime();
      System.out.println("Response time in milliseconds: " + c);
   }
}

Output

raja
Updated on 18-Nov-2021 10:57:35

Advertisements