JMeter如果控制器不工作

时间:2018-05-18 19:09:00

标签: jmeter

JMeter 4.0中非常直接的要求。在jmeter中运行简单测试,如果失败则发送电子邮件。所以我有这个:

 >ThreadGroup
   >Http Request
      >Response Assertion
   >Summary Report
   >BeanShell Listener
   >If Controller
      >SMTP Sampler

因此,运行SMTP Sampler的最佳方法是使用JMeterThread.last_sample_ok变量。我在BeanShell Listener中测试这个变量,它可以工作。如果我关闭我正在测试的REST服务,我可以看到该变量为false,如果我启动该服务,则返回true。完善。在If Controller中,当我输入JMeterThread.last_sample_ok时,我会在服务开启时收到一封电子邮件。太棒了......按原样工作。问题:当我关闭服务以便断言失败并且我将If控制器更改为反向:!JMeterThread.last_sample_ok时,我从未收到电子邮件。如果预期失败的断言,If Controller似乎永远不会响应,我永远不会收到我的电子邮件。我可以证明变量在BeanShell Listener中正确转换...我可以看到在失败的情况下,!JMeterThread.last_sample_ok转换为" true"所以If控制器中的相同代码应该可以工作。我究竟做错了什么?这是XML文件......

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="4.0" jmeter="4.0 r1823414">
  <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.tearDown_on_shutdown">true</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="AddressValidation 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>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="AddressValidation Request" enabled="true">
          <boolProp name="HTTPSampler.postBodyRaw">true</boolProp>
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments">
            <collectionProp name="Arguments.arguments">
              <elementProp name="" elementType="HTTPArgument">
                <boolProp name="HTTPArgument.always_encode">false</boolProp>
                <stringProp name="Argument.value">{&#xd;
  &quot;address&quot;: {&#xd;
    &quot;address2&quot;: &quot;1625&quot;,&#xd;
    &quot;city&quot;: &quot;austin&quot;,&#xd;
    &quot;address1&quot;: &quot;4701 Staggerbrush Rd&quot;,&#xd;
    &quot;zip4&quot;: &quot;&quot;,&#xd;
    &quot;state&quot;: &quot;tx&quot;,&#xd;
    &quot;zip5&quot;: &quot;78749&quot;&#xd;
  }&#xd;
}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="HTTPSampler.domain">localhost</stringProp>
          <stringProp name="HTTPSampler.port">9095</stringProp>
          <stringProp name="HTTPSampler.protocol">http</stringProp>
          <stringProp name="HTTPSampler.contentEncoding"></stringProp>
          <stringProp name="HTTPSampler.path">/address-standardization</stringProp>
          <stringProp name="HTTPSampler.method">POST</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
          <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
          <stringProp name="HTTPSampler.response_timeout"></stringProp>
        </HTTPSamplerProxy>
        <hashTree>
          <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
            <collectionProp name="HeaderManager.headers">
              <elementProp name="" elementType="Header">
                <stringProp name="Header.name">Content-Type</stringProp>
                <stringProp name="Header.value">application/json</stringProp>
              </elementProp>
            </collectionProp>
          </HeaderManager>
          <hashTree/>
          <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
            <collectionProp name="Asserion.test_strings">
              <stringProp name="-1781066058">{&quot;address&quot;:{&quot;address1&quot;:&quot;APT 1625&quot;,&quot;address2&quot;:&quot;4701 STAGGERBRUSH RD&quot;,&quot;city&quot;:&quot;AUSTIN&quot;,&quot;state&quot;:&quot;TX&quot;,&quot;zip5&quot;:&quot;78749&quot;,&quot;zip4&quot;:&quot;1048&quot;}}</stringProp>
            </collectionProp>
            <stringProp name="Assertion.custom_message"></stringProp>
            <stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
            <boolProp name="Assertion.assume_success">false</boolProp>
            <intProp name="Assertion.test_type">16</intProp>
          </ResponseAssertion>
          <hashTree/>
        </hashTree>
        <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="AddressValidation Response" enabled="true">
          <boolProp name="ResultCollector.error_logging">true</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>false</xml>
              <fieldNames>false</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <sentBytes>true</sentBytes>
              <threadCounts>true</threadCounts>
              <idleTime>true</idleTime>
              <connectTime>true</connectTime>
            </value>
          </objProp>
          <stringProp name="filename">C:\test\reports\response_out.csv</stringProp>
        </ResultCollector>
        <hashTree/>
        <ResultCollector guiclass="SummaryReport" testclass="ResultCollector" testname="Summary Report" enabled="true">
          <boolProp name="ResultCollector.error_logging">true</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>false</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>false</threadName>
              <dataType>false</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>true</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <connectTime>true</connectTime>
            </value>
          </objProp>
          <stringProp name="filename">C:\test\reports\AddressValidation.jtl</stringProp>
        </ResultCollector>
        <hashTree/>
        <BeanShellListener guiclass="TestBeanGUI" testclass="BeanShellListener" testname="BeanShell Listener" enabled="true">
          <stringProp name="filename"></stringProp>
          <stringProp name="parameters"></stringProp>
          <boolProp name="resetInterpreter">false</boolProp>
          <stringProp name="script">log.debug( &quot;should_be_false=&quot; + !${JMeterThread.last_sample_ok} );
