Home > Back-end >  My compare condition "or" is not working in Jmeter, did I do something wrong?
My compare condition "or" is not working in Jmeter, did I do something wrong?

Time:11-30

I did a "while", which starts with a variable that was not previously defined, so I did the comparison:

${__javaScript(${status} == "undefined",)} and if the answer is "true" the while loop starts and works; it runs once and gets the new status of the request, this time with a value of == 500 because I use a Json extractor to get the new status returned in the API and then the API stops because the status is no longer undefined, now == 500, worked just fine. But

when I put one more validation using the "or" comparison:

${__javaScript("${status}" == 500 || ${status} == "undefined",)}

It should exit the loop if the response in the status field was different from "undefined" or "500", but when it returns "ERROR" it keeps running, I looked at the log and got the following error:

2021-11-29 18:05:10,029 ERROR o.a.j.f.JavaScript: Error processing Javascript: ["ERROR" == 500 || ERROR == "undefined"] javax.script.ScriptException: ReferenceError: "ERROR" is not defined in at line number 1 at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470) ~[nashorn.jar:?] at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454) ~[nashorn.jar:?] at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406) ~[nashorn.jar:?] at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402) ~[nashorn.jar:?] at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155) ~[nashorn.jar:?] at org.apache.jmeter.functions.JavaScript.executeWithNashorn(JavaScript.java:141) [ApacheJMeter_functions.jar:5.4.1] at org.apache.jmeter.functions.JavaScript.execute(JavaScript.java:102) [ApacheJMeter_functions.jar:5.4.1] at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:138) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:113) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:100) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.WhileController.getCondition(WhileController.java:142) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.WhileController.endOfLoop(WhileController.java:62) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.WhileController.nextIsNull(WhileController.java:85) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.GenericController.next(GenericController.java:170) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.WhileController.next(WhileController.java:117) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.GenericController.nextIsAController(GenericController.java:222) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.GenericController.next(GenericController.java:175) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.control.LoopController.next(LoopController.java:134) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.threads.AbstractThreadGroup.next(AbstractThreadGroup.java:91) [ApacheJMeter_core.jar:5.4.1] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:291) [ApacheJMeter_core.jar:5.4.1] at java.lang.Thread.run(Unknown Source) [?:1.8.0_271]

but like... "ERROR" isn't really meant to be set, because if it returns it's to exit the loop

so i really don't understand this error message, does anyone know what might be going on?

CodePudding user response:

There is a syntax issue in the Javascript

Following will work within your while controller

${__javaScript("${status}" == "500" || "${status}" == "undefined",)}

Also it's recommended to use __groovy or __jexl3 over Javascript considering performance.

javaScript is not the best scripting language for performances in JMeter. If your plan requires a high number of threads it is advised to use __jexl3 or __groovy functions.

Groovy

${__groovy(vars.get("status")=="500" || vars.get("status")=="undefined",)}

Jexl3

${__jexl3(vars.get("status")=="500" || vars.get("status")=="undefined",)}

CodePudding user response:

Looks like a copy-paste issue:

${__javaScript("${status}" == 500 || ${status} == "undefined",)}
               ^         ^           ^       ^
                 here you have       ^ and here 
                 quotation marks     you don't

it also makes sense to surround this 500 with quotation marks as well otherwise you can get an unexpected behaviour trying compare a Number to a String

All JMeter Variables with their respective values can be visualized using Debug Sampler and View Results Tree listener combination.

  • Related