zoukankan      html  css  js  c++  java
  • 性能测试九:jmeter进阶之beanshell的使用+断言

    一、使用

    BeanShell使用方式一

    BeanShell面板上写脚本

    // 从vars中获取用户定义的参数,并转换为int类型

    int p_skuId = Integer.parseInt(vars.get("p_skuId"));

    // 进行逻辑判断,如果是偶数,保存一个参数p_opt,值为“Rick”;如果是奇数,值为“Glen”

    if (p_skuId % 2 == 0){

      vars.put("p_opt","Rick");

    }else{

      vars.put("p_opt","Glen");

    }

    BeanShell使用方式二

    引用外部java文件 步骤:

    //引用外部java文件

    source("D:\md5\Md5Util.java");

    //获取出原来的p_opt参数

    String str_opt = vars.get("p_opt");

    //调用Md5Util类中的getMd5Hex方法,将str_opt加密

    String md5_opt = Md5Util.getMd5Hex(str_opt);

    //将加密后的参数重新保存为一个mdt_opt参数,并放到vars中

    vars.put("md5_opt",md5_opt);

     

    BeanShell使用方式三

    引入外部jar包 步骤:

    1.“测试计划”里,Add directory or jar to classpath

    2.import 所需要的类名

    //导入类名

    import com.lee.util.Md5Util;

    //获取出原来的p_opt参数

    String str_opt = vars.get("p_opt");

    //调用Md5Util类中的getMd5Hex方法,将str_opt加密

    String md5_opt = Md5Util.getMd5Hex(str_opt);

    //将加密后的参数重新保存为一个mdt_opt参数,并放到vars中

    vars.put("md5_opt",md5_opt);

     

    二、断言

    内置变量

    Failure:是否失败,boolean类型
    FailureMessage:失败日志,在断言失败时显示
    例:
    int stock = Integer.parseInt(vars.get("p_stock"));
    if (stock > 1500){
      Failure = true;
      FailureMessage = "库存数量超过了1500";
      // ResponseData是服务器返回的byte[]类型的数据,如果想打印,必须转换为String类型的,用new String(ResponseData)
      log.info(new String(ResponseData));
      //打印当前请求的url, SamplerData是String类型的数据
      log.info(SamplerData);
      }

  • 相关阅读:
    远程支付技术方案
    软件架构设计(第2版)——程序员向架构师转型必备
    概念架构是什么
    项目报警机制
    编写有效用例
    移动支付的基本要素
    相机的日常维护和保养注意事项
    吴炜摄影教程随堂笔记1
    D80使用心得3
    项目沟通管理识别干系人
  • 原文地址:https://www.cnblogs.com/malinalian/p/10492050.html
Copyright © 2011-2022 走看看