ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-05-09 21:53:38  阅读:263  来源: 互联网

标签:脚本 groovy grinder 请求 02 GTest static import junit


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

标签:脚本,groovy,grinder,请求,02,GTest,static,import,junit
来源: https://blog.csdn.net/xqtesting/article/details/90048546

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有