JMeter学习-014-JMeter 配置元件实例之 - 用户定义的变量 参数化配置

前文讲述了通过 CSV Data Set Config 实现参数化配置(详情敬请参阅:JMeter学习-010-JMeter 配置元件实例之 - CSV Data Set Config 参数化配置),在 JMeter 的配置元件中还有一个元件:用户定义的变量,通过 用户定义的变量,也可实现参数化的配置,通常用户定义的变量一般作为全局变量使用。下面就以 用户定义的变量 替换之前博文中的配置文件实现单一循环的参数化配置。

 

用户定义的变量添加路径:【 添加 / 配置元件 / 用户定义的变量 】,其对应的配置页面如下所示:

配置参数说明:

名称:对用户定义变量的简述

注释:对用户定义变量的详细概述

名称:定义变量的引用名称

值:变量的值

描述:对定义变量的描述信息

 

参数化脚本修改方法:

第一步:删除原 配置元件/CSV Data Set Config 参数文件

第二步:通过 【 添加 / 配置元件 / 用户定义的变量 】添加用户定义的变量组件

第三步:配置用户定义的变量

将之前 CSV Data Set Config 参数文件中配置的变量配置到添加的组件 用户定义的变量中,配置好的用户定义的变量如下所示:

第四步:执行脚本

脚本执行结果如下图所示:

由上图我们可以看到,实际脚本中配置了两个用户定义的变量组件(分别为不同的商品,且变量中的名称均相同),从执行结果我们可以看出,当变量名称相同时,脚本优先引用最近的可使用的变量。若在日后的脚本编写调试过程中,发现变量的值不是期望的值,除了获取变量获取错误或配置错误之外,还存在一种可能就是就近变量覆盖。

 

