nGrinder中快速编写groovy脚本02-解读最基本的GET请求脚本

一、自动生成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 对象,然后存放到相应的数组中,传递到请求中。

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...