JMeter 如果控制器不工作

Tra*_*vis 5 jmeter

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

 >ThreadGroup
   >Http Request
      >Response Assertion
   >Summary Report
   >BeanShell Listener
   >If Controller
      >SMTP Sampler
Run Code Online (Sandbox Code Playgroud)

因此,练习 SMTP 采样器的最佳方法似乎是使用 JMeterThread.last_sample_ok 变量。我正在 BeanShell Listener 中测试这个变量并且它有效。如果我关闭正在测试的 REST 服务,我可以看到该变量为 false,如果我启动该服务,它会返回 true。完美的。在 If 控制器中,当我输入 JMeterThread.last_sample_ok 时,我会在服务开启时收到一封电子邮件。太棒了...按其应有的方式工作。问题:当我关闭服务导致断言失败并将 If 控制器更改为相反的值:!JMeterThread.last_sample_ok 时,我从未收到电子邮件。当预期断言失败时,If 控制器似乎永远不会响应,而且我从未收到电子邮件。我可以证明该变量在 BeanShell 侦听器中正确转换...我可以看到在失败情况下,!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>
Run Code Online (Sandbox Code Playgroud)

Kir*_* S. 4

好吧,这是因为在您添加布尔运算符的那一刻!,您的条件不能再解释为变量表达式。相反,它需要使用语言(默认为 JavaScript)进行评估。

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

您有多种选择:

  • 取消选中Interpret Condition as Variable Expression,以便使用 JavaScript 评估该条件。但正如文档所述,这样做会带来性能损失。

  • 反转您的响应断言条件,以便在不需要发送电子邮件时失败。这样你就可以让 If 控制器变得简单${JMeterThread.last_sample_ok}

  • 您还可以在 Http 请求后处理器中创建自定义变量,该变量将采用与断言相反的值,但计算结果为truefalse。在 If 控制器中使用该变量。