Bastian
Bastian

Reputation: 1237

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

Upvotes: 0

Views: 620

Answers (2)

Dmitri T
Dmitri T

Reputation: 168002

It is recommended to avoid scripting where possible so:

  1. 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
  2. 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.

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58774

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

Upvotes: 1

Related Questions