jmeter—beanshell 使用

本贴最后更新于 1345 天前,其中的信息可能已经时移世异

目录:

  一、什么是Bean Shell

  二、Jmeter有哪些Bean Shell

  三、BeanShell的用法

  四、引入java代码的三种方式

MD5加密之Bean Shell(了解)

一、什么是Bean Shell

BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法;

BeanShell是一种松散类型的脚本语言(这点和JS类似);

BeanShell是用Java写成的,一个小型的、免费的、可以下载的、嵌入式的Java源代码解释器,具有对象脚本语言特性,非常精简的解释器jar文件大小为175k。

BeanShell执行标准Java语句和表达式,另外包括一些脚本命令和语法。

官网:http://www.BeanShell.org/

二、Jmeter有哪些Bean Shell

定时器:  BeanShell Timer

前置处理器:BeanShell PreProcessor

采样器:  BeanShell Sampler

后置处理器:BeanShell PostProcessor

断言:   BeanShell断言

监听器:  BeanShell Listener

三、BeanShell的用法

   在此介绍下BeanShell Sampler的用法,其它的beahshell可以类推。在此我们使用beahshell调用自己写的工具类,工具类实现了密码的加、解密功能:

1、Idea工具准备好算法

image.png

准备好调试的方法

image.png

Idea导出jar包

image.png

image.png

设置完是这样子的,关于 JAR files from libraries的两个选项:

选中第一个的话,打完包后是一个jar包

选中第二个的话,打完包后是一个jar包,外带你项目所用的jar包

image.png

image.png

最后一步

image.png

image.png

2、把jar包在测试计划里面,进行导入

3、两种方式进行:

一种是:打开jmeter,添加一个http sampler(调用重置支付密码接口),在sampler下添加一个BeanShell Sampler

另外一种:直接在beanshell 里面完成请求加密操作

4、在BeanShell Sampler中导入我们的jar包,调用里面的加、解密码方法,把结果保存在jmeter变量中,下面两个方法是beanshell中我们最常用到的:

vars.get():获得变量值

vars.put():,将数据存到jmeter变量中

image.png

import jmeter.MD5;
{
String newpwd = "123456";
MD5 getMD5 = new MD5();
String res = getMD5.GetMD5Code(newpwd);
vars.put("resnewpwd",res);
System.out.println(res);
}
5、把加密后的密码存到jmeter变量中,然后在http sampler中就可以通过${resnewpwd}进行使用了

image.png

6、执行脚本:

image.png

另外一种方式:直接在BeanShell Sampler里面完成

import jmeter.MD5;

import jmeter.HttpClientRequest;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.codehaus.jettison.json.JSONObject;

   String login_url = "http://119.29.164.88:8080/mobile/api/user/login";

    String login_data = "{\"mobile\":\"15800000002\",\"password\":\"123456\"}";

String postResult = HttpClientRequest.sendPost(login_url,login_data);

  JSONObject jsonObj = new JSONObject(postResult);


  //int status = (int) jsonObj.get("code");
  String token = (String) jsonObj.getJSONObject("data").get("token");

  System.out.println( "查看登录的结果:" + jsonObj );

  System.out.println( "token:" + token );

  String modifypaypwd_url = "http://119.29.164.88::8080/mobile/api/user/resetpaypwd";

  String newPasswod = MD5.GetMD5Code("123456");

  String modifypaypwd_data = "{\"token\":\""+token+"\",\"password\":\""+newPasswod+"\"}";

  //System.out.print(modifypaypwd_data);

  String postResult1 = HttpClientRequest.sendPost(modifypaypwd_url,modifypaypwd_data);

  JSONObject jsonObj1 = new JSONObject(postResult1);

  int status = (int) jsonObj1.get("code");

  //String token = (String) jsonObj.getJSONObject("data").get("token");

  System.out.println( "查看加密的结果:" + jsonObj1 );

  log.info("code的值:" + status);

 vars.put("code",status.toString());

  vars.put("jsonObj1",jsonObj1.toString());

beanshell断言

// 使用vars.get()方法获取变量的值

String status = vars.get("code");

log.info("code的值:" + status);

if(status.equals("0")){

Failure = false;

FailureMessage = "成功";

log.info("成功");

}
else{
Failure = true;

FailureMessage = "失败";

log.info("失败");

}

引入java代码另外两种方式

一、一种方式:直接引入java文件

1、假如我有一个java 源文件,名为: MD5.java

2、Bean Shell使用代码如下: 

 在bean shel中通过source("代码路径")方法引入java,然后调用方法和java一样,new一个class,再调用里面的GetMD5Code 方法。

source("D:\workspace\jmeter\src\jmeter\MD5.java");

  String newpwd = "123456";       
  
  MD5 getMD5 = new MD5();   

  String resnew =  getMD5.GetMD5Code(newpwd);	

	//System.out.println(res);

	//vars.put("resoldpwd",resold);

	vars.put("resnewpwd",resnew);

二、另外一种方式:引用外部class文件:

  现在知道如何引用外部文件,有时候如果我们只有class文件怎么办呢?其实在jmeter中也可以直接引用class文件,示例如下:

1、直接把上例中的java文件编译成class文件,如何编译请自行百度。

2、Bean Shell使用代码如下:

  用addClassPath("D:\")方法引入 class文件,在用import导入包及类,然后就可以像java一样调用了

image.png

回帖
请输入回帖内容 ...