(一)BeanShell简介html
BeanShell是一个小型嵌入式Java源代码解释器,具备对象脚本语言特性,可以动态地执行标准JAVA语法,并利用在JavaScript和Perl中常见的的松散类型、命令、闭包等通用脚原本对其进行拓展。BeanShell不单单能够经过运行其内部的脚原本处理Java应用程序,还能够在运行过程当中动态执行你java应用程序执行java代码。由于BeanShell是用java写的,运行在同一个虚拟机的应用程序,所以能够自由地引用对象脚本并返回结果。java
BeanShell通常使用在逻辑比较复杂的地方shell
(二)Beanshell特色apache
·完整的java语法的动态执行,java代码片断,以及松散类型的java和其余脚本。小程序
·透明地访问全部java对象和APIapi
·运行在四种模式:命令行,控制台,小程序,远程会话服务器。安全
·能够在安全约束的环境中工做服务器
·编译器是一个小于150K的jar文件闭包
(三)BeanShell的六个种类函数
定时器: BeanShell Timer
前置处理器:BeanShell PreProcessor
采样器: BeanShell Sampler
后置处理器:BeanShell PostProcessor
断言: BeanShell断言
监听器: BeanShell Listener
(四)Bean Shell经常使用内置变量
log(Logger):打印日志信息,写入信息到jmeber.log文件,
使用方法可参考:log.info("info")可参考:http://excalibur.apache.org/apidocs/org/apache/log/Logger.html
ctx(JMeterContext):该变量引用了当前线程的上下文,
使用方法可参考:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html
vars(JMeterVariables):操做jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,经常使用方法:
1).vars.get(String key):从jmeter中得到变量值
2). vars.put(String key,String value):数据存到jmeter变量中
使用方法可参考:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
props(JMeterProperties):操做jmeter属性,该变量引用了JMeter的配置信息,能够获取Jmeter的属性,它的使用方法与vars相似,可是只能put进去String类型的值。
1) props.get("START.HMS"); 注:START.HMS为属性名,在文件jmeter.properties中定义
2) props.put("PROP1","1234");
prev(SampleResult):获取前面的sample返回的信息,经常使用方法:
1) getResponseDataAsString():获取响应信息
2) getResponseCode() :获取响应code
使用方法可参考:http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
sampler(Sampler):gives access to the current sampler
使用方法可参考:http://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html
threadName - String containing the current thread name
(五)BeanShell使用(以BeanShell PreProcessor为例)
1)运行自定义函数
2)调用外部Java文件, source("D\\workspace\\TelephoneNumber.java")
3)导入本身打出的jar包,放到lib下的ext文件夹中,再导入类路径调用,以下图:
(六)其它用法:
一、在Test Plan中定义以下三个变量:
二、Bean Shell可脚本以下:
a、bean shell能够接受传入参数,以下图:${u1} ${u2} ${u3}
b、参数能够经过bsh.args[]按顺序提取
c、bean shell提供了一个内置变量Parameters,来保存参数的集合
(以上把变量存入参数中的方式能够这样写:vars.put("ABCEEFG","${sid}"); 结果:ABCEEFG=70695742 )