如何在 Rest Assured 中獲取請求的響應時間?
我們可以在 Rest Assured 中獲取請求的響應時間。傳送請求到伺服器,再收到響應所經過的時間稱為響應時間。
預設情況下,以毫秒為單位獲取響應時間。但是,我們也可以使用其他的時間單位。ResponseOptions 介面的以下方法可用於獲取響應時間 −
- getTime - 以毫秒為單位獲取響應時間。
- getTimeIn(time unit) - 以作為該方法的引數傳遞的時間單位獲取響應時間。
- time() - 以毫秒為單位獲取響應時間。
- timeIn(time unit) - 以作為該方法的引數傳遞的時間單位獲取響應時間。
示例
程式碼實現
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://tutorialspoint.tw/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);
}
}輸出

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP