jmeter,参数、Bean Shell断言

一、参数说明

JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下:

1、vars :操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:

  a) vars.get(String key);       从jmeter中获得变量值

  b) vars.put(String key,String value);       数据存到jmeter变量中

2、props :操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值。对应于java.util.Properties。 

  a) props.get("START.HMS");      注:START.HMS为属性名,在文件jmeter.properties中定义 

  b) props.put("PROP1","1234"); 

3、ctx:该变量引用了当前线程的上下文

4、prev :获取前面的sample返回的信息,常用方法:

    a) getResponseDataAsString();    获取响应信息

    b) getResponseCode() ;    获取响应code

5、写入信息到jmeber.log文件:log.info(“This is log info!”)

6、获得当前线程:

thread=Thread.currentThread().getName();

user=thread.replaceAll(" ","_")+"_压力测试";

log.info("====打印线程名称===="+user);

vars.put("user",user);

二、Jmeter有哪些Bean Shell

BeanShell Sampler       ,采样器

BeanShell PreProcessor    ,前置处理器

BeanShell PostProcessor  ,后置处理器

BeanShell Timer      ,定时器

BeanShell Listener     ,监听器

BeanShell Assertion     ,断言

1、在eclipse写好代码,然后把该类打成jar包(在类上点击右键->Export->jar file)

2、把jar包放到jmeter目录\apache-jmeter-2.13\lib\ext下

3、打开jmeter,添加一个BeanShell ,导入我们的jar包,调用里面的方法。

三、BeanShell断言

//示例脚本
if (ResponseCode != null && ResponseCode.equals ("200") == false )
{
    // this is standard stuff
    Failure=true ;
    FailureMessage ="Response code was not a 200 response code it was " + ResponseCode + "." ;
    print ( "the return code is " + ResponseCode);   // this goes to stdout
    log.warn( "the return code is " + ResponseCode); // this goes to the JMeter log file
} else {
    try
    {
         byte [] arr = (byte[]) ResponseData ;
         if (arr != null){
        String question=vars.get("question");
        String response= new String(ResponseDate,"utf-8");
        
             Failure= ! response.contains("  \"answer\": \" "+question+"  \"  ");
             FailureMessage = Failure?"The response data size was not as expected\n" 
          +"question"+question+"\n"
          +"response"+response:"";

          }else {
              Failure= true ;
              FailureMessage = "The response data size was null"  ;
           }
     }
     catch ( Throwable t )
     {
          print ( t ) ;
          log.warn("Error: ",t);
     }
}

 

 

转载于:https://www.cnblogs.com/yinwenbin/p/10827943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值