beanshell of scripting jmeter

When using the jmeter interface testing or performance testing, we need to deal with complex requests, then you need to use beanshell script, BeanShell is a fully compliant Java scripting language syntax specification, and they have some of their own grammar and the method, java, and it is seamless. beanshell due to built some unique variables, you can not go up in some integration code debugging tools. And then we used two code examples to share in today, it can be directly used under slightly modified can be used.
Bean Shell built-in variables Daquan
First we need to have some built-in variables and methods bean shell comes with its own. JMeter its built-in variable BeanShell user is interacting with JMeter these variables. 
1.log print journal, write information to jmeber.log file.
2.SampleResult get SampleResult object through the object to get the desired information.
3.Response Get Response object, this object can acquire response information.
4.Failure use to view the interface transfer success, if success is to return false, true failure.
5.FailureMessage failure information, the time is not set failure information is empty, this information can be set.
6.ResponseData Get response body type is byte [].
7.ResponseCode successfully returns an interface code is 200.
responseCode successfully returns an interface code is 200;
8.ResponseMessage get msg success is OK.
ResponseMessage get success msg is ok;
9.ResponseHeaders get an interface header information returned from the server.
ResponseHeaders get an interface header information returned from the server.
10.RequestHeaders obtaining header information requested by the client. RequestHeaders obtaining header information requested by the client
11.SampleLabel 获取接口请求的名称。 SampleLabel获取接口请求的名称
12.SamplerData 获取请求的url和body。 SamplerData获取请求的url和body
13.ctx 代表上下文信息,能直接用。 ctx代表上下文信息,能直接用
14.vars即JMeterVariables,操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),常用方法:
a) vars.get(String key):从jmeter中获得变量值;
b) vars.put(String key,String value):数据存到jmeter变量中;
15.prev 获取前面的sample返回的信息,常用方法:
a) getResponseDataAsString():获取响应信息。
b) getResponseCode() :获取响应code
 
实例:
签名函数
import java.util.*;
import Sign.*;
 
Signature signs = new Signature();
String appid = vars.get("login_appid");
String openid = vars.get("openid");
log.info(openid);
Map headers = signs.make_headers(appid);
Map url_params = new HashMap();
url_params.put("appId", appid);
url_params.put("openId", openid);
log.info(String.valueOf(headers));
String url ="https://" +vars.get("login_url")+vars.get("login_params");
log.info(url);
headers = signs.get_signature(url, "GET", url_params, headers);
log.info(String.valueOf(headers));
vars.put("h_appid", headers.get("X-QP-AppId"));
vars.put("h_timestamp", headers.get("X-QP-Timestamp"));
vars.put("h_nonce", headers.get("X-QP-Nonce"));
vars.put("h_signature", headers.get("X-QP-Signature"));
 

Guess you like

Origin www.cnblogs.com/summerxye/p/10975272.html