在自動化測試時(shí)凶异,經(jīng)常會用到上一步接口(或sql查詢)的結(jié)果作為下一個(gè)測試節(jié)點(diǎn)的入?yún)ⅲ鞘褂胘meter做測試時(shí)如何傳參呢?
在上一篇建好的測試計(jì)劃的基礎(chǔ)上薪夕,我們來配置參數(shù)
首先 在jdbc request中配置好sql腳本入出參變量:
建一個(gè)java測試類,接收兩個(gè)參數(shù) state和suggest_operate
package com.jd.xc.jmeter;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class QlBlockerTest implements JavaSamplerClient {
private Integer state;
private String suggest;
public void setupTest(JavaSamplerContext javaSamplerContext){ }
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sampleResult = new SampleResult();
sampleResult.setSampleLabel("qlBlocker");
state = javaSamplerContext.getIntParameter("state");
suggest = javaSamplerContext.getParameter("suggest_operate");
sampleResult.sampleStart();
System.out.println("運(yùn)單當(dāng)前狀態(tài)"+state+" 攔截結(jié)果"+suggest);
sampleResult.setSamplerData("運(yùn)單當(dāng)前狀態(tài)"+state+" 攔截結(jié)果"+suggest);
sampleResult.setDataType(SampleResult.TEXT);
sampleResult.setResponseMessageOK();
sampleResult.setResponseCodeOK();
sampleResult.sampleEnd();
sampleResult.setSuccessful(Boolean.TRUE);
sampleResult.setResponseData("運(yùn)單當(dāng)前狀態(tài)"+state+" 攔截結(jié)果"+suggest,"UTF-8");
return sampleResult;
}
public void teardownTest(JavaSamplerContext javaSamplerContext) {
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("state","4");
params.addArgument("suggest","pass");
params.addArgument("suggest_operate","passOk");
return params;
}
}
將java類打成jar包jmeterTestCase-1.0-SNAPSHOT.jar,然后將jar包放到j(luò)meter的\lib\ext路徑下瘤礁,重啟jmeter
新建java請求取樣器 如下圖所示,在下拉框中選中我們創(chuàng)建的java類
修改參數(shù)值
這里的{b_1} 標(biāo)識取sql返回?cái)?shù)據(jù)的第1條梅尤,如下圖所示柜思,循環(huán)次數(shù)設(shè)置10次時(shí),java輸出的都是sql返回的第一條返回值
如果想循環(huán)獲取sql返回的每一個(gè)結(jié)果巷燥,需要添加循環(huán)控制器赡盘,將java請求拖動至循環(huán)控制器下,增加一個(gè)計(jì)數(shù)器
配置計(jì)數(shù)器參數(shù)缰揪,引用名我們隨便定義一個(gè) idx
如果單純的拼接參數(shù){idx}} {idx}} jmeter運(yùn)行會報(bào)錯(cuò)
需要我們利用函數(shù)助手V函數(shù) 綁定兩個(gè)變量
再點(diǎn)擊運(yùn)行查看java類返回值陨享,會發(fā)現(xiàn)打印的是sql查詢出來的每一個(gè)記錄