How to increase variable which I get in response and assign new value in next request JMETER

StackOverflow https://stackoverflow.com/questions/23393976

  •  12-07-2023
  •  | 
  •  

Question

For example: In response jmeter give me TOT_CHG = 4056,6. Now I want to increase this variable TOT_CHG = 4056,6 + 100 and assign new increased value as a variable in next request. How can I do that?

enter image description here

enter image description here

enter image description here

Was it helpful?

Solution

First you need to add a Regular Expression Extractor to extract the value of TOT_GEN_CHG to put it in var TOT_CHG.

Using JSR223 Post processor and Groovy as language:

import java.util.*;
import java.text.*;
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',' as char);
NumberFormat format = new DecimalFormat ("000.00");
format.setDecimalFormatSymbols(symbols);
Float parsed = (Float) format.parse(vars["TOT_GEN_CHG"]).floatValue(); 
parsed += 100;
vars.put("NEW_VAR", format.format(parsed));

You can then use ${NEW_VAR}

To configure Groovy in jmeter, download it and unzip it, then add embeddable/groovy-all.jar in jmeter/lib folder and restart jmeter.

See example:

        <?xml version="1.0" encoding="UTF-8"?>
    <jmeterTestPlan version="1.2" properties="2.6" jmeter="2.12-SNAPSHOT.20140413">
      <hashTree>
        <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Plan testów" enabled="true">
          <stringProp name="TestPlan.comments"></stringProp>
          <boolProp name="TestPlan.functional_mode">false</boolProp>
          <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
          <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="Zmienne zdefiniowane przez użytkownika" enabled="true">
            <collectionProp name="Arguments.arguments"/>
          </elementProp>
          <stringProp name="TestPlan.user_define_classpath"></stringProp>
        </TestPlan>
        <hashTree>
          <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
            <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
            <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
              <boolProp name="LoopController.continue_forever">false</boolProp>
              <stringProp name="LoopController.loops">1</stringProp>
            </elementProp>
            <stringProp name="ThreadGroup.num_threads">1</stringProp>
            <stringProp name="ThreadGroup.ramp_time">1</stringProp>
            <longProp name="ThreadGroup.start_time">1399401625000</longProp>
            <longProp name="ThreadGroup.end_time">1399401625000</longProp>
            <boolProp name="ThreadGroup.scheduler">false</boolProp>
            <stringProp name="ThreadGroup.duration"></stringProp>
            <stringProp name="ThreadGroup.delay"></stringProp>
          </ThreadGroup>
          <hashTree>
            <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
              <collectionProp name="Arguments.arguments">
                <elementProp name="TOT_GEN_CHG" elementType="Argument">
                  <stringProp name="Argument.name">TOT_GEN_CHG</stringProp>
                  <stringProp name="Argument.value">123,23</stringProp>
                  <stringProp name="Argument.metadata">=</stringProp>
                </elementProp>
              </collectionProp>
            </Arguments>
            <hashTree/>
            <DebugSampler guiclass="TestBeanGUI" testclass="DebugSampler" testname="Debug Sampler" enabled="true">
              <boolProp name="displayJMeterProperties">false</boolProp>
              <boolProp name="displayJMeterVariables">true</boolProp>
              <boolProp name="displaySystemProperties">false</boolProp>
            </DebugSampler>
            <hashTree>
              <JSR223PostProcessor guiclass="TestBeanGUI" testclass="JSR223PostProcessor" testname="JSR223 PostProcessor" enabled="true">
                <stringProp name="cacheKey"></stringProp>
                <stringProp name="filename"></stringProp>
                <stringProp name="parameters"></stringProp>
                <stringProp name="script">import java.util.*;
    import java.text.*;
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator(&apos;,&apos; as char);
    NumberFormat format = new DecimalFormat (&quot;000.00&quot;);
    format.setDecimalFormatSymbols(symbols);
    //NumberFormat format = NumberFormat.getIntegerInstance(Locale.FRENCH);
    Float parsed = (Float) format.parse(vars[&quot;TOT_GEN_CHG&quot;]).floatValue(); 
    parsed += 100;
    vars.put(&quot;NEW_VAR&quot;, format.format(parsed));</stringProp>
                <stringProp name="scriptLanguage">groovy</stringProp>
              </JSR223PostProcessor>
              <hashTree/>
            </hashTree>
            <DebugSampler guiclass="TestBeanGUI" testclass="DebugSampler" testname="Debug Sampler-${NEW_VAR}" enabled="true">
              <boolProp name="displayJMeterProperties">false</boolProp>
              <boolProp name="displayJMeterVariables">true</boolProp>
              <boolProp name="displaySystemProperties">false</boolProp>
            </DebugSampler>
            <hashTree/>
          </hashTree>
        </hashTree>
      </hashTree>
    </jmeterTestPlan>

OTHER TIPS

Basically, you should extract value of TOT_CHG into variable let's call it MY_VAR, create script (for example in BeanShell Pre- or PostProcessor) where you increase the value of variable, and use variable in the next request as ${MY_VAR}.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top