Search code examples
jmeterbeanshell

how to change the value of a variable in Jmeter


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.

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.


Solution

  • 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