一、自動生成GET請求腳本
1、配置 Create a script
在ngrinder管理臺主頁,點擊script–>Create a script,並填寫腳本名稱和請求的url,如下所示:
點擊 Create 按鈕,nGrinder會自動生成對應的腳本結構,如果沒有參數需要設置的話,可以直接運行了。
二、詳細解析GET請求腳本
ngrinder自動生成的腳本如下所示:
解析見代碼中的註釋
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.FixMethodOrder
import org.junit.runners.MethodSorters
import java.util.Date
import java.util.List
import java.util.ArrayList
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
/**
* A simple example using the HTTP plugin that shows the retrieval of a
* single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestRunner {
public static GTest test
// 定義 HTTPRequest 靜態變量 request,用於發送 HTTP 請求
public static HTTPRequest request
// 定義 NVPair 數組 headers ,用於存放通用的請求頭數據
public static NVPair[] headers = []
// 定義 NVPair 數組 params ,用於存放請求參數數據
public static NVPair[] params = []
// 定義 Cookie 數組 cookies ,用於存放通用的 cookie 數據
public static Cookie[] cookies = []
@BeforeProcess
public static void beforeProcess() {
// 設置請求響應超時時間(ms),超過則拋出異常
HTTPPluginControl.getConnectionDefaults().timeout = 6000
// 創建GTest對象,第一個參數1代表有多個請求/事務時的執行順序ID
// 第二個參數是請求/事務的名稱,會顯示在summary結果中
// 有多個請求/事務時,要創建多個GTest對象
test = new GTest(1, "www.baidu.com")
// 創建 HTTPRequest 對象,用於發起 HTTP 請求
request = new HTTPRequest()
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
// 註冊事件,啓動test,第二個參數要與@Test註解的方法名保持一致
// 有多個請求/事務時,要註冊多個事件
test.record(this, "test")
// 配置延遲報告統計結果
grinder.statistics.delayReports=true;
grinder.logger.info("before thread.");
}
@Before
public void before() {
// 在這裏可以添加headers屬性和cookies
request.setHeaders(headers)
// 設置本次請求的 cookies
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before thread. init headers and cookies");
}
@Test
public void test(){
// 發送GET請求
HTTPResponse result = request.GET("https://www.baidu.com", params)
// 斷言HTTP請求狀態碼
assertThat(result.statusCode, is(200))
}
}
GTest
GTest是對測試記錄進行統計的單元;
使用 GTest 的構造方法 GTest(int number, String description) 創建,在腳本內每個GTest對象都使用唯一的編號定義。
如果創建了多個編號一樣的對象,最後只會選用第一個。
test.record()
在@BeforeThread註解下會執行 test.record(this, "test")
record(Object target, String methodName)
這裏使用 GTest的record方法給 GTest 配置需要進行統計的方法;
target 指腳本對象,這裏是this;
methodName 是需要統計的方法名,通常都是被 @Test 註釋的方法。如果未配置,方法會正常執行,但是沒有統計結果數據;
每一個被 @Test 註釋的方法都是一個整體事務,在運行測試時,即便裏面有 多次 HTTP 請求也只做一次統計!!
Cookies
可以通過 Cookie(String name, String value, String domain, String path, Date expires, boolean secure) 構造 cookie 對象,然後存放到相應的數組中,傳遞到請求中。