garima
garima

Reputation: 111

how to delay a HTTP request when assertion failed - in recorded jmeter script

I have a recorded script for my web application load testing. In one of the HTTP requests, i need to check Response message for "Completed" status text. If Status is not "Completed" then i need to run a loop 5 times to check if status is "Completed".

I know about assertion where i can check for "Completed" status but not sure how to make HTTP request wait and check in loop for 5 times before it moves to next HTTP request.

Any help would be appreciated. Thanks!

Upvotes: 1

Views: 1326

Answers (2)

Guru
Guru

Reputation: 443

Please find the attached sample test plan implementing the above logic.

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
      <stringProp name="TestPlan.comments">This test plan demonstrates simple usage of Dummy Sampler</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>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
        <stringProp name="TestPlan.comments">Thread group is set to generate 50 samples</stringProp>
        <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>
          <intProp name="LoopController.loops">-1</intProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <longProp name="ThreadGroup.start_time">1300542586000</longProp>
        <longProp name="ThreadGroup.end_time">1300542586000</longProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <LoopController guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
          <boolProp name="LoopController.continue_forever">true</boolProp>
          <intProp name="LoopController.loops">-1</intProp>
        </LoopController>
        <hashTree>
          <IfController guiclass="IfControllerPanel" testclass="IfController" testname="If Controller" enabled="true">
            <stringProp name="IfController.condition">&quot;${Status}&quot;!=&quot;20&quot;</stringProp>
            <boolProp name="IfController.evaluateAll">false</boolProp>
          </IfController>
          <hashTree>
            <kg.apc.jmeter.samplers.DummySampler guiclass="kg.apc.jmeter.samplers.DummySamplerGui" testclass="kg.apc.jmeter.samplers.DummySampler" testname="jp@gc - Dummy Sampler" enabled="true">
              <stringProp name="TestPlan.comments">Dummy Sampler set to use random Response Code
For now, Response data used as sent and received data</stringProp>
              <boolProp name="SUCCESFULL">true</boolProp>
              <stringProp name="RESPONSE_CODE">${__Random(1,5)}0</stringProp>
              <stringProp name="RESPONSE_MESSAGE">OK</stringProp>
              <stringProp name="RESPONSE_DATA">Dummy Sampler used to simulate requests and responses
without actual network activity. This helps debugging tests.

Random number:${__Random(1,1000)}

</stringProp>
              <stringProp name="RESPONSE_TIME">${__Random(10,1000)}</stringProp>
              <boolProp name="WAITING">true</boolProp>
              <stringProp name="REQUEST_DATA">Dummy Sampler used to simulate requests and responses
without actual network activity. This helps debugging tests.</stringProp>
              <stringProp name="LATENCY">${__Random(1,100)}</stringProp>
              <stringProp name="CONNECT"></stringProp>
            </kg.apc.jmeter.samplers.DummySampler>
            <hashTree>
              <BeanShellAssertion guiclass="BeanShellAssertionGui" testclass="BeanShellAssertion" testname="BeanShell Assertion" enabled="true">
                <stringProp name="BeanShellAssertion.query">String S= SampleResult.getResponseCode();
if (S.equals(&quot;20&quot;))
{
//log.info(&quot;RESPONSE&quot;+S);
vars.put(&quot;Status&quot;,&quot;20&quot;);
}
</stringProp>
                <stringProp name="BeanShellAssertion.filename"></stringProp>
                <stringProp name="BeanShellAssertion.parameters"></stringProp>
                <boolProp name="BeanShellAssertion.resetInterpreter">false</boolProp>
              </BeanShellAssertion>
              <hashTree/>
            </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/>
          </hashTree>
        </hashTree>
        <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
          <collectionProp name="Arguments.arguments">
            <elementProp name="Status" elementType="Argument">
              <stringProp name="Argument.name">Status</stringProp>
              <stringProp name="Argument.value">0</stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
            </elementProp>
          </collectionProp>
        </Arguments>
        <hashTree/>
      </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>false</encoding>
            <assertions>true</assertions>
            <subresults>true</subresults>
            <responseData>false</responseData>
            <samplerData>false</samplerData>
            <xml>true</xml>
            <fieldNames>false</fieldNames>
            <responseHeaders>false</responseHeaders>
            <requestHeaders>false</requestHeaders>
            <responseDataOnError>false</responseDataOnError>
            <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
            <assertionsResultsToSave>0</assertionsResultsToSave>
            <bytes>true</bytes>
            <threadCounts>true</threadCounts>
          </value>
        </objProp>
        <stringProp name="TestPlan.comments">Run the test and see various results of Dummy Sampler
Response Assertion marks some of results as failed</stringProp>
        <stringProp name="filename"></stringProp>
      </ResultCollector>
      <hashTree/>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

Upvotes: 0

vins
vins

Reputation: 15370

You can use Loop Controller + If Controller to achieve your requirement.

The test plan should be almost as given below.

enter image description here

Lets assume we have a variable completed.status = false

If condition should be "${completed.status}"=="false" to make a first call by default to get the status.

The Beanshell assertion should change the completed.status = true whenever the condition is met.

If the condition is not met - Beanshell should create another variable for the timer vars.put("wait", "3000"). Use ${wait} for the timer to wait for 3 seconds to make another call. So that timer does not delay if the status is 'completed' in the first call itself!!

Upvotes: 1

Related Questions