如何在 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);
   }
}

輸出

更新於: 2021 年 11 月 18 日

超過 6K 次瀏覽

開啟 職業生涯

完成課程以獲得認證

開始入門
廣告
© . All rights reserved.