附录(一):

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <jmeterTestPlan version="1.2" properties="2.7" jmeter="2.12 r1636949">
  3   <hashTree>
  4     <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="接口自动化测试实例 - 用户定义的变量 参数化 实例" enabled="true">
  5       <stringProp name="TestPlan.comments"></stringProp>
  6       <boolProp name="TestPlan.functional_mode">false</boolProp>
  7       <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
  8       <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
  9         <collectionProp name="Arguments.arguments"/>
 10       </elementProp>
 11       <stringProp name="TestPlan.user_define_classpath"></stringProp>
 12     </TestPlan>
 13     <hashTree>
 14       <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="008-搜索商品" enabled="true">
 15         <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
 16         <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
 17           <boolProp name="LoopController.continue_forever">false</boolProp>
 18           <stringProp name="LoopController.loops">1</stringProp>
 19         </elementProp>
 20         <stringProp name="ThreadGroup.num_threads">2</stringProp>
 21         <stringProp name="ThreadGroup.ramp_time">0</stringProp>
 22         <longProp name="ThreadGroup.start_time">1419232321000</longProp>
 23         <longProp name="ThreadGroup.end_time">1419232321000</longProp>
 24         <boolProp name="ThreadGroup.scheduler">false</boolProp>
 25         <stringProp name="ThreadGroup.duration"></stringProp>
 26         <stringProp name="ThreadGroup.delay"></stringProp>
 27       </ThreadGroup>
 28       <hashTree>
 29         <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量 - 商品编号 101-006-78945" enabled="true">
 30           <collectionProp name="Arguments.arguments">
 31             <elementProp name="goodsNo" elementType="Argument">
 32               <stringProp name="Argument.name">goodsNo</stringProp>
 33               <stringProp name="Argument.value">101-006-78945</stringProp>
 34               <stringProp name="Argument.desc">商品编号</stringProp>
 35               <stringProp name="Argument.metadata">=</stringProp>
 36             </elementProp>
 37             <elementProp name="districtId" elementType="Argument">
 38               <stringProp name="Argument.name">districtId</stringProp>
 39               <stringProp name="Argument.value">29313</stringProp>
 40               <stringProp name="Argument.desc">配送区域</stringProp>
 41               <stringProp name="Argument.metadata">=</stringProp>
 42             </elementProp>
 43             <elementProp name="areacode" elementType="Argument">
 44               <stringProp name="Argument.name">areacode</stringProp>
 45               <stringProp name="Argument.value">1</stringProp>
 46               <stringProp name="Argument.desc">仓库大区</stringProp>
 47               <stringProp name="Argument.metadata">=</stringProp>
 48             </elementProp>
 49             <elementProp name="sysNo" elementType="Argument">
 50               <stringProp name="Argument.name">sysNo</stringProp>
 51               <stringProp name="Argument.value">2169256</stringProp>
 52               <stringProp name="Argument.desc">商品系统编号,用于校验搜索结果</stringProp>
 53               <stringProp name="Argument.metadata">=</stringProp>
 54             </elementProp>
 55             <elementProp name="skuid" elementType="Argument">
 56               <stringProp name="Argument.name">skuid</stringProp>
 57               <stringProp name="Argument.value">489349159815</stringProp>
 58               <stringProp name="Argument.desc">单品SKU,用户校验搜索结果</stringProp>
 59               <stringProp name="Argument.metadata">=</stringProp>
 60             </elementProp>
 61             <elementProp name="appSource" elementType="Argument">
 62               <stringProp name="Argument.name">appSource</stringProp>
 63               <stringProp name="Argument.value">android</stringProp>
 64               <stringProp name="Argument.desc">平台</stringProp>
 65               <stringProp name="Argument.metadata">=</stringProp>
 66             </elementProp>
 67             <elementProp name="appVersion" elementType="Argument">
 68               <stringProp name="Argument.name">appVersion</stringProp>
 69               <stringProp name="Argument.value">45</stringProp>
 70               <stringProp name="Argument.desc">版本</stringProp>
 71               <stringProp name="Argument.metadata">=</stringProp>
 72             </elementProp>
 73           </collectionProp>
 74           <stringProp name="TestPlan.comments">,,,,,,</stringProp>
 75         </Arguments>
 76         <hashTree/>
 77         <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量 - 商品编号 101-006-78945" enabled="true">
 78           <collectionProp name="Arguments.arguments">
 79             <elementProp name="goodsNo" elementType="Argument">
 80               <stringProp name="Argument.name">goodsNo</stringProp>
 81               <stringProp name="Argument.value">101-005-31058</stringProp>
 82               <stringProp name="Argument.desc">商品编号</stringProp>
 83               <stringProp name="Argument.metadata">=</stringProp>
 84             </elementProp>
 85             <elementProp name="districtId" elementType="Argument">
 86               <stringProp name="Argument.name">districtId</stringProp>
 87               <stringProp name="Argument.value">29313</stringProp>
 88               <stringProp name="Argument.desc">配送区域</stringProp>
 89               <stringProp name="Argument.metadata">=</stringProp>
 90             </elementProp>
 91             <elementProp name="areacode" elementType="Argument">
 92               <stringProp name="Argument.name">areacode</stringProp>
 93               <stringProp name="Argument.value">1</stringProp>
 94               <stringProp name="Argument.desc">仓库大区</stringProp>
 95               <stringProp name="Argument.metadata">=</stringProp>
 96             </elementProp>
 97             <elementProp name="sysNo" elementType="Argument">
 98               <stringProp name="Argument.name">sysNo</stringProp>
 99               <stringProp name="Argument.value">1903913</stringProp>
