JMeter JSON 带输入变量的路径提取器

JMeter JSON Path Extractor with input variable

是否可以在 JSON 路径提取器表达式中使用输入变量?

我试过表达式$.[${someInputVariable}].name,但没用。

数据格式:

[ 
{name=a}, 
{name=b}, 
{name=c}
]

如果例如${inputVariable}设置为1,我希望表达式为return"b"。

到目前为止我找到的所有示例都有硬编码的搜索值,例如 $.[1].name$..[?(@.name == 'Smith')]

首先你JSON错了,应该是:

 [{"name":"a"}, {"name":"b"},{"name":"c"}]

要提取您想要的内容,只需使用 __eval 函数:

 ${__eval($.[${someInputVariable}].name)}

我查看了 JSONPostProcessor 源代码: https://github.com/apache/jmeter/blob/740dd7420af6ef576af5e6ce2646ede360748691/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java

您不能将变量放在 json 路径表达式中,因为这些变量不会被计算。顺便说一句,如果您需要帮助,可以阅读这篇关于 JMeter json path extractor 的文章。它为 json 路径提取器提供了更多示例和替代方案。

答案 是完全错误的:json 路径表达式中的变量被评估并且至少可以从 JMeter 3.1 开始使用(我没有检查早期版本)。

甚至不需要加${__eval()}。您的示例不起作用,因为您使用了错误的表达式。正确的应该是$[${someInputVariable}].name。 $ 和 [ 之间没有点。 JSON 也错了。

这是一个 jmx 示例,您的数据根据​​ ${someInputVariable} 的值填充 ${NAME-VARIABLE}。要检查它是如何工作的,只需在 JMeter 中将其保存为 jmx,运行 并检查 "View Results Tree" 元素中 "Debug PostProcessor" 叶的 "Response data" 选项卡上的变量值。 :

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="3.1" jmeter="3.1 r1770033">
  <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>
      <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments">
          <elementProp name="someInputVariable" elementType="Argument">
            <stringProp name="Argument.name">someInputVariable</stringProp>
            <stringProp name="Argument.value">2</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
          <elementProp name="JSONToBeParsed" elementType="Argument">
            <stringProp name="Argument.name">JSONToBeParsed</stringProp>
            <stringProp name="Argument.value">[{&quot;name&quot;:&quot;a&quot;},{&quot;name&quot;:&quot;b&quot;},{&quot;name&quot;:&quot;c&quot;}]</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
        </collectionProp>
      </Arguments>
      <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>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <longProp name="ThreadGroup.start_time">1535106337000</longProp>
        <longProp name="ThreadGroup.end_time">1535106337000</longProp>
      </ThreadGroup>
      <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>
          <JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor with constant" enabled="true">
            <stringProp name="JSONPostProcessor.referenceNames">NAME-CONSTANT</stringProp>
            <stringProp name="JSONPostProcessor.jsonPathExprs">$[1].name</stringProp>
            <stringProp name="JSONPostProcessor.match_numbers"></stringProp>
            <stringProp name="Sample.scope">variable</stringProp>
            <stringProp name="Scope.variable">JSONToBeParsed</stringProp>
          </JSONPostProcessor>
          <hashTree/>
          <JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor with variable" enabled="true">
            <stringProp name="JSONPostProcessor.referenceNames">NAME-VARIABLE</stringProp>
            <stringProp name="JSONPostProcessor.jsonPathExprs">$[${someInputVariable}].name</stringProp>
            <stringProp name="JSONPostProcessor.match_numbers"></stringProp>
            <stringProp name="Sample.scope">variable</stringProp>
            <stringProp name="Scope.variable">JSONToBeParsed</stringProp>
          </JSONPostProcessor>
          <hashTree/>
          <DebugPostProcessor guiclass="TestBeanGUI" testclass="DebugPostProcessor" testname="Debug PostProcessor" enabled="true">
            <boolProp name="displayJMeterProperties">false</boolProp>
            <boolProp name="displayJMeterVariables">true</boolProp>
            <boolProp name="displaySamplerProperties">true</boolProp>
            <boolProp name="displaySystemProperties">false</boolProp>
          </DebugPostProcessor>
          <hashTree/>
        </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>false</xml>
            <fieldNames>true</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"></stringProp>
      </ResultCollector>
      <hashTree/>
    </hashTree>
    <WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="WorkBench" enabled="true">
      <boolProp name="WorkBench.save">true</boolProp>
    </WorkBench>
    <hashTree/>
  </hashTree>
</jmeterTestPlan>