JMeter学习-011-JMeter 后置处理器实例之 - 正则表达式提取器(三)多参数获取进阶引用篇

前端之家收集整理的这篇文章主要介绍了JMeter学习-011-JMeter 后置处理器实例之 - 正则表达式提取器(三)多参数获取进阶引用篇前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JMeter学习-011-JMeter 后置处理器实例之 - 正则表达式提取器(三)多参数获取进阶引用篇


前两篇文章分表讲述了 后置处理器 - 正则表达式提取器概述及简单实例、多参数获取,相应博文敬请参阅 简单实例、多参数获取

此文主要讲述如何引用正则表达式提取获取的数据信息。其实,正则表达式提取获取的数据,均可看做一个变量(单个数据,此处指所需获取的测试相关数据)或数组(多个数据),通过引用变量或者数组的数据,达到应用其数据的目的。下面针对此两种方式进行介绍。

第一种:单数据变量

但数据变量的应用比较简单,同我们日常 shell 脚本引用变量相同,引用样式:引用名称。例如要引用下图中的变量,引用方式为: 引用名称。例如要引用下图中的变量,引用方式为:{price}。




第二种:多数据变量(数组)

多数据变量(数组)的提取器配置如下图所示:


通过调试工具 DPP,我们可以获取上述 goodInfo 的相关信息如下所示:

goodInfo=2142670,101-006-72936,628800

goodInfo_g=5

goodInfo_g0=sysNo":"2142670","skuid":"487623639432","productID":"101-006-72936","productTitle":"苹果(Apple)iPhone 6 Plus (A1593) 64GB 银色 移动4G手机","price":"628800","

goodInfo_g1=2142670

goodInfo_g2=","

goodInfo_g3=101-006-72936

goodInfo_g4=productTitle":"苹果(Apple)iPhone 6 Plus (A1593) 64GB 银色 移动4G手机

goodInfo_g5=628800


通过上述信息,结合正则表达式提取器的配置信息,我们可以很容易的看出,配置中模板 1 1 中的数字,对应上述信息中的不同数据。例如:若模板为 0 0,则最终的数据为上述第三行等号后的数据;若模板为 3 3,则最终数据为 101-006-72936。上述提取器的模板为: 1 1,3 3,5 5,最终的数据为 2142670,628800。那么若是想要获取 101-006-72936,改怎么引用呢?相信聪明的你已经在上述信息中发现了,对了,实际应用的表达式为 : ${goodInfo_g3}。

下面就以获取商品编号,并通过商品编号搜索商品,同时通过 商品系统编号、商品价格 验证搜索结果是否正确(商品搜索的 HTTP请求 获取请小主自行获取,谢谢!),依据下图所示,将搜索请求中的关键字参数化为上步 HTTP请求中的 商品编号。


最终脚本执行结果如下所示(断言请小主自行仿照配置):




