1
votes

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: extractor

My first If controller:

first if controller

My second if controller: enter image description here

Order of execution:
order

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.

4

4 Answers

4
votes

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.

0
votes

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.

0
votes

This If-Then-Else construct is not so easy, but possible:

  1. Just use the Switch controller (activated by integers) instead of the IF controller, and
  2. use (written as Groovy) the ternary 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.

  1. To get 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.

-1
votes

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.