Jmeter - How to keep trying the same request until it succeeds

时间:2018-09-18 20:30:56

标签: jmeter

So right now I have my http request under a while controller and I have a user defined variable Failure set to true. I would like jmeter to keep trying this request until it succeeded (without returned 500). My while loop condition is:

${__javaScript(${Failure})} 

I also tried ${Failure} as while condition but getting the same result.

And I have a JSR223 Assertion after the result tree as following:

if (ResponseCode.equals("500") == true) {
    vars.put("Failure", true)
}
else { 
    vars.put("Failure", false)
}

When I ran this, I got into infinite loop even my request succeeded. It seems the Failure value was never updated. Any suggestion on this would be appreciated.

2 个答案:

答案 0 :(得分:1)

这是因为您试图将Boolean对象添加到需要String的函数中。为了能够将布尔值存储到JMeter变量中,您需要使用vars.putObject()函数,例如:

vars.putObject("Failure", true)

或用引号将true括起来,这样对于Groovy来说它看起来像是一个字符串:

vars.put("Failure", "true"); 
  1. 修改您的JSR223断言代码,使其看起来像:

    if (ResponseCode.equals("500")) {
        vars.put("Failure", "true")
    }
    else { 
        vars.put("Failure", "false")
    }
    
  2. 将While控制器条件修改为${Failure}。如果需要执行一些脚本编写,则使用JavaScript是一种性能反模式的形式-请转到Groovy。在特定情况下,您可以仅使用${Failure}变量作为条件,因为它可以是truefalse

答案 1 :(得分:0)

我终于开始工作了。在我的JSR233断言中,我将其更新为:

if (prev.getResponseCode().equals("500")) {
    vars.put("Failure", "true")
}
else { 
    vars.put("Failure", "false")
}

它现在可以工作。