100               <stringProp name="Argument.desc">商品系统编号,用于校验搜索结果</stringProp>
101               <stringProp name="Argument.metadata">=</stringProp>
102             </elementProp>
103             <elementProp name="skuid" elementType="Argument">
104               <stringProp name="Argument.name">skuid</stringProp>
105               <stringProp name="Argument.value">472108827742</stringProp>
106               <stringProp name="Argument.desc">单品SKU,用户校验搜索结果</stringProp>
107               <stringProp name="Argument.metadata">=</stringProp>
108             </elementProp>
109             <elementProp name="appSource" elementType="Argument">
110               <stringProp name="Argument.name">appSource</stringProp>
111               <stringProp name="Argument.value">android</stringProp>
112               <stringProp name="Argument.desc">平台</stringProp>
113               <stringProp name="Argument.metadata">=</stringProp>
114             </elementProp>
115             <elementProp name="appVersion" elementType="Argument">
116               <stringProp name="Argument.name">appVersion</stringProp>
117               <stringProp name="Argument.value">45</stringProp>
118               <stringProp name="Argument.desc">版本</stringProp>
119               <stringProp name="Argument.metadata">=</stringProp>
120             </elementProp>
121           </collectionProp>
122           <stringProp name="TestPlan.comments">,,,,,,</stringProp>
123         </Arguments>
124         <hashTree/>
125         <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="008-01-搜索商品 【${goodsNo} | ${districtId}】" enabled="true">
126           <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
127             <collectionProp name="Arguments.arguments">
128               <elementProp name="q" elementType="HTTPArgument">
129                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
130                 <stringProp name="Argument.value">${goodsNo}</stringProp>
131                 <stringProp name="Argument.metadata">=</stringProp>
132                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
133                 <stringProp name="Argument.name">q</stringProp>
134               </elementProp>
135               <elementProp name="districtId" elementType="HTTPArgument">
136                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
137                 <stringProp name="Argument.value">${districtId}</stringProp>
138                 <stringProp name="Argument.metadata">=</stringProp>
139                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
140                 <stringProp name="Argument.name">districtId</stringProp>
141               </elementProp>
142               <elementProp name="areacode" elementType="HTTPArgument">
143                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
144                 <stringProp name="Argument.value">${areacode}</stringProp>
145                 <stringProp name="Argument.metadata">=</stringProp>
146                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
147                 <stringProp name="Argument.name">areacode</stringProp>
148               </elementProp>
149               <elementProp name="dtype" elementType="HTTPArgument">
150                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
151                 <stringProp name="Argument.value">list|page|classes</stringProp>
152                 <stringProp name="Argument.metadata">=</stringProp>
153                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
154                 <stringProp name="Argument.name">dtype</stringProp>
155               </elementProp>
156               <elementProp name="districtId" elementType="HTTPArgument">
157                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
158                 <stringProp name="Argument.name">districtId</stringProp>
159                 <stringProp name="Argument.value">${districtId}</stringProp>
160                 <stringProp name="Argument.metadata">=</stringProp>
161                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
162               </elementProp>
163               <elementProp name="exAppTag" elementType="HTTPArgument">
164                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
165                 <stringProp name="Argument.name">exAppTag</stringProp>
166                 <stringProp name="Argument.value">2045191607</stringProp>
167                 <stringProp name="Argument.metadata">=</stringProp>
168                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
169               </elementProp>
170               <elementProp name="appSource" elementType="HTTPArgument">
171                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
172                 <stringProp name="Argument.value">${appSource}</stringProp>
173                 <stringProp name="Argument.metadata">=</stringProp>
174                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
175                 <stringProp name="Argument.name">appSource</stringProp>
176               </elementProp>
177               <elementProp name="appVersion" elementType="HTTPArgument">
178                 <boolProp name="HTTPArgument.always_encode">false</boolProp>
179                 <stringProp name="Argument.value">${appVersion}</stringProp>
180                 <stringProp name="Argument.metadata">=</stringProp>
181                 <boolProp name="HTTPArgument.use_equals">true</boolProp>
182                 <stringProp name="Argument.name">appVersion</stringProp>
183               </elementProp>
184             </collectionProp>
185           </elementProp>
186           <stringProp name="HTTPSampler.domain">mb.51buy.com</stringProp>
187           <stringProp name="HTTPSampler.port"></stringProp>
188           <stringProp name="HTTPSampler.connect_timeout"></stringProp>
189           <stringProp name="HTTPSampler.response_timeout"></stringProp>
190           <stringProp name="HTTPSampler.protocol">http</stringProp>
191           <stringProp name="HTTPSampler.contentEncoding">GB2312</stringProp>
192           <stringProp name="HTTPSampler.path">/json.php?mod=Search&amp;act=page&amp;p=1&amp;pp=20</stringProp>
193           <stringProp name="HTTPSampler.method">POST</stringProp>
194           <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
195           <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
196           <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
197           <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
198           <boolProp name="HTTPSampler.monitor">false</boolProp>
199           <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
200         </HTTPSamplerProxy>
201         <hashTree>
202           <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息头管理器" enabled="true">
203             <collectionProp name="HeaderManager.headers">
204               <elementProp name="Charset" elementType="Header">
205                 <stringProp name="Header.name">Charset</stringProp>
206                 <stringProp name="Header.value">UTF-8</stringProp>
207               </elementProp>
208               <elementProp name="Content-Type" elementType="Header">
209                 <stringProp name="Header.name">Content-Type</stringProp>
210                 <stringProp name="Header.value">application/x-www-form-urlencoded</stringProp>
211               </elementProp>
212               <elementProp name="Accept-Encoding" elementType="Header">
213                 <stringProp name="Header.name">Accept-Encoding</stringProp>
214                 <stringProp name="Header.value">gzip</stringProp>
215               </elementProp>
216               <elementProp name="User-Agent" elementType="Header">
217                 <stringProp name="Header.name">User-Agent</stringProp>
218                 <stringProp name="Header.value">Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9502 Build/KOT49H)</stringProp>
219               </elementProp>
220             </collectionProp>
221           </HeaderManager>
222           <hashTree/>
223           <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="响应断言" enabled="true">
224             <collectionProp name="Asserion.test_strings">
225               <stringProp name="-781931418">${sysNo}</stringProp>
226               <stringProp name="-794775524">${skuid}</stringProp>
227             </collectionProp>
228             <stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
229             <boolProp name="Assertion.assume_success">false</boolProp>
230             <intProp name="Assertion.test_type">16</intProp>
231           </ResponseAssertion>
232           <hashTree/>
233         </hashTree>
234       </hashTree>
235       <ResultCollector guiclass="AssertionVisualizer" testclass="ResultCollector" testname="断言结果" enabled="true">
236         <boolProp name="ResultCollector.error_logging">false</boolProp>
237         <objProp>
238           <name>saveConfig</name>
239           <value class="SampleSaveConfiguration">
240             <time>true</time>
241             <latency>true</latency>
242             <timestamp>true</timestamp>
243             <success>true</success>
244             <label>true</label>
245             <code>true</code>
246             <message>true</message>
247             <threadName>true</threadName>
248             <dataType>true</dataType>
249             <encoding>false</encoding>
250             <assertions>true</assertions>
251             <subresults>true</subresults>
252             <responseData>false</responseData>
253             <samplerData>false</samplerData>
254             <xml>false</xml>
255             <fieldNames>false</fieldNames>
256             <responseHeaders>false</responseHeaders>
257             <requestHeaders>false</requestHeaders>
258             <responseDataOnError>false</responseDataOnError>
259             <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
260             <assertionsResultsToSave>0</assertionsResultsToSave>
261             <bytes>true</bytes>
262             <threadCounts>true</threadCounts>
263           </value>
264         </objProp>
265         <stringProp name="filename"></stringProp>
266       </ResultCollector>
267       <hashTree/>
268       <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">
269         <boolProp name="ResultCollector.error_logging">false</boolProp>
270         <objProp>
271           <name>saveConfig</name>
272           <value class="SampleSaveConfiguration">
273             <time>true</time>
274             <latency>true</latency>
275             <timestamp>true</timestamp>
276             <success>true</success>
277             <label>true</label>
278             <code>true</code>
279             <message>true</message>
280             <threadName>true</threadName>
281             <dataType>true</dataType>
282             <encoding>false</encoding>
283             <assertions>true</assertions>
284             <subresults>true</subresults>
285             <responseData>false</responseData>
286             <samplerData>false</samplerData>
287             <xml>false</xml>
288             <fieldNames>false</fieldNames>
289             <responseHeaders>false</responseHeaders>
290             <requestHeaders>false</requestHeaders>
291             <responseDataOnError>false</responseDataOnError>
292             <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
293             <assertionsResultsToSave>0</assertionsResultsToSave>
294             <bytes>true</bytes>
295             <threadCounts>true</threadCounts>
296           </value>
297         </objProp>
298         <stringProp name="filename">${__property(JmeterAuto_LogFile)}</stringProp>
299       </ResultCollector>
300       <hashTree/>
301     </hashTree>
302   </hashTree>
303 </jmeterTestPlan>
JMeter学习-014-JMeter 配置元件之 - 用户定义的变量 参数化配置

 

至此, JMeter学习-014-JMeter 配置元件实例之 - 用户定义的变量 参数化配置 顺利完结,希望此文能够给初学 JMeter 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

posted @ 2015-08-27 15:25  范丰平  Views(3364)  Comments(0Edit  收藏  举报