beanshell official website: http://www.BeanShell.org/
Introduce a .beanshell
- Is a fully compliant Java syntax specification lightweight scripting language;
- The equivalent of a small, free, embeddable Java source code interpreter, the source code can be run directly, without compiling, support object-like scripting language features;
- Implementation of standard Java statements and expressions ,, and some have their own syntax and methods;
1. Common manipulated variable
vars: BeanShell script uses " VARS" variables get () and put () method to access JMeter variables
- vars.get ( "name"): variable values obtained from jmeter defined in the variable name, GET (String Key) method only supports variable string values
- vars.put (String key, String value): value obtained in the "value" stored in the variable "key", jmeter reference variable parameter values obtained key value
- vars.putObject("obj",new Object()):用法同vars.put(String key,String value)方法
log: write information to jmeber.log file,
- log.info ( "This is log info!"): used to record log file, write to jmeber.log file, you can set the log level
prev: access to information sample returns for the assertion, associate at interfaces
- prev.getResponseDataAsString (): Get Information Response
- prev.getResponseCode (): Get the response code
2.beanshell usage
Custom Functions
In BeanShell, the java language may be used to write a particular logical combination of built-in objects BeanShell access variables,
引用外部java文件
在beanshel通过source("路径+文件名")方法引入java文件,路径使用两个\\,调用方法同java语法一样
引用外部class文件
用addClassPath("E:\\")方法引入 class文件,添加路径时使用“盘符:\\”即可,会自动搜索到class文件,用import导入包.类名,如下
引用外部Jar包
把jar包放到jmeter目录lib\ext下,或者直接引用jar包,如下图添加,用import导入包.类名
二.beanshell使用
1.定时器:BeanShell Timer
调用sleep()方法
2.前置处理器:BeanShell PreProcessor
可以对接口进行加密处理,如登录密码,加密后的字符串传入后端,后端也要有相应的解密处理
3.采样器:BeanShell Sampler
4.后置处理器:BeanShell PostProcessor
通过prev内置变量获取响应内容
- prev.getResponseDataAsString():获取响应信息
- prev.getResponseCode() :获取响应code
5.断言:BeanShell断言
通过Failure和FailureMessage来设置断言结果
- Failure = false; 表示断言成功
- Failure = true; 表示断言失败
- FailureMessage = "自定义返回断言结果消息";
6.监听器:BeanShell Listener
待续