Search code examples
jmeterbeanshelljmeter-3.2

Variable appears to be lost when setting a property in jmeter


I want create a property from a variable. The variables was created by calling a variable from a xpath extraction, then using a substring to then get the last 4 characters. The substring string value is saved to a variable, then set to a property.

When I run the script, the log.info(vars.get("lastcard")); returns the value of the variable. However it then fails to save to a property, because when that property is called(${__property(lastNum)} it will display - ${lastcard}

      import org.apache.jmeter.util.JMeterUtils;
      import org.apache.commons.lang3;


      String tesTe = vars.get("card");
      String last4 = tesTe.substring(tesTe.length()-4,tesTe.length());
      vars.put("lastcard", String.valueOf(last4));
      log.info(vars.get("lastcard"));

     ${__setProperty(lastNum,${lastcard})};

Any ideas as to what is going on


Solution

  • You should read user manual about scripting:

    ensure the script does not use any variable using ${varName}

    You should use JSR223 variables vars and props to handle variables and properties. In your case change last line to:

     props.put("lastNum",  vars.get("lastcard"));
    

    Also you can set variable in shorter way:

      vars.put("lastcard", vars.get("card").substring(tesTe.length()-4));