0
点赞
收藏
分享

微信扫一扫

Beanshell相关

那小那小 2022-05-02 阅读 53
功能测试

目录

一、Beanshell预处理器(BeanShell PreProcessor)

二、Beanshell取样器(BeanShell Sampler)

三、BeanShell 后置处理程序(BeanShell PostProcessor)

四、断言

补充知识点

JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下:

log:写入信息到jmeter.log文件,使用方法:log.info(“hello world!”);

ctx:该变量引用了当前线程的上下文,使用方法可参考:org.apache.jmeter.threads.JMeterContext。

vars - (JMeterVariables):操作jmeter变量,常用方法:

    a) vars.get(String key):从jmeter中获得变量值

    b) vars.put(String key,String value):数据存到jmeter变量中

  更多方法可参考:org.apache.jmeter.threads.JMeterVariables

props - (JMeterProperties - class java.util.Properties):操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值,而不能是一个对象。对应于java.util.Properties。

    a) props.get("START.HMS");  注:START.HMS为属性名,在文件jmeter.properties中定义

    b) props.put("PROP1","1234");

prev - (SampleResult):一般用于前置处理器中,获取前面sample返回的信息,常用方法:

    a) prev.getResponseDataAsString():获取前面sample的响应信息

    b) prev.getResponseCode():获取前面sample的响应code

更多方法可参考:https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html

一、Beanshell预处理器(BeanShell PreProcessor)

位置:添加-前置处理器-BeanShell PreProcessor

一般用于提取前面样本的响应信息,来作对比或者可以使用,同时它也可以在执行请求之前添加一些用到的参数,数据等。提前处理请求参数,如:加密解密

vars.put("Resultcxt",prev.getResponseDataAsString()); //获取上面的请求响应内容,赋值给"Resultcxt"

vars.put("resultcode",prev.getResponseCode());//获取上面的请求响应码,赋值给"resultcode"

vars.get("Resultcxt"); vars.get("resultcode");

在调试取样器中能查看到这两个变量的值。

二、Beanshell取样器(BeanShell Sampler)

位置:添加-取样器-BeanShell Sampler

可以对数据进行一些处理,如:获取当前时间之后,用beanshell对时间进行处理,再在其它地方引用这个时间

import java.text.SimpleDateFormat; 
import java.util.*; 
String str1 = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); 
String str2 = new SimpleDateFormat("HH: mm:ss.SSS").format(new Date()); 
String str3 = new SimpleDateFormat("HH:mm:ss").format(new Date( )); vars.put("str0",strl+"T"+str3+".000"+"对时间进行了处理"); 
vars.get("str0") ;

运行结果:

三、BeanShell 后置处理程序(BeanShell PostProcessor)

一般用于对响应数据处理,比如要把响应数据保存到本地或者一些其它用途。

//String Resultcxt=prev.getResponseDataAsString(); 
//log.info(Resultcxt); 

vars.put("Resultcxt",prev.getResponseDataAsString()); 
xixi=vars.get("Resultcxt"); 
log.info(xixi); 
String resultcode=prev.getResponseCode(); 
log.info(resultcode);

注意有个特殊用法:

参数传递,这些参数放置在bsh.args[]数组中,用空格隔开

一次性将所有参数输出,使用Parameters

  • 参数为常量:
log.info("第一个变量值:"+bsh.args[0]); 
log.info("第二个变量值:"+bsh.args[1]); 
log.info("第三个变量值:"+bsh.args[2]); 
log.info("第四个变量值:"+bsh.args[3]);

运行结果:

  • 参数为变量:

在测试计划中定义全局变量:

log.info("第一个变量值:"+bsh.args[0]); 
log.info("第二个变量值:"+bsh.args[1]); 
log.info("第三个变量值:"+bsh.args[2]); 
log.info("第四个变量值:"+bsh.args[3]); 
log.info("所有的参数为:"+Parameters);

运行结果:

四、断言

BeanShell断言中可以通过ResponseCode、ResponseHeaders、prev.getResponseDataAsString()来分别获得String格式的响应状态码、响应头、响应体数据。

通过变量Failure=false或Failure=true来设置断言是否通过。当设置Failure=true时,还可以设置FailureMessage来设置失败原因

url=prev.getUrlAsString(); 
Resultcxt=prev.getResponseDataAsString(); 
resultcode=prev.getResponseCode(); 
log.info(url); log.info(Resultcxt); 
log.info(resultcode); 
log.info("!!!!!!!响应状态码:"+ResponseCode); 
log.info("!!!!!!!响应头信息:"+ResponseHeaders); 
log.info("!!!!!!!响应体数据:"+Resultcxt); 

//响应体包含某些特定数据可表示:Resultcxt.contains("登录success") 
//此处注意,比较字符串用equals,不用==,==的作用是判断对象的内存地址是否一致 
if(resultcode.equals("200")){ 
    Failure=false; //表示断言成功 
    FailureMessage="登录成功"; //自定义的成功信息 
}
else{ 
    Failure=true; //表示断言失败 
    FailureMessage="登录失败"; //自定义的失败信息 
}

如何断言正确,则运行结果与正常请求一样;

如果断言失败,则会出现断言失败的提示:

添加-监听器-断言结果:

补充知识点

JSON响应体字段提取及断言。

将String类型的响应体转为JSON对象并操作需要额外的JAR包,测试计划中导入此jar包。

  • 取响应体中某个json字段:

取token:

import org.json.*;
//import org.json.JSONObject; 可以这样写,处理json对象
//import org.json.JSONArray;处理json数组

//rescxt为返回String类型的响应体
String rescxt=prev.getResponseDataAsString();
log.info(rescxt);
 
//转为JSONObject对象
JSONObject json1 = new JSONObject(rescxt);

//String token1=json1.get("Access-Token").toString();
//有一点:String token1=json1.get("A").get("B").toString();
//如果要获取响应体中的json数组:
String token1=json1.getString("Access-Token");
String code1=json1.getString("code");
log.info(token1);
log.info(code1);

if(code1.equals("201")){
	Failure=false;
	FailureMessage="登录成功";
}
else{
	Failure=true;
	FailureMessage="登录失败";
}

  • 当响应体中有数组,取数组中的字段:

取数组中的id/name:

举报

相关推荐

0 条评论