• jmeter之Bean Shell


    代码始终比工具要灵活,Bean Shell就是一个jmeter中可以融合代码的工具

    目录

    1、jmeter中的Bean Shell工具

    2、beanshell之断言

    3、beanshell之sample

    1、jmeter中的Bean Shell工具

    • jmeter中有很多Bean Shell插件供使用,方便自己写代码实现工具的功能扩展,如:

    定时器:BeanShell Timer

    前置处理器:BeanShell PreProcessor

    采样器:BeanShell Sampler

    后置处理器:BeanShell PostProcessor

    断言:BeanShell Assert

    监听器:BeanShell Listener

    • jmeter常用内置变量和方法

    prev:获取前面的sample返回的信息

    prev.getResponseDataAsString():获取响应体数据(String类型)

    prev.getResponseCode():获取状态码(同ResponseCode,String类型)

    vars: 操作用户定义变量

    String var1 = vars.get("变量名"):取值

    vars.put("变量名", 变量值):赋值

     

    log:输出信息到日志

    log.debu("调试信息")

    log.info("响应状态码" + ResponseCode)

    log.warn("警告信息")

    log.error("出错信息")

    2、beanshell之断言

    第一步:如果是json格式的response,则需要先下载json包放入jmeter的/lib/ext

    json.jar下载地址:链接: https://pan.baidu.com/s/1fWdO6qA8c4qZQmrlvIGKsA 提取码: y3d4

    除了json.jar外还有Fastjson和gjson,后续研究好了再补充

    第二步:使用Bean Shell断言工具

     第三步:编写脚本

    import org.json.*;   //导入org.json包
    
    
    String response = prev.getResponseDataAsString();  //获取响应数据
    JSONObject responseJson = new JSONObject(response);  //转为JSON对象
    String message = responseJson.getString("message"); //获取key为message的value
    log.info("响应message字段:" + message);
    if(message.equals("登陆成功")){
        Failure=false;  //断言成功
        log.info("登陆成功");
    }
    else{
        Failure=true;   //断言失败
        FailureMessage="登陆失败;
    }

    注:Failure=false;这个断言表示成功

    3、Bean Shell之Sample

    • 使用Bean Shell Sample保存变量的值

    第一种:使用FileOutputStream

    import java.io.*;
    
    public static void execParam(){
        String getdata = vars.get("assess_token");
        String filename = "D:/token.txt"; // file path
        try{
            File file = new File(filename);
            if (!file.exists()){
                file.createNewFile();
            } 
            FileOutputStream fos = new FileOutputStream(filename,true);//true superaddition
            fos.write(getdata.getBytes());
            fos.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    execParam();

    第二种:使用BufferedWrite

    import java.io.*;
    
    FileWriter fstream=new FileWriter("D:/token.txt",true);
    BufferedWriter out =new BufferedWriter(fstream);
    out.write("${assess_token}");
    out.write(System.getProperty("line.separator"));
    out.close();
    fstream.close();
    

      

    • 使用Bean Shell Sampler导入自己写的方法

     

    有三种方式可以导入自己写的方法

    第一种:Bean Shell Sampler使用外部的.java文件,使用source导入

    source("D:\Myclass.java");
    
    int res=new Myclass().add(3,8);
    
    vars.put("add",res.toString());

    第二种:引入class文件

    addClassPath("d:\"); //引入class文件
    
    import test.Myclass; //导入类名
    
    int res =new Myclass().add(6,8); //调用
    
    vars.put("add",res.toString());//保存变量

    第三种:将写好的方法打成jar包,导入jar包

    感谢网络上的大神提供的文章,如有侵权,请联系删除:https://www.cnblogs.com/puresoul/p/4949889.html

  • 相关阅读:
    GNU make manual 翻译( 一百五十)
    [导入]Google开发者日见闻 王开源现身
    [导入]微软中国原高管宫力就任火狐中国总经理
    [导入]QQTalk Beta1 简体中文版
    [导入]《南方都市报》:国产龙芯产业化 难
    [导入][多图]Nokia正式发布奢华8600/6500双子手机
    [导入]用户界面设计的技巧与技术
    [导入]BitComet(比特彗星) 0.89
    [导入]µTorrent 1.7 beta 2248
    今天我注册了
  • 原文地址:https://www.cnblogs.com/weizhideweilai/p/13584571.html
Copyright © 2020-2023  润新知