Why is 'while controller' not working in 'loop controller'? - jmeter

I need to implement this use case.
I have to use a while controller test fragment in a loop controller and then run a request. After that, I need to run another request which is in a while loop. It should repeat 2 times.
Loop set to =2.
It runs successfully the first time, but the second time it just skips the while controller request.

While Controller can be "skipped" only in case when the "Condition" is (or becomes) false so maybe your test fragment is setting some variable to false or increments a counter to some specific value.
I would recommend adding a Debug Sampler as a last request in the Test Fragment and put the same expression you use in the While Controller condition as its label. Run your test and check the label using View Results Tree listener. If it's false - find a way to make it true, otherwise your fragment won't be executed second time.
See Using the While Controller in JMeter article to learn more about implementing "While" loops in JMeter tests.

It worked by setting newValue to variable.
Added BeanShell post processor and added code like
vars.put("Id_job","newValue");

Related

Facing issue while handling if controller

I am facing issue while using if controller in my jmeter script.
i used condition "${Response}"=="Test unsuccessful" in if controller.
Response variable contains complete response and i'm checking test unsuccessful in response if not it should execute next. however its not executing next even though we are not getting correct response.
How to handle this?
I don't think you can use the condition like "${Response}"=="Test unsuccessful", you can use a JMeter Variable if it resolves to true (or false), however if you want to compare 2 strings and basing on result conditionally execute (or don't execute child sampler(s)) you need to use a function which compares the strings and returns true or false, i.e. __jexl3() would be a good candidate:
${__jexl3("${Response}"=="Test unsuccessful",)}
More information: 6 Tips for JMeter If Controller Usage

Using BeanShell in JMeter if controller to stop thread

The accepted answer in this thread is from 2014 and does not seem to work any more. I can't figure out why: JMeter. How to determine when thread group is finished
I have 2 threadgroups (ignore the setUp group)
Based on the accepted answer in the thread above I added
A BeanShell Preprocessor vars.put("DONE", "FALSE");
A BeanShell Postprocessor
int activeThreadCount = org.apache.jmeter.threads.JMeterContextService.getNumberOfThreads();
if (activeThreadCount <= 1)
{
vars.put("DONE", "TRUE");
}
A If Controller ${__BeanShell(vars.get("DONE") != null && vars.get("DONE")=="TRUE")};
... with a Flow Control Action terminating all threads via Stop now if triggered.
via print statements in the postprocessor I was able to verify that the variable "DONE" is correctly set and the condition vars.get("DONE") != null && vars.get("DONE")=="TRUE" is evaluated correctly (when used in the postprocessor).
However when I use the condition inside of the If Controller it does not seem to be evaluated correctly no code inside of child elements of the If Controller is executed. The "Thred Group: ETL" just keeps on going even if the condition should evaluate to true.
My assumption would be that this has to do with the "Interpret Condition as Vairable Expression"-checkbox or the interpreter behind the If Controller. But unfortunately I don't know enough about JMeter to figure this out.
No Semicolon at the end of an If Controller expression is allowed. After removing the semicolon the controller works as intended
First of all get familiarized with JMeter Scoping Rules as your Beanshell Pre and Post processors are executed before/after each Sampler and it doesn't seem to me that this is something you really want to achieve
Second, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting
If Controller accepts something which resolves to true or false, in your case it's true; so If Controller's children will never be executed
It is possible to stop all the threads directly from the PostProcessor itself as simple as prev.setStopTest(true)

JMeter - Looping

