Reputation: 439
I expect two possible cases in my application: Search was successful and search was failed. In both cases I have two different sets of HTTP requests, which JMeter should execute. How can I implement if-else block in JMeter scenario? I've tried to use if controller with Regular Expression Extractor, but relying on the results of Debug Sampler, this kind of extractors doesn't attached for the current thread. Hence, one thread can simply override the result of another thread. Is it bug or feature? Are there any workarounds?
My Regular Expression Extractor:
My first If controller:
My second if controller:
Order of execution:
Always fires the first controller and never the second. When the customer search was failed, the page does not contains the word "Daniel" and I expect ${customer_name} with 0 length. Moreover, Debug Sampler returns customer_name with filled value after unsuccessful search. Looks like that other thread overrides it and this extractor is not thread safe.
Upvotes: 2
Views: 12058
Reputation: 1034
This If-Then-Else
construct is not so easy, but possible:
Switch controller
(activated by integers) instead of the IF controller, andternary operator ?:
within the condition. I.e.:
${__groovy( (vars.get("customer_name")==null) ?0:1 )}
The "0" case is then used as a (somewhat) default. This way, you can have more "Else" branches, however, just two branches (explicit+default) are rather enough, used as usual.
Else-If
, you need to nest another switch to the root-some switch.Also notice that the Elvis operator
exists (a collapsed ternary), which could be handy for int
values.
Upvotes: 1
Reputation: 168092
Your second condition is flaky.
For instance I have ${foo} variable. If it is not set, it's value will be ${foo}
(surprisingly) and it's length will be 6.
So if there is no match your "${customer_name}".length
value will be 16.
I would suggest changing your 2nd If Controller condition to be something like:
${__javaScript(vars.get('customer_name')==null,)}
and it should work this way. (you need to use __javaScript() function to get access to vars
object which is a shorthand to JMeterVariables instance)
Another option is marking sampler as failed in case of "Daniel" is not found and use ${JMeterThread.last_sample_ok}
variable value as a condition.
See How to use JMeter's 'IF' Controller and get Pie. guide for some If Controller tips and tricks.
Upvotes: 4
Reputation: 18051
Just use two IF-controllers. One IF-controller for the successful search and one IF-controller for the unsuccessful search.
Make sure your variable is cleared on every run.
Upvotes: -1
Reputation: 439
The main detail of my problem: I've executed the scenario in Loop controller. Therefore, my variable has not been reseted and the next iteration was incorrect. The order to avoid it I've added the BeanShell PostProcessor, which resets my variable after every iteration.
vars.put("customer_name","");
Now everything is fine.
Upvotes: 0