</stringProp>
        </BeanShellListener>
        <hashTree/>
        <IfController guiclass="IfControllerPanel" testclass="IfController" testname="If Controller" enabled="true">
          <stringProp name="IfController.condition">!${JMeterThread.last_sample_ok}</stringProp>
          <boolProp name="IfController.evaluateAll">true</boolProp>
          <boolProp name="IfController.useExpression">true</boolProp>
        </IfController>
        <hashTree>
          <SmtpSampler guiclass="SmtpSamplerGui" testclass="SmtpSampler" testname="SMTP Sampler" enabled="true">
            <stringProp name="SMTPSampler.server">ezmail-out.xyz.com</stringProp>
            <stringProp name="SMTPSampler.serverPort">25</stringProp>
            <stringProp name="SMTPSampler.mailFrom">freddie_mercury@xyz.com</stringProp>
            <stringProp name="SMTPSampler.replyTo"></stringProp>
            <stringProp name="SMTPSampler.receiverTo">brian_may@xyz.com</stringProp>
            <stringProp name="SMTPSampler.receiverCC"></stringProp>
            <stringProp name="SMTPSampler.receiverBCC"></stringProp>
            <stringProp name="SMTPSampler.subject">AddressValidation Test Result</stringProp>
            <stringProp name="SMTPSampler.suppressSubject">false</stringProp>
            <stringProp name="SMTPSampler.include_timestamp">true</stringProp>
            <stringProp name="SMTPSampler.message"></stringProp>
            <stringProp name="SMTPSampler.plainBody">false</stringProp>
            <stringProp name="SMTPSampler.attachFile">C:\test\reports\AddressValidation.jtl</stringProp>
            <stringProp name="SMTPSampler.useSSL">false</stringProp>
            <stringProp name="SMTPSampler.useStartTLS">false</stringProp>
            <stringProp name="SMTPSampler.trustAllCerts">false</stringProp>
            <stringProp name="SMTPSampler.enforceStartTLS">false</stringProp>
            <stringProp name="SMTPSampler.useLocalTrustStore">false</stringProp>
            <stringProp name="SMTPSampler.trustStoreToUse"></stringProp>
            <boolProp name="SMTPSampler.use_eml">false</boolProp>
            <stringProp name="SMTPSampler.emlMessageToSend"></stringProp>
            <stringProp name="SMTPSampler.useAuth">false</stringProp>
            <stringProp name="SMTPSampler.password"></stringProp>
            <stringProp name="SMTPSampler.username"></stringProp>
            <stringProp name="SMTPSampler.messageSizeStatistics">false</stringProp>
            <stringProp name="SMTPSampler.enableDebug">false</stringProp>
            <collectionProp name="SMTPSampler.headerFields"/>
          </SmtpSampler>
          <hashTree/>
        </hashTree>
      </hashTree>
      <PostThreadGroup guiclass="PostThreadGroupGui" testclass="PostThreadGroup" testname="tearDown 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>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </PostThreadGroup>
      <hashTree/>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

2 个答案:

答案 0 :(得分:1)

嗯,这是因为目前你添加了布尔运算符!,你的条件不再是解释为变量表达式。相反,它需要使用语言(默认情况下为javascript)进行评估。

换句话说,如果您选中Interpret Condition as Variable Expression复选框,则会将条件与单词true进行比较 - 就是这样!如果在它前面添加感叹号,它将变为!true并且将失败。

您有几种选择:

  • 取消选中Interpret Condition as Variable Expression,以便使用JavaScript评估条件。但是作为documentation states,会有性能损失。

  • 撤消响应断言条件,以便在无需发送电子邮件时失败。这样就可以使控制器变得简单${JMeterThread.last_sample_ok}

  • 您还可以在Http Request后处理器中创建自定义变量,该变量将采用与断言相反的值,但将评估为truefalse。在If Controller中使用该变量。

答案 1 :(得分:0)

修改您的If Controller的条件,使用__groovy() function,如:

${__groovy(vars.get('JMeterThread.last_sample_ok').equals('false'),)}

另请注意,根据JMeter Best Practices Beanshell脚本是反模式(以及将JMeter函数内联到脚本中),因此请考虑迁移到JSR223 ListenerGroovy语言并使用JMeter变量访问vars简写,代表JMeterVariables类实例

JMeter Vars from JSR223 and Groovy