Error 500 while trying to set a variable in Jmeter - variables

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.

Related

How to construct the By.xpath method for customized locators?

I'm trying to use the By method for the setting of variables in my page object class. One of my scenario requires the table to loop with customized value based on user input.
So I had to write customized xpath. But when trying to write to fit into the By method i'm stuck on how to handle the iteration number. For example the below shows my locator:
By test = By.xpath("//thead/tr[1]/th[" + i + "]"));
It shows error for the "i" value in the declaration, even if int i ; is declared.
Please let me know how to handle this.
As the variable i is of type integer, you have to convert it into a string before constructing the effective locator strategy as follows:
By test = By.xpath("//thead/tr[1]/th[" + toString(i) + "]"));

Why can't a sub access dynamic variables when used in a "return"ed map in raku?

It seems that a a sub cannot access dynamic variables when it is used inside a map and that map is "return"ed.
Consider this piece of code:
sub start {
my $*something = 'foobar';
# WORKS
say 'first say-something:';
say-something;
# WORKS
say 'mapped say-something, no return:';
my #foo = (^2).map({say-something});
# ERROR: Dynamic variable $*something not found
say 'mapped say-something, with return:';
return (^2).map({say-something});
}
sub say-something {
say $*something;
1
}
start;
This will output:
first say-something:
foobar
mapped say-something, no return:
foobar
foobar
mapped say-something, with return:
Dynamic variable $*something not found
in sub say-something at main.raku line 18
in block <unit> at main.raku line 14
Why can't the sub access the dynamic variable? Is there a workaround for this?
Routine map is lazy, so the block is not run until after start has returned.
From the documentation:
multi method map(Hash:D \hash)
multi method map(Iterable:D \iterable)
multi method map(|c)
multi method map(\SELF: &block;; :$label, :$item)
multi sub map(&code, +values)
Examples applied to lists are included here for the purpose of
illustration.
For a list, it invokes &code for each element and gathers the return
values in a sequence and returns it. This happens lazily, i.e. &code
is only invoked when the return values are accessed.
So in your case, the map is evaluated once start returns, thus the dynamic variable is already out of scope.
A workaround for this is to force the map to happen eagerly by adding .eager
return (^2).map({say-something}).eager

How to use jsr223 variables in loop controller in jmeter

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()

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")

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