nGrinder 的 Groovy 脚本使用指南(HTTP 请求详解)
发布时间:2020-12-14 16:41:52 所属栏目:大数据 来源:网络整理
导读:请确定您已经了解了 nGrinder 的 Groovy 脚本结构:nGrinder 的 Groovy 脚本使用指南(Groovy 脚本结构) 在 nGrinder 上创建 Groovy 脚本时,会自动生成一个基础的脚本,让我们将其拆分说明 import HTTPClient.Cookie import HTTPClient.CookieModule impor
在 nGrinder 上创建 Groovy 脚本时,会自动生成一个基础的脚本,让我们将其拆分说明 import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
import static org.junit.Assert.assertThat
/** * * @author aub */
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1,"www.aub.com")
request = new HTTPRequest()
// 设置请求头数据
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("test_header","test_header_value"))
headers = headerList.toArray()
// 设置请求参数
List<NVPair> paramList = new ArrayList<NVPair>()
paramList.add(new NVPair("wd","nGrinder"))
params = paramList.toArray()
// 设置 cookie 信息
List<Cookie> cookieList = new ArrayList<Cookie>()
cookieList.add(new Cookie("username","aub","www.aub.com","/",new Date(32503647599000L),false))
cookies = cookieList.toArray()
// 记录日志
grinder.logger.info("before process.")
}
@BeforeThread
public void beforeThread() {
test.record(this,"test")
// 配置延迟报告统计结果
grinder.statistics.delayReports = true
// 记录日志
grinder.logger.info("before thread.")
}
@Before
public void before() {
// 设置本次请求头
request.setHeaders(headers)
// 设置本次请求的 cookies
cookies.each { CookieModule.addCookie(it,HTTPPluginControl.getThreadHTTPClientContext()) }
// 记录日志
grinder.logger.info("before thread. init headers and cookies")
}
@Test
public void test() {
HTTPResponse result = request.GET("http://www.baidu.com/s",params)
if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.",result.statusCode)
} else {
assertThat(result.statusCode,is(200))
}
}
}
静态变量说明public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []
初始化进程级数据使用 @BeforeProcess
public static void beforeProcess() {
// HTTP请求超时时间,单位毫秒
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1,"www.aub.com")
request = new HTTPRequest()
// 设置请求头数据
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("test_header","test_header_value"))
headers = headerList.toArray()
// 设置请求参数
List<NVPair> paramList = new ArrayList<NVPair>()
paramList.add(new NVPair("wd","nGrinder"))
params = paramList.toArray()
// 设置 cookie 信息
List<Cookie> cookieList = new ArrayList<Cookie>()
cookieList.add(new Cookie("username",new Date(32503647599000L),false))
cookies = cookieList.toArray()
// 记录日志
grinder.logger.info("before process.")
}
初始化线程级数据使用 @BeforeThread
public void beforeThread() {
test.record(this,"test")
// 配置延迟报告统计结果
grinder.statistics.delayReports = true
// 记录日志
grinder.logger.info("before thread.")
}
初始化测试级别数据使用 @Before
public void before() {
// 设置本次请求头
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("http://www.baidu.com/s",params)
if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.",result.statusCode)
} else {
assertThat(result.statusCode,is(200))
}
}
思考
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |