It’s always good to use user defined variable in JMeter test plan. This way one can easily configure and customize the test plan. It definitely works in GUI but what if you want to execute the test plan using command line. You can only pass the property value using command line.

Note:
Command line execution takes less time than GUI execution

But there is way by which you can override JMeter user defined variables. Usually you use __P() method to access the property value and set the user defined variable. You can also pass some default value in the function so if property value is not provided in the command line then the default value will be set.

Sample code

Check the following picture for the test plan.

Override JMeter user defined variable1

Override JMeter user defined variable1

As you can see in the picture myvar is set by calling __P() function which basically try to access myprop property and set the myvar value. If myprop is not present then default value is return by the function in this case it returns 10.

Override JMeter user defined variable2

Override JMeter user defined variable2


Here is how we are accessing the user defined variable in BeanShell sampler.

<jmeterTestPlan version="1.2" properties="2.4" jmeter="2.9 r1437961">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" 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="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments">
          <elementProp name="myvar" elementType="Argument">
            <stringProp name="Argument.name">myvar</stringProp>
            <stringProp name="Argument.value">${__P(myprop, 10)}</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
        </collectionProp>
      </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">1371241972000</longProp>
        <longProp name="ThreadGroup.end_time">1371241972000</longProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <BeanShellSampler guiclass="BeanShellSamplerGui" testclass="BeanShellSampler" testname="BeanShell Sampler" enabled="true">
          <stringProp name="BeanShellSampler.query">log.info(&quot;Just simple logging here.&quot;);
log.info(&quot;myvar : &quot; + vars.get(&quot;myvar&quot;));
return vars.get(&quot;myvar&quot;);</stringProp>
          <stringProp name="BeanShellSampler.filename"></stringProp>
          <stringProp name="BeanShellSampler.parameters"></stringProp>
          <boolProp name="BeanShellSampler.resetInterpreter">false</boolProp>
        </BeanShellSampler>
        <hashTree/>
        <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>true</dataType>
              <encoding>true</encoding>
              <assertions>true</assertions>
              <subresults>true</subresults>
              <responseData>true</responseData>
              <samplerData>true</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>true</responseHeaders>
              <requestHeaders>true</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <sampleCount>true</sampleCount>
            </value>
          </objProp>
          <stringProp name="filename">/home/rakeshHome2/sprint/finder_jmeter/propertyTestlog</stringProp>
        </ResultCollector>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

You can run the following command to execute the sample test plan.

/> jmeter -n -t propertyTest.jmx -Jmyprop=200
Note:
-J option basically helps to set the system or user property value.

You can also download the Jmeter-test plan from github.

Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below.

, ,
Trackback

no comment untill now

Add your comment now