I have the following script with 1 thread and 2 iterations.
Debug in Switch is not called. The second Google in the first iteration and the second Yahoo in the second iteration are not executed. Why?
Thank you for the help.
I added the image of Switch Controller.
Google and Yahoo are Simple Controllers with one HTTP Request Sampler.
Remove spaces in the Domains.csv file in the second column. As of now action= Google is checked instead of action=Google. so the behaviour.
Domains.csv
domain_1,domain_2
Google,Google
Yahoo,Yahoo
Note: As you are using Switch Controller, it executes only matching element inside of it.
Debug Sampler in Switch element will never be matched as you are looking for either Google or Yahoo.
As Edi Prayitno mentioned, you can keep it inside the Simple Controller, if you want to execute Debug Sampler in Switch every time.
Based on the help of Switch Controller above, you put the Switch Value = ${action}. It means you filled the Switch Value with the name of the subordinate element. When ${action} name = Google, it will execute subordinate element = Google. When ${action} = Yahoo, it will execute subordinate element name = Yahoo.That means Debug in Switch will be never be called.
If you want to put debug step inside of Switch Controller, you can re-arrange your test as below:
I hope that helps you.

How to validate JMeter user defined variables?

I'm new to JMeter, I want to validate a JMeter test input variables defined as part of "User Defined Parameters". Let's say I have a variable "sessions" and my tester should pass input values in between 0 to 30 for sessions, if tester passes other than this range the test should not go further and should throw an error with appropriate message.
Is it possible with any kind of JMeter controllers/assertions/... without writing code for validation?
I am not sure is there any efficient/direct way of doing this. but I achieved your requirement as follows:
Add Setup Thread Group (before any other ThreadGroup). select radio button Stop Test Now in Action to be taken after a Sample error
Add Debug Sampler to the Setup Thread Group.
Add Response Assertion (RA) to the Debug Sampler.
In RA, Select JMeter Variable in Apply to section.
In RA, select Matches in Pattern Matching Rules section.
In RA, add the regex ^[0-9]$|^0[1-9]$|^1[0-9]$|^2[0-9]$|^30$ in Pattern to test text area.
Test will be stopped if you provide sessions value other than 0-30 in User Defined Variables, as Setup Thread Group is configured to Stop Test Now on Sample error.
Note1: Added View Results Tree Listener for confirmation whether the test is continued and also to check the error message. View Results Tree is added only for visual confirmation, must be removed during the load test, does not effect the actual logic.
Note2: I am not aware of any component, which can show custom message/alert to the user. so, used View Results Tree. We should remove this command during load testing. I added here for visual confirmation purpose. If not present also, Test will be stopped on wrong value for sessions, i.e., other than 0-30
Note3: We need a Sampler component in order to apply an Assertion. so, added Debug Sampler. Debug Sampler just reports all the JMeter variable values at the point of its execution.
Image references:
Setup Thread Group:
Response Assertion:
View Results Tree:
You cannot achieve such validation in GUI without amending JMeter source code but you can check the variable range using scripting.
For example, add a JSR223 Sampler as a child of the first request and put the following code into "Script" area:
import org.apache.commons.lang3.Range;
int sessions = Integer.parseInt(vars.get("sessions"));
String errorMessage = "Provided sessions number is not between 1 and 30, stopping test";
if (!Range.between(1, 30).contains(sessions)) {
log.info(errorMessage);
SampleResult.setSuccessful(false);
SampleResult.setResponseMessage(errorMessage);
SampleResult.setStopTest(true);
}
Demo:
Make sure you are using Groovy as a language (the option should be default as per JMeter 3.1, if you are using earlier JMeter version for some reason - you will have to choose groovy from the "Language" dropdown)

Jmeter If controller condition statement

I am trying to built a test plan in jmeter where i want to run test for specific HTTP request based on their names. I used if controller but I dont know what condition to write.
I am writing ${__samplerName()}=="HTTPRequestName" in the condition but it isn't executing.
kindly help me as soon as possible.
You need to surround ${__samplerName()} with quotation marks as follows:
"${__samplerName()}"=="HTTPRequestName"
See How to use JMeter's 'IF' Controller and get Pie. guide for more details on If Controller use cases and clauses.
In case if you need to run samplers basing on some condition, you can use JMeter Properties as follows:
Launch JMeter providing sampler name property like jmeter -Jrunsomesampler=true
Add If Controller with the following condition: ${__P(runsomesampler,)} == true
Add desired HTTP Requests as a children of the IF Controller

Resources