文章目录

  • 一、BeanShell组件
  • 二、BeanShell自带的语法(BeanShell常用变量和语法)
  • 1.log打印
  • 2.vars用来操作JMeter的局部变量(只能在一个线程组里面使用的变量)
  • 3.props用来操作JMeter的全局变量(能够跨线程组取值的变量)
  • 3.prev获取前一个请求返回的信息
  • 三、BeanShell调用java文件、jar包
  • 1.BeanShell调用java文件
  • 2.BeanShell调用jar包
  • 四、BeanShell调用python无参、一个参数、多个参数的函数文件
  • 1.BeanShell调用无参函数
  • 2.BeanShell调用有一个参数的函数
  • 3.BeanShell调用有多个参数的函数(无变量)
  • 4.BeanShell调用有多个参数的函数(有变量)


一、BeanShell组件

BeanShell组件完全符合java的语法以及自带语法的组件,总共有6种

  1. BeanShell 断言
  2. BeanShell 前置处理器
  3. BeanShell 取样器
  4. BeanShell 后置处理器
  5. BeanShell 定时器
  6. BeanShell 监听器

二、BeanShell自带的语法(BeanShell常用变量和语法)

1.log打印

  • 信息打印
//在日志中打印张三
log.info("张三");

shell_exec函数调用python例子 shell中调用python_python

  • 错误信息打印
//在日志中打印错误信息
log.error("打印错误信息");

shell_exec函数调用python例子 shell中调用python_python_02

  • 拼接打印
//不支持整类,需要使用字符串拼接
log.info(123 + "");

shell_exec函数调用python例子 shell中调用python_python_03

  • java打印
//java打印,在控制台显示
System.out.println("java打印");

shell_exec函数调用python例子 shell中调用python_jmeter_04


shell_exec函数调用python例子 shell中调用python_python_05

2.vars用来操作JMeter的局部变量(只能在一个线程组里面使用的变量)

1.对于 [用户定义的变量] 可以直接通过vars.get()取值
2.对于通过 [正则表达式] 或其他表达式获取到的中间变量可以直接通过vars.get()取值
3.不同的BeanShell之间可以相互设置或者获取值

  • 获取变量
//获取用户定义变量中的值
log.info(vars.get("name"));

shell_exec函数调用python例子 shell中调用python_jmeter_06

shell_exec函数调用python例子 shell中调用python_python_07

  • 定义变量
//定义一个变量
vars.put("age","18");

shell_exec函数调用python例子 shell中调用python_jmeter_08

shell_exec函数调用python例子 shell中调用python_接口测试_09

3.props用来操作JMeter的全局变量(能够跨线程组取值的变量)

//定义一个变量
props.put("age","18");

shell_exec函数调用python例子 shell中调用python_cmd命令_10

//获取线程组1中BeanShell取样器1定义变量的值
log.info(props.get("age"));

shell_exec函数调用python例子 shell中调用python_python_11

注意:执行的时候记得勾选测试计划中的 [独立运行每个线程组] ,因为线程组执行是无序的

shell_exec函数调用python例子 shell中调用python_jmeter_12

3.prev获取前一个请求返回的信息

//获取响应码
log.info(prev.getResponseCode());

shell_exec函数调用python例子 shell中调用python_python_13

//获取响应的内容
log.info(prev.getResponseDataAsString());

shell_exec函数调用python例子 shell中调用python_python_14

三、BeanShell调用java文件、jar包

1.BeanShell调用java文件

  1. 准备一个java文件
public class Test{
	public int sum(int a,int b){
		return a+b;
	}
}

shell_exec函数调用python例子 shell中调用python_cmd命令_15

  1. BeanShell调用java文件
//引入java文件 (java文件所在的路径)
source("D:\\Test.java");
//创建一个对象并调用方法
int result = new Test().sum(5,6);
//拼接打印
log.info(result + "");

shell_exec函数调用python例子 shell中调用python_java_16

2.BeanShell调用jar包

  • 准备的项目及代码内容——将项目打包成jar包
  1. 在测试计划中引入jar包
  2. BeanShell调用jar包
//导包(import跟的是java中的包名.类名)
import com.xj.Test;
//创建一个对象并调用方法
int result = new Test().sum(5,6);
//拼接打印
log.info(result + "");

shell_exec函数调用python例子 shell中调用python_接口测试_17

四、BeanShell调用python无参、一个参数、多个参数的函数文件

1.BeanShell调用无参函数

原理:通过cmd命令执行python.py文件,然后将获取输出并打印的值作为返回结果

  1. 准备如下代码的一个pthon文件
import time


def get_random_number():
    times = str(int(time.time()))
    print(times)


if __name__ == '__main__':
    get_random_number()

shell_exec函数调用python例子 shell中调用python_jmeter_18

  1. BeanShell调用python文件
//导包
import java.io.BufferedReader;
import java.io.InputStreamReader;

//组建cmd命令并执行python文件
String command = "cmd /c python D:/test.py";
Runtime rt = Runtime.getRuntime();  //初始化一个运行时对象
Process pr = rt.exec(command);  //通过运行时对象运行cmd命令。

