首先新建一个java工程,然后在scr下新建一个cn.com.functions 包,在eclipse中导入二次开发中需要的jar包
新建一个class :String1Encrypt.java 继承 AbstractFunction 类
package cn.com.functions;// 这里是包名,是固定的写法
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class String1Encrypt extends AbstractFunction {
private static final List<String> desc = new LinkedList();
private static final String KEY1 = "__String1_Encrypt";//这里是函数名,也就是在函数助手列表中显示的名称
private Object[] values;
private static final String KEY = "xxxxxxxxx"; //声明密匙,根据需要进行修改,当然也可以讲该参数封封装在下面的static中,作为一个可输入的选项
static {
desc.add("String to be encrypted or decrypted:");//待加密或者解密的字符串
desc.add(" must input :1 encryption, 2 decryption ");//这里是选项,1代表加密2代表解密
desc.add("Name of variable in which to store the result (optional)");
}//这里是声明一个变量用于在jmeter中引用,可选的
//static的内容就是在选择函数助手中的函数后显示的内容
private static final String OFFSET = "Con-ere-ceD-cumt";
private static final String ENCODING = "UTF-8";
private static final String ALGORITHM = "AES";
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
String signString = "缺少参数";//这里定义字符串,如果未输入2个参数,则返回缺少参数,方便调试
//加密方法
public static String encrypt(String data) throws Exception {
IvParameterSpec iv = new IvParameterSpec("Con-ere-ceD-cumt".getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec("e5b40d286cxfca34".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(1, skeySpec, iv);
byte[] encrypted = cipher.doFinal(data.getBytes());
return (new BASE64Encoder()).encode(encrypted);
}
//解密方法
public static String decrypt(String data) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec skeySpec = new SecretKeySpec("e5b40d286cxfca34".getBytes("UTF-8"), "AES");
IvParameterSpec iv = new IvParameterSpec("Con-ere-ceD-cumt".getBytes("UTF-8"));
cipher.init(2, skeySpec, iv);
byte[] buffer = (new BASE64Decoder()).decodeBuffer(data);
byte[] encrypted = cipher.doFinal(buffer);
return new String(encrypted, "utf-8");
}
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
//这里是对函数中的处理逻辑,想实现什么,都可以在这里进行编写
JMeterVariables vars = getVariables();//声明一个jmeter的变量vars
String str = ((CompoundVariable) this.values[0]).execute().replaceAll("\\s", "");
//通过this.values[0]的方法即可取出static 中第一个输入的字符串并进行处理。去掉空格、制表符等内容
try {
if (this.values.length >= 2) { //判断是否输入了2个选项。由于第三个选项时可选的,所以这里设置为2
//通过this.values[1]方法取出static中第二个输入的内容
String appendStr = ((CompoundVariable) this.values[1]).execute().trim();
//判断输入的内容是否是1,或者是true如果是,则调用加密函数进行加密并赋值给this.signString,反之则进行解密
if ("1".equals(appendStr) || "true".equals(appendStr)) {
this.signString = encrypt(str).replaceAll("\\s", "");
}
if ("2".equals(appendStr) || "false".equals(appendStr)) {
this.signString = decrypt(str).replaceAll("\\s", "");
}
}
//这里是判断是否输入 了变量,即static中第三个字符串,如果输入了,则将该变量指向加密或解密的字符串
if (vars != null && this.values.length > 1) {
String varName = ((CompoundVariable) this.values[2]).execute().trim();
vars.put(varName, this.signString);
}
} catch (Exception e) {
this.signString = "算法无法解密,请核对***********" ;
}//此处捕获了异常并进行处理
return this.signString; //返回加密或者解密的字符串
}
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
checkMinParameterCount(parameters, 2);
//获取输入 的参数并赋值给this.values,这里可以看出函数助手输入的字符串会以一个列表的方式返回
this.values = parameters.toArray();
}
public String getReferenceKey() {
return "__String1_Encrypt"; //返回函数名,这里都是固定的写法,唯一要变的就是return 的内容
}
public List<String> getArgumentDesc() {
return desc; //返回 static中声明的内容
}
}
如果对python自动化测试、web自动化、接口自动化、移动端自动化、面试经验交流等等感兴趣的测试人,可以 点这自行获取…
将写好的代码导入jmeter中进行调试
选择jarfile
这里需要注意的是一定要导入\lib\ext目录下,否则jmeter函数助手是看不到导入的函数名称的
启动jmeter后,在这里即可看到已经开发好的函数
开发过程中,如果需要进行调试,可以加入打印语句,并在jmeter启动后台看到打印结果
参考资料:jmeter官方函数https://github.com/ufctester/apache-jmeter/tree/master/src/functions/org/apache/jmeter/functions
最后: 可以在公众号:伤心的辣条 ! 自行领取一份216页软件测试工程师面试宝典文档资料【免费的】。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
学习不要孤军奋战,最好是能抱团取暖,相互成就一起成长,群众效应的效果是非常强大的,大家一起学习,一起打卡,会更有学习动力,也更能坚持下去。你可以加入我们的测试技术交流扣扣群:914172719(里面有各种软件测试资源和技术讨论)
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!