- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
Advertisements