附录(一):

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="001-类目搜索验证" 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">1</stringProp>
21 <stringProp name="ThreadGroup.ramp_time">1</stringProp>
22 <longProp name="ThreadGroup.start_time">1419564228000</longProp>
23 <longProp name="ThreadGroup.end_time">1419564228000</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 <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="手机数码-手机通讯-苹果" enabled="true">
30 <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
31 <collectionProp name="Arguments.arguments">
32 <elementProp name="districtId" elementType="HTTPArgument">
33 <boolProp name="HTTPArgument.always_encode">false</boolProp>
34 <stringProp name="Argument.name">districtId</stringProp>
35 <stringProp name="Argument.value">29357</stringProp>
36 <stringProp name="Argument.Metadata">=</stringProp>
37 <boolProp name="HTTPArgument.use_equals">true</boolProp>
38 </elementProp>
39 <elementProp name="exAppTag" elementType="HTTPArgument">
40 <boolProp name="HTTPArgument.always_encode">false</boolProp>
41 <stringProp name="Argument.name">exAppTag</stringProp>
42 <stringProp name="Argument.value">2045191607</stringProp>
43 <stringProp name="Argument.Metadata">=</stringProp>
44 <boolProp name="HTTPArgument.use_equals">true</boolProp>
45 </elementProp>
46 </collectionProp>
47 </elementProp>
48 <stringProp name="HTTPSampler.domain">mb.51buy.com</stringProp>
49 <stringProp name="HTTPSampler.port"></stringProp>
50 <stringProp name="HTTPSampler.connect_timeout"></stringProp>
51 <stringProp name="HTTPSampler.response_timeout"></stringProp>
52 <stringProp name="HTTPSampler.protocol">http</stringProp>
53 <stringProp name="HTTPSampler.contentEncoding">GB2312</stringProp>
54 <stringProp name="HTTPSampler.path">/json.PHP?mod=Search&amp;act=page&amp;p=1&amp;path=706188t706189&amp;districtId=29357&amp;areacode=1&amp;dtype=list%7Cpage%7Cclasses&amp;appSource=android&amp;appVersion=45</stringProp>
55 <stringProp name="HTTPSampler.method">POST</stringProp>
56 <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
57 <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
58 <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
59 <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
60 <boolProp name="HTTPSampler.monitor">false</boolProp>
61 <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
62 </HTTPSamplerProxy>
63 <hashTree>
64 <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息头管理器" enabled="true">
65 <collectionProp name="HeaderManager.headers">
66 <elementProp name="Charset" elementType="Header">
67 <stringProp name="Header.name">Charset</stringProp>
68 <stringProp name="Header.value">UTF-8</stringProp>
69 </elementProp>
70 <elementProp name="Content-Type" elementType="Header">
71 <stringProp name="Header.name">Content-Type</stringProp>
72 <stringProp name="Header.value">application/x-www-form-urlencoded</stringProp>
73 </elementProp>
74 <elementProp name="Accept-Encoding" elementType="Header">
75 <stringProp name="Header.name">Accept-Encoding</stringProp>
76 <stringProp name="Header.value">gzip</stringProp>
77 </elementProp>
78 <elementProp name="User-Agent" elementType="Header">
79 <stringProp name="Header.name">User-Agent</stringProp>
80 <stringProp name="Header.value">Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9502 Build/KOT49H)</stringProp>
81 </elementProp>
82 </collectionProp>
83 </HeaderManager>
84 <hashTree/>
85 <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="01 - 判断类目搜索结果状态码" enabled="true">
86 <collectionProp name="Asserion.test_strings">
87 <stringProp name="71131476">&quot;errno&quot;:0</stringProp>
88 </collectionProp>
89 <stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
90 <boolProp name="Assertion.assume_success">false</boolProp>
91 <intProp name="Assertion.test_type">2</intProp>
92 </ResponseAssertion>
93 <hashTree/>
94 <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="正则表达式提取器 - 获取商品列表中商品的系统编号" enabled="true">
95 <stringProp name="RegexExtractor.useHeaders">false</stringProp>
96 <stringProp name="RegexExtractor.refname">sysNo</stringProp>
97 <stringProp name="RegexExtractor.regex">&quot;sysNo&quot;:&quot;(.+?)&quot;,&quot;</stringProp>
98 <stringProp name="RegexExtractor.template">$1$</stringProp>
99 <stringProp name="RegexExtractor.default">ERROR</stringProp>
100 <stringProp name="RegexExtractor.match_number">2</stringProp>
101 </RegexExtractor>
102 <hashTree/>
103 <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="正则表达式提取器 - 获取商品列表中商品的销售价格" enabled="true">
104 <stringProp name="RegexExtractor.useHeaders">false</stringProp>
105 <stringProp name="RegexExtractor.refname">price</stringProp>
106 <stringProp name="RegexExtractor.regex">&quot;price&quot;:&quot;(.+?)&quot;,&quot;activePrice</stringProp>
107 <stringProp name="RegexExtractor.template">$1$</stringProp>
108 <stringProp name="RegexExtractor.default">ERROR</stringProp>
109 <stringProp name="RegexExtractor.match_number">3</stringProp>
110 </RegexExtractor>
111 <hashTree/>
112 <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="正则表达式提取器 - 多匹配项处理之一 - 获取单个匹配项" enabled="true">
113 <stringProp name="RegexExtractor.useHeaders">false</stringProp>
114 <stringProp name="RegexExtractor.refname">single_sysNo</stringProp>
115 <stringProp name="RegexExtractor.regex">sysNo&quot;:&quot;([0-9]{2,8}).+?price&quot;:&quot;(.+?)&quot;,&quot;</stringProp>
116 <stringProp name="RegexExtractor.template">$1$</stringProp>
117 <stringProp name="RegexExtractor.default">ERROR</stringProp>
118 <stringProp name="RegexExtractor.match_number">2</stringProp>
119 </RegexExtractor>
120 <hashTree/>
121 <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="正则表达式提取器 - 多匹配项处理之二 - 获取单个匹配项" enabled="true">
122 <stringProp name="RegexExtractor.useHeaders">false</stringProp>
123 <stringProp name="RegexExtractor.refname">single_price</stringProp>
124 <stringProp name="RegexExtractor.regex">sysNo&quot;:&quot;([0-9]{2,&quot;</stringProp>
125 <stringProp name="RegexExtractor.template">$2$</stringProp>
126 <stringProp name="RegexExtractor.default">ERROR</stringProp>
127 <stringProp name="RegexExtractor.match_number">2</stringProp>
128 </RegexExtractor>
129 <hashTree/>
130 <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="正则表达式提取器 - 多匹配项处理之三 - 获取多个匹配项" enabled="true">
131 <stringProp name="RegexExtractor.useHeaders">false</stringProp>
132 <stringProp name="RegexExtractor.refname">goodInfo</stringProp>
133 <stringProp name="RegexExtractor.regex">sysNo&quot;:&quot;([0-9]{2,8})(.+?)productID&quot;:&quot;(.+?)&quot;,&quot;(.+?)&quot;,&quot;price&quot;:&quot;(.+?)&quot;,&quot;</stringProp>
134 <stringProp name="RegexExtractor.template">$1$,$3$,$5$</stringProp>
135 <stringProp name="RegexExtractor.default">ERROR,ERROR,ERROR</stringProp>
136 <stringProp name="RegexExtractor.match_number">3,3,3</stringProp>
137 </RegexExtractor>
138 <hashTree/>
139 <DebugPostProcessor guiclass="TestBeanGUI" testclass="DebugPostProcessor" testname="DPP -- 所属 :HTTP请求" enabled="true">
140 <boolProp name="displayJMeterProperties">false</boolProp>
141 <boolProp name="displayJMeterVariables">true</boolProp>
142 <boolProp name="displaySamplerProperties">true</boolProp>
143 <boolProp name="displaySystemProperties">false</boolProp>
144 </DebugPostProcessor>
145 <hashTree/>
146 </hashTree>
147 <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="搜索商品 - 【${goodInfo}】" enabled="true">
148 <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
149 <collectionProp name="Arguments.arguments">
150 <elementProp name="q" elementType="HTTPArgument">
151 <boolProp name="HTTPArgument.always_encode">false</boolProp>
152 <stringProp name="Argument.value">${goodInfo_g3}</stringProp>
153 <stringProp name="Argument.Metadata">=</stringProp>
154 <boolProp name="HTTPArgument.use_equals">true</boolProp>
155 <stringProp name="Argument.name">q</stringProp>
156 </elementProp>
157 <elementProp name="districtId" elementType="HTTPArgument">
158 <boolProp name="HTTPArgument.always_encode">false</boolProp>
159 <stringProp name="Argument.value">29357</stringProp>
160 <stringProp name="Argument.Metadata">=</stringProp>
161 <boolProp name="HTTPArgument.use_equals">true</boolProp>
162 <stringProp name="Argument.name">districtId</stringProp>
163 </elementProp>
164 <elementProp name="areacode" elementType="HTTPArgument">
165 <boolProp name="HTTPArgument.always_encode">false</boolProp>
166 <stringProp name="Argument.value">1</stringProp>
167 <stringProp name="Argument.Metadata">=</stringProp>
168 <boolProp name="HTTPArgument.use_equals">true</boolProp>
169 <stringProp name="Argument.name">areacode</stringProp>
170 </elementProp>
171 <elementProp name="dtype" elementType="HTTPArgument">
172 <boolProp name="HTTPArgument.always_encode">false</boolProp>
173 <stringProp name="Argument.value">list|page|classes</stringProp>
174 <stringProp name="Argument.Metadata">=</stringProp>
175 <boolProp name="HTTPArgument.use_equals">true</boolProp>
176 <stringProp name="Argument.name">dtype</stringProp>
177 </elementProp>
178 <elementProp name="districtId" elementType="HTTPArgument">
179 <boolProp name="HTTPArgument.always_encode">false</boolProp>
180 <stringProp name="Argument.name">districtId</stringProp>
181 <stringProp name="Argument.value">29357</stringProp>
182 <stringProp name="Argument.Metadata">=</stringProp>
183 <boolProp name="HTTPArgument.use_equals">true</boolProp>
184 </elementProp>
185 <elementProp name="exAppTag" elementType="HTTPArgument">
186 <boolProp name="HTTPArgument.always_encode">false</boolProp>
187 <stringProp name="Argument.name">exAppTag</stringProp>
188 <stringProp name="Argument.value">2045191607</stringProp>
189 <stringProp name="Argument.Metadata">=</stringProp>
190 <boolProp name="HTTPArgument.use_equals">true</boolProp>
191 </elementProp>
192 <elementProp name="appSource" elementType="HTTPArgument">
193 <boolProp name="HTTPArgument.always_encode">false</boolProp>
194 <stringProp name="Argument.value">android</stringProp>
195 <stringProp name="Argument.Metadata">=</stringProp>
196 <boolProp name="HTTPArgument.use_equals">true</boolProp>
197 <stringProp name="Argument.name">appSource</stringProp>
198 </elementProp>
199 <elementProp name="appVersion" elementType="HTTPArgument">
200 <boolProp name="HTTPArgument.always_encode">false</boolProp>
201 <stringProp name="Argument.value">45</stringProp>
202 <stringProp name="Argument.Metadata">=</stringProp>
203 <boolProp name="HTTPArgument.use_equals">true</boolProp>
204 <stringProp name="Argument.name">appVersion</stringProp>
205 </elementProp>
206 </collectionProp>
207 </elementProp>
208 <stringProp name="HTTPSampler.domain">mb.51buy.com</stringProp>
209 <stringProp name="HTTPSampler.port"></stringProp>
210 <stringProp name="HTTPSampler.connect_timeout"></stringProp>
211 <stringProp name="HTTPSampler.response_timeout"></stringProp>
212 <stringProp name="HTTPSampler.protocol">http</stringProp>
213 <stringProp name="HTTPSampler.contentEncoding">GB2312</stringProp>
214 <stringProp name="HTTPSampler.path">/json.PHP?mod=Search&amp;act=page&amp;p=1&amp;pp=20</stringProp>
215 <stringProp name="HTTPSampler.method">POST</stringProp>
216 <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
217 <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
218 <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
219 <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
220 <boolProp name="HTTPSampler.monitor">false</boolProp>
221 <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
222 </HTTPSamplerProxy>
223 <hashTree>
224 <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息头管理器" enabled="true">
225 <collectionProp name="HeaderManager.headers">
226 <elementProp name="Charset" elementType="Header">
227 <stringProp name="Header.name">Charset</stringProp>
228 <stringProp name="Header.value">UTF-8</stringProp>
229 </elementProp>
230 <elementProp name="Content-Type" elementType="Header">
231 <stringProp name="Header.name">Content-Type</stringProp>
232 <stringProp name="Header.value">application/x-www-form-urlencoded</stringProp>
233 </elementProp>
234 <elementProp name="Accept-Encoding" elementType="Header">
235 <stringProp name="Header.name">Accept-Encoding</stringProp>
236 <stringProp name="Header.value">gzip</stringProp>
237 </elementProp>
238 <elementProp name="User-Agent" elementType="Header">
239 <stringProp name="Header.name">User-Agent</stringProp>
240 <stringProp name="Header.value">Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9502 Build/KOT49H)</stringProp>
241 </elementProp>
242 </collectionProp>
243 </HeaderManager>
244 <hashTree/>
245 <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="响应断言" enabled="true">
246 <collectionProp name="Asserion.test_strings">
247 <stringProp name="-388667338">${goodInfo_g1}</stringProp>
248 <stringProp name="-388667214">${goodInfo_g5}</stringProp>
249 </collectionProp>
250 <stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
251 <boolProp name="Assertion.assume_success">false</boolProp>
252 <intProp name="Assertion.test_type">16</intProp>
253 </ResponseAssertion>
254 <hashTree/>
255 <DebugPostProcessor guiclass="TestBeanGUI" testclass="DebugPostProcessor" testname="DPP -- 所属 :HTTP请求" enabled="true">
256 <boolProp name="displayJMeterProperties">false</boolProp>
257 <boolProp name="displayJMeterVariables">true</boolProp>
258 <boolProp name="displaySamplerProperties">true</boolProp>
259 <boolProp name="displaySystemProperties">false</boolProp>
260 </DebugPostProcessor>
261 <hashTree/>
262 </hashTree>
263 </hashTree>
264 <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">
265 <boolProp name="ResultCollector.error_logging">false</boolProp>
266 <objProp>
267 <name>saveConfig</name>
268 <value class="SampleSaveConfiguration">
269 <time>true</time>
270 <latency>true</latency>
271 <timestamp>true</timestamp>
272 <success>true</success>
273 <label>true</label>
274 <code>true</code>
275 <message>true</message>
276 <threadName>true</threadName>
277 <dataType>true</dataType>
278 <encoding>false</encoding>
279 <assertions>true</assertions>
280 <subresults>true</subresults>
281 <responseData>false</responseData>
282 <samplerData>false</samplerData>
283 <xml>false</xml>
284 <fieldNames>false</fieldNames>
285 <responseHeaders>false</responseHeaders>
286 <requestHeaders>false</requestHeaders>
287 <responseDataOnError>false</responseDataOnError>
288 <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
289 <assertionsResultsToSave>0</assertionsResultsToSave>
290 <bytes>true</bytes>
291 <threadCounts>true</threadCounts>
292 </value>
293 </objProp>
294 <stringProp name="filename"></stringProp>
295 </ResultCollector>
296 <hashTree/>
297 </hashTree>
298 </hashTree>
299 </jmeterTestPlan>




至此, JMeter学习-011-JMeter 后置处理器实例之 - 正则表达式提取器(三)多参数获取进阶引用篇 顺利完结,希望此文能够给初学 JMeter 的您一份参考。

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

我本渺小,但山峰,我一次次绝顶!

PS:若有错误,敬请告知,不胜感激! Copyright @范丰平 版权所有,如需转载请标明本文原始链接出处,严禁商业用途! 我的个人博客链接地址:http://www.cnblogs.com/fengpingfan

猜你在找的正则表达式相关文章