JMeter If Controller使用groovy和Or无法正常工作 [英] JMeter If Controller using groovy and Or is not working
问题描述
我有一个脚本,其中有一些if控制器.我试图添加第四个If控制器,如果三个预期值均未返回,则该脚本将触发脚本故障.这是说三个期望值之一是无效的.
首先,我有一个用户定义的变量,如下所示:
testTool = $ {__ P(testTool,APPLES)}
2nd,我有这3个If控制器,它们具有以下表达式:
-
$ {__ groovy(vars.get("testTool").toUpperCase().equals("APPLES"))}
-
$ {__ groovy(vars.get("testTool").toUpperCase().equals("BANANAS"))}
-
$ {__ groovy(vars.get("testTool").toUpperCase().equals("PEACHES"))}
如果testTool的值不是3个期望值之一,则应该触发第4个If.它的表情看起来像这样:
>$ {__ groovy((vars.get("testTool").toUpperCase().equals("APPLES")== false ||>vars.get("testTool").toUpperCase().equals("BANANAS")==假||>vars.get("testTool").toUpperCase().equals("PEACHES")== false))}
我也尝试过这种方式:
>$ {__ groovy((!vars.get("testTool").toUpperCase().equals("APPLES")||>!vars.get("testTool").toUpperCase().equals("BANANAS")||>!vars.get("testTool").toUpperCase().equals("PEACHES")),)}
某种程度上说APPLES是无效的testTool.我究竟做错了什么?所有控制器是否都已选中"解释为变量表达式的条件".
您应该使用&&
运算符而不是 ||
,请参见
请参见 JMeter轻松创建中的选择语句有关详细信息的指南.
I have a script where I have some if controllers. I'm attempting to add a 4th If controller that will trigger a script failure if none of the 3 expected values is returned. It's saying one of the 3 expected values is invalid.
1st, I have a user defined variable like this:
testTool= ${__P(testTool,APPLES)}
2nd, I have these 3 If controllers with these Expressions:
${__groovy(vars.get("testTool").toUpperCase().equals("APPLES"))}
${__groovy(vars.get("testTool").toUpperCase().equals("BANANAS"))}
${__groovy(vars.get("testTool").toUpperCase().equals("PEACHES"))}
The 4th If is supposed to be triggered if the value of testTool is not one of the 3 expected values. It's Expression looks like this:
> ${__groovy( (vars.get("testTool").toUpperCase().equals("APPLES") == false ||
> vars.get("testTool").toUpperCase().equals("BANANAS") == false ||
> vars.get("testTool").toUpperCase().equals("PEACHES") == false)) }
I have also tried it this way:
> ${__groovy((!vars.get("testTool").toUpperCase().equals("APPLES") ||
> !vars.get("testTool").toUpperCase().equals("BANANAS") ||
> !vars.get("testTool").toUpperCase().equals("PEACHES")),)}
It is somehow saying APPLES is an invalid testTool. What am I doing wrong? All if controllers have the 'Interpret Condition as Variable Expression' checked.
You should use &&
operator instead of ||
, see Groovy Logical Operators for detailed explanation and more information.
Your 4th expression needs to be amended to look like:
${__groovy(!(vars.get("testTool").toUpperCase().equals("APPLES")) && !(vars.get("testTool").toUpperCase().equals("BANANAS")) && !(vars.get("testTool").toUpperCase().equals("PEACHES")))}
An easier option would be using Switch Controller, from implementation and especially performance perspectives it is the optimal solution.
- Add Switch Controller to your Test Plan
- Use
${testTool}
as the "Switch Value" Put 4 requests as the children of the Switch Controller and name them as:
APPLES
BANANAS
PEACHES
DEFAULT
So if
${testTool}
variable value will beAPPLES
- theAPPLES
sampler will be executed, if${testTool}
variable value will beBANANAS
- theBANANAS
sampler will be executed, etc.If
${testTool}
will not match any other children - JMeter will runDEFAULT
sampler
See Selection Statements in JMeter Made Easy guide for details.
这篇关于JMeter If Controller使用groovy和Or无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!