簡體   English   中英

JMeter在beanshell中設置用戶定義的變量不起作用

[英]JMeter setting user defined variable in beanshell not working

我想在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>

錯誤是:致命:角色“$ {user}”不存在 - >所以我的變量不能替換,我不知道為什么

您是否有機會在不同的線程組中引用變量? 如果是,則需要使用JMeter Properties,如:

props.put("box_user", "mybox");

然后使用${__P(box_user,)}來訪問該值。

這是由於JMeter變量的可見性范圍僅限於當前的Thread Group,根據Functions和Variables章節:

屬性與變量不同。 變量是線程的本地變量; 屬性對所有線程都是通用的,需要使用__P或__property函數進行引用。

請參見如何在不同的線程組中使用變量。 指南了解更多細節。

問題是時間,而不是beanhell是否正確保存變量。 請參閱第3.9節:處理JMeter元素的順序的執行順序 它清楚地表明配置元素在其他所有內容之前處理。 這就是在處理JDBC連接配置時未定義${user} (在您的beanshell預處理器中定義)的原因。

我嘗試使用CSV數據集來填充數據庫配置元素,但似乎在CSV數據集元素之前處理數據庫配置,因為這對我來說從未起作用。

我知道有兩種方法可以將變量傳遞給數據庫配置。

  1. 更簡單的方法是使用User Defined Variables配置元素(位於我所有測試中的數據庫配置之前)。
  2. 另一種方法是在啟動時將屬性傳遞給JMeter,這可以通過多種方式完成,然后以${__P(box_user,)} (或作為props.get("box_user");從beanshell內部)訪問變量。 要在啟動時傳遞屬性,可以使用-Jbox_user=one在命令行上添加它。 或者,您可以使用新行box_user=one將其添加到jmeter安裝的bin目錄中的user.properties(或jmeter.properties,但不要這樣做)。 或者,您可以使用--addprop pathToFile在命令行上指定其他屬性文件,其中使用與jmeter.properties和user.properties相同的格式讀取引用的文件。

顯然它沒有被設定。 您應該在您的線程組中放置一個調試采樣器,並確保它甚至被設置。 調試采樣器可讓您在jmeter中查看所有變量。 如果您沒有看到變量,則表示未設置變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM