jmeter - 在第二次迭代后动态添加标头失败

时间:2017-06-22 15:42:50

标签: jmeter beanshell

我正在尝试从csv文件添加uri并运行功能测试。所以基本上测试计划如下所述。对于从jmeter触发的每个http请求,使用预处理器我试图在头管理器中添加或清除标头详细信息。对于csv中的前2行(请求),请求会成功触发,但对于第三个URL,标头清除会产生错误。

Test plan-|            
          |->thread-|
                    |->while loop-|
                                  -> CSV file
                                  -> http request
                                     -> header manager
                                     -> beanshell pre processor
                     -> result tree

import org.apache.jmeter.protocol.http.control.Header;

log.info("--------------------->>>> BEFORE 1 CLEARING HEADER");
if (ctx.getCurrentSampler().getHeaderManager().size() != 0){
    log.info("--------------------->>>> BEFORE 2 CLEARING HEADER");
    sampler.getHeaderManager().clear();
    log.info("--------------------->>>> AFTER 1 CLEARING HEADER");
}

if(!vars.get("LABEL").equals("login")) {
    log.info("--------------------->>>> IN iF COND LABEL != LOGIN");
    if (vars.get("environment").equals("gateway")) {
        log.info("--------------------->>>> ADDING HEADER <!LOGIN> <environment = gateway>"); 
        ctx.getCurrentSampler().getHeaderManager().add(new Header("Accept", "application/json"));
        ctx.getCurrentSampler().getHeaderManager().add(new Header("Content-Type", "application/json"));
         ctx.getCurrentSampler().getHeaderManager().add(new Header("Authentication",vars.get("authCode")));
         ctx.getCurrentSampler().getHeaderManager().add(new Header("Authorization", "Bearer 4b6e13b4dfc3187de6c2b762c84d9196"));
         sizeHeader = ctx.getCurrentSampler().getHeaderManager().size();
         log.info("==================>>>> size of header after ADDING HEADER <!LOGIN> <environment = gateway> <" + sizeHeader + ">");
    } else {
        log.info("--------------------->>>> ADDING HEADER <!LOGIN> <environment != gateway>");
        ctx.getCurrentSampler().getHeaderManager().add(new Header("Accept", "application/json"));
        ctx.getCurrentSampler().getHeaderManager().add(new Header("Content-Type", "application/json"));
        ctx.getCurrentSampler().getHeaderManager().add(new Header("Authorization",vars.get("authCode")));
        sizeHeader = ctx.getCurrentSampler().getHeaderManager().size();
         log.info("==================>>>> size of header after ADDING HEADER <!LOGIN> <environment = !gateway> <" + sizeHeader + ">");
    }
} else {
    log.info("--------------------->>>> ADDING HEADER <LOGIN> <environment = gateway>"); 
    ctx.getCurrentSampler().getHeaderManager().add(new Header("Accept", "application/json"));
    ctx.getCurrentSampler().getHeaderManager().add(new Header("Content-Type", "application/x-www-form-urlencoded"));
    sizeHeader = ctx.getCurrentSampler().getHeaderManager().size();
    log.info("==================>>>> size of header after ADDING HEADER <LOGIN> <environment = gateway> <" + sizeHeader + ">");
}

--------------------------日志文件如下----------------- ---------

2017-06-22 20:37:57,049 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2017-06-22 20:37:57,050 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2017-06-22 20:37:57,056 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2017-06-22 20:37:57,060 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2017-06-22 20:37:57,061 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2017-06-22 20:37:57,061 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2017-06-22 20:37:57,061 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 perThread=1000.0 delayedStart=false
2017-06-22 20:37:57,066 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2017-06-22 20:37:57,066 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2017-06-22 20:37:57,067 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2017-06-22 20:37:57,067 INFO o.a.j.s.FileServer: Stored: C:\Users\XXX\Desktop\apache-jmeter-3.2\bin\projects\testFrag\apiTest.csv
2017-06-22 20:37:57,070 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> BEFORE 1 CLEARING HEADER
2017-06-22 20:37:57,073 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> ADDING HEADER <LOGIN> <environment = gateway>
2017-06-22 20:37:57,073 INFO o.a.j.u.BeanShellTestElement: ==================>>>> size of header after ADDING HEADER <LOGIN> <environment = gateway> <2>
2017-06-22 20:37:57,889 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> BEFORE 1 CLEARING HEADER
2017-06-22 20:37:57,889 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> BEFORE 2 CLEARING HEADER
2017-06-22 20:37:57,889 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> AFTER 1 CLEARING HEADER
2017-06-22 20:37:57,891 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> IN iF COND LABEL != LOGIN
2017-06-22 20:37:57,891 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> ADDING HEADER <!LOGIN> <environment = gateway>
2017-06-22 20:37:57,892 INFO o.a.j.u.BeanShellTestElement: ==================>>>> size of header after ADDING HEADER <!LOGIN> <environment = gateway> <4>
2017-06-22 20:38:00,050 INFO o.a.j.u.BeanShellTestElement: --------------------->>>> BEFORE 1 CLEARING HEADER
2017-06-22 20:38:00,050 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval     Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.Header;  log.info("-------------- . . . ''
2017-06-22 20:38:00,050 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval       Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.Header;  log.info("-------------- . . . ''
2017-06-22 20:38:01,466 INFO o.a.j.t.JMeterThread: Stop Thread seen for thread Thread Group 1-1, reason: org.apache.jorphan.util.JMeterStopThreadException: End of file:C:\Users\XXX\Desktop\apache-jmeter-3.2\bin\projects\testFrag\apiTest.csv detected for CSV DataSet:CSV Data Set Config configured with stopThread:true, recycle:false
2017-06-22 20:38:01,467 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2017-06-22 20:38:01,468 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test

编辑1

当第三次执行时,感觉错误可能出现在“ctx.getCurrentSampler()。getHeaderManager()。size()”语句中。用try和catch语句尝试了代码。显示为空指针错误。

while循环由CSV数据集控制,我将“EOF上的STOP线程”设置为“true”。

0 个答案:

没有答案