

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to validate the response time of a request in Rest Assured?
We can validate 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. To validate the response time with Matchers, we need to use the below-overloaded methods of the ValidatableResponseOptions −
- time(matcher) - it verifies the response time in milliseconds with the matcher passed as a parameter to the method.
- time(matcher, time unit) - it verifies the response time with the matcher and time unit is passed as parameters to the method.
We shall perform the assertion with the help of the Hamcrest framework which uses the Matcher class for assertion. To work with Hamcrest we have to add the Hamcrest Core dependency in the pom.xml in our Maven project. The link to this dependency is available in the below link −
https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core
Example
Code Implementation
import org.hamcrest.Matchers; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; public class NewTest { @Test public void verifyResTime() { //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(); // obtain ValidatableResponse type ValidatableResponse v = res.then(); //verify response time lesser than 1000 milliseconds v.time(Matchers.lessThan(1000L)); } }
Output
- Related Questions & Answers
- How to get the response time of a request in Rest Assured?
- How to validate XML response in Rest Assured?
- Explain DELETE request in Rest Assured.
- Explain PUT request in Rest Assured.
- Validate JSON Schema in Rest Assured.
- How to use Assertion in response in Rest Assured?
- How to verify JSON response headers in Rest Assured?
- How to update the value of a field in a request using Rest Assured?
- How to transform the response to the Java list in Rest Assured?
- Explain how to get the size of a JSON array response in Rest Assured.
- How to parse a JSON response and get a particular field from the response in Rest Assured?
- How to extract the whole JSON response as a string in Rest Assured?
- How to verify a JSON response body using Assertions in Rest Assured?
- How to incorporate TestNG assertions in validating Response in Rest Assured?
- How to pass more than one header in a request in Rest Assured?
Advertisements