如何在断言失败时延迟HTTP请求 - 在记录的jmeter脚本中

时间:2016-02-24 22:45:54

标签: jmeter load-testing

我有一个用于我的Web应用程序负载测试的录制脚本。在其中一个HTTP请求中,我需要检查“已完成”状态文本的响应消息。如果状态不是“已完成”,那么我需要运行一次循环5次以检查状态是否为“已完成”。

我知道断言我可以检查“已完成”状态但不确定如何在转移到下一个HTTP请求之前使HTTP请求等待并检查循环5次。

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用循环控制器 + 如果控制器来满足您的要求。

测试计划应该几乎如下所示。

enter image description here

让我们假设我们有一个变量completed.status = false

如果条件应为"${completed.status}"=="false",则默认情况下首先调用以获取状态。

Beanshell断言应该在条件满足时更改completed.status = true

如果不满足条件 - Beanshell应为计时器vars.put("wait", "3000")创建另一个变量。使用${wait}计时器等待3秒钟再拨打一个电话。因此,如果第一个呼叫本身的状态为“已完成”,则定时器不会延迟!!

答案 1 :(得分:0)

请查看实施上述逻辑的附件样本测试计划。

<?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>