Bean Shell常用变量、对象总结

一、log 对象

               写入信息到jmeber.log文件,使用方法:log.info(“Agoly”);

二、 ctx对象

        该变量引用了当前线程的上下文,使用方法可参考:org.apache.jmeter.threads.JMeterContext

三、 vars对象

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

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

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

    更多方法可参考:org.apache.jmeter.threads.JMeterVariables

四、props 对象

  • props - (JMeterProperties - class java.util.Properties):操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值,而不能是一个对象。对应于java.util.Properties。 

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

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

五、SampleResult 对象(只能用于Bean Shell 断言)

                获取前面的sample返回的结果,常用方法:  

    String responseData= SampleResult.getResponseDataAsString(); //获取响应数据
    String responseCode = SampleResult.getResponseCode(); //获取响应码 HTTP: 200 、502、404等
    String sampleLabel=SampleResult.getSampleLabel(); //接口名称
    String url = SampleResult.getUrlAsString() ; //请求url
    String samplerData = SampleResult.getSamplerData() ; //请求数据 ;请求url、请求body
    String requestHeaders= SampleResult.getRequestHeaders() ; // 请求header
    boolean status = SampleResult.isResponseCodeOK(); // HTTP 返回 200时为true

    更多方法可参考:org.apache.jmeter.samplers.SampleResult

       

六、 prev对象(用于后置处理器、断言)

               实际是一个SampleResult对象;使用同上

                 

   SampleResult 与prev 区别: 前者只能用于Bean Shell 断言,后者可以用于后置处理器、断言

七、samper对象(用于前置处理器)

         a)获取http请求的 url

           String url = sampler.getPath();

         b)获取http请求的body内容           

           Arguments arguments = sampler.getArguments(); // 调用时注意sampler小写
          String body = arguments.getArgument(0).getValue();

           备注:需要引入包import org.apache.jmeter.config.Arguments;

八、beanshell断言

    Failure = true;

    FailureMessage = "断言失败描述";

九、BeanShell的API文档

    http://jmeter.apache.org/api/

原文地址:https://www.cnblogs.com/peak911/p/9238738.html