1、ngrinder自动生成 GET/POST请求脚本:
2、CSV文件存放至resources目录下
3、POST请求实例:
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 java.util.Date
import java.util.List
import java.util.ArrayList
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import org.slf4j.LoggerFactory
import ch.qos.logback.classic.Level
/**
* 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)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static String body
public static Cookie[] cookies = []
public static List<String> LineList //存放参数文件记录
public static def rowNumber //参数行
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "xxx.xx.xx.x")
request = new HTTPRequest()
// Set header datas
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("Content-Type", "application/json"))
headerList.add(new NVPair("charset", "UTF-8"))
headers = headerList.toArray()
/*读取csv文件内容*/
LineList = new File("./resources/queryPackOrderDetail.csv").readLines("GB2312")
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.each { CookieModule.addCookie(it,HTTPPluginControl.getThreadHTTPClientContext()) }
/**
* new Random().nextInt(lineList.size()) lineList.size()获取csv文件数据行数
* 为了避免获取到空的数据,建议将 lineList.size()替换为正整数,即csv文件中数据的行数.
*
* 如:buyerRowNumber = new Random().nextInt(10)
*/
rowNumber = new Random().nextInt(LineList.size())
/*获取csv文件中某一行的第一列数据*/
String MCode = LineList.get(rowNumber).toString().split(",")[0]
/*获取csv文件中某一行的第二列数据*/
String OCode = LineList.get(rowNumber).toString().split(",")[1]
//设置请求的入参--json格式
body = "{\"orderCode\":\"${OCode}\",\"memberCode\":\"${MCode}\"}"
grinder.logger.info("before thread. init headers and cookies");
}
@Test
public void test(){
HTTPResponse result = request.POST("http://xxx.xx.xx.x:xxxx/travelterminal/packOrder/queryPackOrderDetail", body.getBytes())
//println body
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));
}
}
}