How to use jsr223 variables in loop controller in jmeter - testing

I have created a list of lists in jsr223 preprocessor and want to run a loop for size of list and print the elements in list in post processor inside the loop. But i am unable to use the count variable in loop controller.
The end result i need is in first iteration of loop [1,2,3,4,5]
second iteration [6,7,8,9,10]
at present im getting no output because loop controller is not reading count variable.

JSR223 PreProcessor is being executed before request so by the time your Loop Controller is being kicked off your count variable is not initialized yet.
Convert JSR223 PreProcessor into a JSR223 Sampler and your code should start working as expected.
If you don't want JSR223 Sampler to appear in your test results add the next line to your script:
SampleResult.setIgnore()

Related

How to pass values from variable in setProperty of jmeter

I have a setup thread group which sets the property value and in Thread group i am using the variable in csv data set configure
it is working if i am giving values like
${__setProperty(${name},_id.csv)} but if i take _id.csv from an array it is not reading the value.
Don't inline JMeter Functions or Variables into Groovy scripts as:
It may resolve into something causing script compilation failure
It may conflict with Groovy GString templates
It conflicts with compilation caching feature
As per JSR223 Sampler documentation:
JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.
props.get("START.HMS");
props.put("PROP1","1234");
So you need to amend your code as follows:
def name = 'file'
def files = ['_id.csv']
props.put(name, files[0])
Check out Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.

Use JMeter Variable in Groovy Code

I have an Config Element in JMeter, Especially User Defined Variables.
I have the variable "user" with the value "Justin", How can I use this variable in the groovy code (of an JSR223 Assertion)?
There are several of getting it:
Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as Parameters which contains the full string passed in the "Parameters" section
Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as args[0] (if you pass more than one variable separated by spaces you will be able to refer 2nd variable as args[1], 3rd as args[2], etc.
You can access it as vars.get('user') where vars stands for JMeterVariables class instance
You can access it as vars['user'] - basically the same as point 3 but uses some Groovy Syntax Sugar
You can access it as ctx.getVariables().get('user') - where ctx stands for JMeterContextService class instance just in case (in some test elements vars shorthand is not available)
Demo:
Any JSR223 element including Assertion have few variables it can use out of the box.
One of the variable is vars which is basically a map of JMeter stored variables.
User Defined Variables row is creating a JMeter variable, so you can get your value Justin in JSR223 using vars.get("user")

Error 500 while trying to set a variable in Jmeter

I tried to create a variable in groovy and Jmeter.
I want it to be the counter of a while loop that I am planning to run.
I want in each iteration to add 1 to this counter.
the problem is that I can not set it to be variable, that I could use later in the program in another sampler.
int while_counter = 0;
vars.put("while_counter",0);
System.out.println("Loop Counter");
I just want to create an integer that will be a counter and all the sampler will know, and can address him ${while_counter}.
and to perform while_counter = while_counter++
what I am missing
Response code: 500
Response message: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer) values: [while_counter, 0]
Possible solutions: put(java.lang.String, java.lang.String), get(java.lang.String), putAt(java.lang.String, java.lang.Object), wait(), any(), dump()
Can someone please advise how to create a simple while loop in Jmeter
and to add 1 to counter
Define variable "i" as 0/1 at start of Test Plan in User Defined Variable/User Parameters
While loop condition:
${__groovy(vars["i"].toInteger() < 5)}
Inside loop add your JSR223 Sampler/Preprocessor with increment it:
String i = vars.get("i");
int counter = Integer.parseInt(i);
counter++;
vars.put("i", "" + counter);
Don't use Javascript function as it doesn't scale as well as Groovy
It is recommended to avoid scripting where possible so:
If you are running an "internal" loop driven by Loop Controller or While Controller it makes sense to add a Counter test element which will generate an incrementing number each time it will be called. You will be able to refer counter's current value using "normal" JMeter variable defined in the counter's "Reference Name" section
For Thread Group level iterations you can use __iterationNum() function
With regards to your question itself, you cannot pass an integer to vars.put() method if you need to store an integer in JMeter Variables you should go for vars.putObject() method instead.

Using Beanshell sampler return value in JMeter while condition

I am trying to use the return value from beanshell sampler, later in the while condition ${__javaScript(${homeState}<6)}.
When i run and check logs, ${homeState} is not getting replaced with the beanshell sampler homeState integer value.
Can any one suggest what's wrong going on?
however, when i check the response of beanshell sampler in view results tree, it is returing integer as expected.
return keyword in Beanshell defines Beanshell sampler response data. If you need to store the value into a JMeter Variable you should replace the line return homeState with the following expression:
vars.put("homeState", String.valueOf(homeState));
vars is a shorthand to JMeterVariables class instance, it provides read/write access to the JMeter Variables in scope.
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on using Beanshell scripting in JMeter tests

Can I use e.parameter in conjunction with a global variable to retrieve an external variable in Google Apps Scripts?

I realize the question is confusing. I'm trying to reference many widgets that were created in the main loop of my script from a secondary function using e.parameter.
Instead of referencing each e.parameter separately, by its name, I'd like to be able to make one reference to e.parameter and have the parameter name portion be a globally defined variable.
As in:
e.parameter.some_id
Would be the same as:
var test=[]
test[0]='some_id'
e.parameter.(test[0])
Or some other syntax. I'm trying to reference the parameters in a loop, and using the array means I can increment a for loop counter instead doing if tests for each parameter individually.
I'm certain there's an easier way to do this, but I'm still new to java.
Use e.parameter[test[0]] . It is not java but JavaScript