Reputation: 25
I am trying to use the variables captured in the Debug Sampler in Jmeter and then convert those variables into some other value. And then use it somewhere in the script.
I have added a BeanShell Sampler along with the Debug Sampler and tried to get the variables displayed in the Debug Sampler. Below is the piece of code I have written in Jmeter.
Is my approach correct? Am completely new to Jmeter and have little Java knowledge. So please help me here and let me know how can I convert or use a variable through a Custom code in Jmeter.
Upvotes: 1
Views: 4596
Reputation: 168002
It is almost correct, you have a couple of syntax errors (missing closing bracket and undefined SomeCharacter
)
Also it is better to use JSR223 Elements and Groovy language rather than Beanshell, as Groovy performance is much better and it is more Java-compliant, see Groovy Is the New Black article for detailed explanation.
Final code should look something like:
def myVariable = vars.get("Corr_ContextN")
if (myVariable.equals("002056653")) {
vars.put("myvariable1", "SomeCharacter")
}
Keep in mind that you are not changing original Corr_ContextN
, you are creating new variable myvariable1
. Also in order to see new variable you need to move the Debug Sampler after the Beanshell Sampler
Upvotes: 1
Reputation: 34516
Your got the concept but your code has the following errors:
imports are wrong and useless. You only need to import Classes for unbound variables, ie the ones that are advertised in the component.
There's a missing ')' in the if clause
SomeCharacter is not defined
And you should avoid Beanshell and favor JSR223 Test Element with Groovy as per those recommandations:
Note that there is also a __groovy function for your use case.
Upvotes: 0