JMeter在beanshell中设置用户定义的变量不起作用

时间:2015-08-31 10:34:08

标签: java jmeter beanshell

我想在beanhell中覆盖Jmeter中的用户定义变量。 我triead beanshell预处理器,postprecessor或样本,但没有任何工作。

我的代码:

vars.put("box_user", "mybox");
log.info(vars.get("box_user"));

输出正确,mybox。 但是当我稍后使用$ {box_user}在采样器中使用变量时,用户名不正确。它具有开始初始化的价值。

有什么问题?

这是我的jmeter项目的xml,尝试了另一个变量名,但它不起作用:

<?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"></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="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>
        <longProp name="ThreadGroup.start_time">1441359197000</longProp>
        <longProp name="ThreadGroup.end_time">1441359197000</longProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <BeanShellSampler guiclass="BeanShellSamplerGui" testclass="BeanShellSampler" testname="BeanShell Sampler" enabled="true">
          <stringProp name="BeanShellSampler.query">vars.put(&quot;user&quot;, &quot;one&quot;);
vars.put(&quot;host&quot;, &quot;localhost&quot;);</stringProp>
          <stringProp name="BeanShellSampler.filename"></stringProp>
          <stringProp name="BeanShellSampler.parameters"></stringProp>
          <boolProp name="BeanShellSampler.resetInterpreter">false</boolProp>
        </BeanShellSampler>
        <hashTree/>
        <JDBCDataSource guiclass="TestBeanGUI" testclass="JDBCDataSource" testname="JDBC Connection Configuration" enabled="true">
          <stringProp name="dataSource">dbcon</stringProp>
          <stringProp name="poolMax">10</stringProp>
          <stringProp name="timeout">10000</stringProp>
          <stringProp name="trimInterval">60000</stringProp>
          <boolProp name="autocommit">true</boolProp>
          <stringProp name="transactionIsolation">DEFAULT</stringProp>
          <boolProp name="keepAlive">true</boolProp>
          <stringProp name="connectionAge">5000</stringProp>
          <stringProp name="checkQuery">Select 1</stringProp>
          <stringProp name="dbUrl">jdbc:postgresql://localhost:5432/box</stringProp>
          <stringProp name="driver">org.postgresql.Driver</stringProp>
          <stringProp name="username">${user}</stringProp>
          <stringProp name="password">pwd</stringProp>
        </JDBCDataSource>
        <hashTree/>
        <JDBCSampler guiclass="TestBeanGUI" testclass="JDBCSampler" testname="JDBC Request" enabled="true">
          <stringProp name="dataSource">dbcon</stringProp>
          <stringProp name="queryType">Select Statement</stringProp>
          <stringProp name="query">SELECT 1</stringProp>
          <stringProp name="queryArguments"></stringProp>
          <stringProp name="queryArgumentsTypes"></stringProp>
          <stringProp name="variableNames"></stringProp>
          <stringProp name="resultVariable"></stringProp>
          <stringProp name="queryTimeout"></stringProp>
          <stringProp name="resultSetHandler">Store as String</stringProp>
        </JDBCSampler>
        <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>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="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

错误是: 致命:角色&#34; $ {user}&#34;不存在 - &gt;所以我的变量不能替换,我不知道为什么

3 个答案:

答案 0 :(得分:4)

您是否有机会在不同的线程组中引用变量?如果是,则需要使用JMeter Properties,如:

public void onCreate(final Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    setContentView(R.layout.activity_main);
    backgroundEl = (RelativeLayout) findViewById(R.id.container);

    toggle = (ToggleButton) findViewById(R.id.toggleButton1);
    toggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            if (toggle.isChecked())     
                backgroundEl.setBackgroundColor(Color.BLACK);
            else
                backgroundEl.setBackgroundColor(Color.WHITE);
        }   
    });
}

以后使用props.put("box_user", "mybox"); 来访问该值。

这是由于JMeter变量的可见范围仅限于当前的线程组,按照Functions and Variables章节:

  

属性与变量不同。变量是线程的本地变量;属性对所有线程都是通用的,需要使用__P或__property函数进行引用。

有关详细信息,请参阅How to Use Variables in Different Thread Groups.指南。

答案 1 :(得分:1)

问题在于时间,而不是beanhell是否正确保存变量。有关处理JMeter元素的顺序,请参阅section 3.9: Execution order。它清楚地表明配置元素在其他所有内容之前处理。这就是在处理JDBC连接配置时未定义${user}(在您的beanshell预处理器中定义)的原因。

我尝试使用CSV数据集来填充数据库配置元素,但似乎在CSV数据集元素之前处理数据库配置,因为这对我来说从未起作用。

我知道有两种方法可以将变量传递给数据库配置。

  1. 更简单的方法是使用User Defined Variables配置元素(位于我所有测试中的数据库配置之前)。
  2. 另一种方法是在启动时将属性传递给JMeter,这可以通过多种方式完成,然后从beanhell内部访问变量${__P(box_user,)}(或props.get("box_user");)。要在启动时传递属性,可以使用-Jbox_user=one在命令行上添加它。或者,您可以使用新行box_user=one将其添加到jmeter安装的bin目录中的user.properties(或jmeter.properties,但不要这样做)。或者,您可以在命令行上使用--addprop pathToFile指定其他属性文件,其中使用与jmeter.properties和user.properties相同的格式读取引用的文件。

答案 2 :(得分:0)

显然它没有被设定。您应该在您的线程组中放置一个调试采样器,并确保它甚至被设置。调试采样器可让您在jmeter中查看所有变量。如果你没有看到你的变量,那么它就没有被设置。

相关问题