//运行时等待
pr.waitFor();

//调用pr进程对象获取到它的输入流。读取pr文件流的内容,并且保存到reponse_data
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder response = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
    response.append(line);
}
String reponse_data = response.toString();
br.close();

//打印返回的内容
log.info("reponse_data:"+reponse_data);

shell_exec函数调用python例子 shell中调用python_java_19

2.BeanShell调用有一个参数的函数

  1. 准备如下代码的一个pthon文件
import argparse
import time


def get_random_number():
    # 接收cmd命令里面的参数
    parse = argparse.ArgumentParser()
    parse.add_argument("-t", "--ticket")
    args = parse.parse_args()
    ticket = args.ticket
    # print("ticket:%s" % ticket)
    # 随机数
    times = str(int(time.time()))
    print(ticket + times)


if __name__ == '__main__':
    get_random_number()

shell_exec函数调用python例子 shell中调用python_java_20

  1. BeanShell调用python文件
//导包
import java.io.BufferedReader;
import java.io.InputStreamReader;

//组建cmd命令并执行python文件
String command = "cmd /c python D:/test.py -t admin";
Runtime rt = Runtime.getRuntime();  //初始化一个运行时对象
Process pr = rt.exec(command);  //通过运行时对象运行cmd命令。

//运行时等待
pr.waitFor();

//调用pr进程对象获取到它的输入流。读取pr文件流的内容,并且保存到reponse_data
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder response = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
    response.append(line);
}
String reponse_data = response.toString();
br.close();

//打印返回的内容
log.info("reponse_data:"+reponse_data);

shell_exec函数调用python例子 shell中调用python_cmd命令_21

3.BeanShell调用有多个参数的函数(无变量)

  1. 准备如下代码的一个pthon文件
import argparse


def get_random_number():
    # 接收cmd命令里面的参数
    parse = argparse.ArgumentParser()
    parse.add_argument("-t", "--ticket", action="append")
    args = parse.parse_args()
    ticket = args.ticket
    print("ticket:%s" % ticket)


if __name__ == '__main__':
    get_random_number()

shell_exec函数调用python例子 shell中调用python_python_22

  1. BeanShell调用python文件
//导包
import java.io.BufferedReader;
import java.io.InputStreamReader;

//组建cmd命令并执行python文件
String command = "cmd /c python D:/test.py -t admin -t 123";
Runtime rt = Runtime.getRuntime();  //初始化一个运行时对象
Process pr = rt.exec(command);  //通过运行时对象运行cmd命令。

//运行时等待
pr.waitFor();

//调用pr进程对象获取到它的输入流。读取pr文件流的内容,并且保存到reponse_data
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder response = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
    response.append(line);
}
String reponse_data = response.toString();
br.close();

//打印返回的内容
log.info("reponse_data:"+reponse_data);

shell_exec函数调用python例子 shell中调用python_python_23

4.BeanShell调用有多个参数的函数(有变量)

  1. 准备如下代码的一个pthon文件
import argparse


def get_random_number():
    # 接收cmd命令里面的参数
    parse = argparse.ArgumentParser()
    parse.add_argument("-t", "--ticket", action="append")
    args = parse.parse_args()
    ticket = args.ticket
    print("ticket:%s" % ticket)


if __name__ == '__main__':
    get_random_number()

shell_exec函数调用python例子 shell中调用python_python_22

  1. 使用 “用户定义的变量” 定义一个变量
  2. BeanShell调用python文件

传参的方式1:可以使用 ${} 取值

//导包
import java.io.BufferedReader;
import java.io.InputStreamReader;

//组建cmd命令并执行python文件
String command = "cmd /c python D:/test.py -t admin -t ${name}";
Runtime rt = Runtime.getRuntime();  //初始化一个运行时对象
Process pr = rt.exec(command);  //通过运行时对象运行cmd命令。

//运行时等待
pr.waitFor();

//调用pr进程对象获取到它的输入流。读取pr文件流的内容,并且保存到reponse_data
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder response = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
    response.append(line);
}
String reponse_data = response.toString();
br.close();

//打印返回的内容
log.info("reponse_data:"+reponse_data);

shell_exec函数调用python例子 shell中调用python_cmd命令_25

传参的方式2:可以使用 vars.get() 获取值之后在拼接

//导包
import java.io.BufferedReader;
import java.io.InputStreamReader;

String names = vars.get("name");

//组建cmd命令并执行python文件
String command = "cmd /c python D:/test.py -t admin -t " + names;
Runtime rt = Runtime.getRuntime();  //初始化一个运行时对象
Process pr = rt.exec(command);  //通过运行时对象运行cmd命令。

//运行时等待
pr.waitFor();

//调用pr进程对象获取到它的输入流。读取pr文件流的内容,并且保存到reponse_data
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder response = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
    response.append(line);
}
String reponse_data = response.toString();
br.close();

//打印返回的内容
log.info("reponse_data:"+reponse_data);

shell_exec函数调用python例子 shell中调用python_java_26