ngrinder groovy POST XML请求
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.apache.commons.lang3.RandomStringUtils; //导入方法依赖的package包/类 import groovy.sql.Sql @RunWith(GrinderRunner) class TestRunner { public static GTest test public static HTTPRequest request public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = [] public static ListLineList //存放参数文件记录 @BeforeProcess public static void beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 6000 test = new GTest(1, "10.248.64.58") request = new HTTPRequest() // 设置请求头header List headerList = new ArrayList () headerList.add(new NVPair("Content-Type", "text/xml")) headers = headerList.toArray() //连接数据库 Sql db= Sql.newInstance( "jdbc:oracle:thin:@10.248.46.46:1521:databaseName", "user", "password", "oracle.jdbc.driver.OracleDriver"); //获取查询数据 LineList= db.rows("select CUSTOMER_ID from cc_customer where create_time >= to_date('20220518','yyyy/mm/dd')").CUSTOMER_ID; grinder.logger.info("LineList_length={}",LineList.size()); 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()) } grinder.logger.info("before thread. init headers and cookies"); } @Test public void test(){ def gtime=new Date().format('yyyyMMddHHmmssSSS')//生成17位时间戳 int totalAgents=grinder.getProperties().getInt("grinder.agents",1); //总代理数 int total_processes=grinder.getProperties().getInt("grinder.processes",1); //总进程数 int total_threads=grinder.getProperties().getInt("grinder.threads",1); //总线程数 int Listindex=grinder.agentNumber*total_processes*total_threads + grinder.processNumber*total_threads + grinder.threadNumber + totalAgents * total_processes * total_threads * grinder.runNumber String customerID = LineList.get(Listindex).toString() //定义请求报文 String xmlbody=" ${customerID} " HTTPResponse result = request.POST("http://10.248.66.11:30051/csf_web", xmlbody.getBytes())//发起请求 grinder.logger.info("响应报文={}",result.getText()) //设置断言 if(result.StatusCode == 200){ assertThat(result.getText(), containsString("0000 Success ")); }else{ assertThat(result.statusCode, is(200)); } } }
关键脚本:
// 设置请求头header ListheaderList = new ArrayList () headerList.add(new NVPair("Content-Type", "text/xml")) headers = headerList.toArray()
String xmlbody="${customerID} " HTTPResponse result = request.POST("http://10.248.66.11:30051/csf_web", xmlbody.getBytes())//发起请求