1、自动生成GET请求脚本
java
一、配置 Create a scriptweb
在ngrinder管理台主页,点击script–>Create a script,并填写脚本名称和请求的url,以下所示:数组
点击 Create 按钮,nGrinder会自动生成对应的脚本结构,若是没有参数须要设置的话,能够直接运行了。cookie
2、详细解析GET请求脚本app
ngrinder自动生成的脚本以下所示:dom
解析见代码中的注释ide
import static net.grinder.script.Grinder.grinder测试
import static org.junit.Assert.*this
import static org.hamcrest.Matchers.*url
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 对象,而后存放到相应的数组中,传递到请求中。