标签:dai vgg monitor dmp ide reg 正则表达式 gty halt
上文我们讲述了如何对 HTTP请求 的响应数据进行断言,以判断响应是否符合我们的预期,敬请参阅:JMeter学习-007-JMeter 断言实例之一 - 响应断言
那么我们如何获取 HTTP请求 响应结果中的数据呢?此文以获取类目 手机数码-手机通讯-苹果 结果列表中的第一个商品的系统编号为例演示(脚本基于上篇文章,请知悉)。
如下为请求响应数据中的部分数据,我们最终要获取的数据为 "sysNo": "2142717" 中的 2142717。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
{ "sysNo": "2142717", "skuid": "487626525247", "productID": "101-006-72958", "productTitle": "苹果(Apple)iPhone 6 Plus (A1524) 16GB 金色 移动联通电信4G手机", "price": "556600", "activePrice": "", "marketprice": "608800", "promotionDesc": "A1524、A1586为三网通版,支持移动\/联通\/电信4G\/3G\/2G。找水货?找翻新?没有!易迅网为国行正品代言!!", "manufacturerCode": "49", "manufacturerName": "苹果(Apple)", "evaluationNum": "1717", "gradeNum": "48", "onlineQty": "796", "shippingday": "0t0", "gift": 0, "promotionType": "0", "attribute": "2000000000e214处理器;< strong >(为保持样式美观,此处有删减文字,请知悉!)</ strong >;43257eFDD-LTE\/TD-LTE\-LTE)\/电信4G(FDD-LTE);移动3G(TD-SCDMA)\353e2;49e9?金色;42914?内存e1?16G;43246?制式e4?5.5寸三网通版", "saleNum": "454", "productTypeMasterid": "0", "areaCode": "1", "classid": "203320", "tag": "超薄手机 大屏手机 待机时间长 高清屏 女神手机 拍照功能强 商务手机 外观漂亮 指纹识别 主流潮机", "goodsUrl": "http:\/\/item.yixun.com\/item-2142717.html", "picUrl": "http:\/\/img3.wgimg.com\/qqbuy\/2295220799\/item-00000000000000000000007188CE4A3F.0.jpg\/200?55DAE367", "reachable": "0" }, |
那么我们如何获取呢? JMeter 提供的后置处理器中提供了响应的获取方法,例如:正则表达式提取器、BeanShell PostProcessor、BSF PostProcessor 等。当下以 正则表达式提取器为例演示讲解,对应的添加路径为:【添加/后置处理器/正则表达式提取器】,添加后目录结果如下:
对应添加的正则表达式提取器如下所示:
正则表达式提取器说明:
对应的脚本源码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
<? 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="接口自动化测试用例 - 非登录态" 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="用户定义的变量" enabled="true"> < collectionProp name="Arguments.arguments"/> </ elementProp > < stringProp name="TestPlan.user_define_classpath"></ stringProp > </ TestPlan > < hashTree > < ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="001-类目搜索验证" enabled="true"> < stringProp name="ThreadGroup.on_sample_error">continue</ stringProp > < elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" 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">1419564228000</ longProp > < longProp name="ThreadGroup.end_time">1419564228000</ longProp > < boolProp name="ThreadGroup.scheduler">false</ boolProp > < stringProp name="ThreadGroup.duration"></ stringProp > < stringProp name="ThreadGroup.delay"></ stringProp > </ ThreadGroup > < hashTree > < HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="手机数码-手机通讯-苹果" enabled="true"> < elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true"> < collectionProp name="Arguments.arguments"> < elementProp name="districtId" elementType="HTTPArgument"> < boolProp name="HTTPArgument.always_encode">false</ boolProp > < stringProp name="Argument.name">districtId</ stringProp > < stringProp name="Argument.value">29357</ stringProp > < stringProp name="Argument.metadata">=</ stringProp > < boolProp name="HTTPArgument.use_equals">true</ boolProp > </ elementProp > < elementProp name="exAppTag" elementType="HTTPArgument"> < boolProp name="HTTPArgument.always_encode">false</ boolProp > < stringProp name="Argument.name">exAppTag</ stringProp > < stringProp name="Argument.value">2045191607</ stringProp > < stringProp name="Argument.metadata">=</ stringProp > < boolProp name="HTTPArgument.use_equals">true</ boolProp > </ elementProp > </ collectionProp > </ elementProp > < stringProp name="HTTPSampler.domain">mb.51buy.com</ stringProp > < stringProp name="HTTPSampler.port"></ stringProp > < stringProp name="HTTPSampler.connect_timeout"></ stringProp > < stringProp name="HTTPSampler.response_timeout"></ stringProp > < stringProp name="HTTPSampler.protocol">http</ stringProp > < stringProp name="HTTPSampler.contentEncoding">GB2312</ stringProp > < stringProp name="HTTPSampler.path">/json.php?mod=Search&act=page&p=1&path=706188t706189&districtId=29357&areacode=1&dtype=list%7Cpage%7Cclasses&appSource=android&appVersion=45</ stringProp > < stringProp name="HTTPSampler.method">POST</ stringProp > < boolProp name="HTTPSampler.follow_redirects">true</ boolProp > < boolProp name="HTTPSampler.auto_redirects">false</ boolProp > < boolProp name="HTTPSampler.use_keepalive">true</ boolProp > < boolProp name="HTTPSampler.DO_MULTIPART_POST">false</ boolProp > < boolProp name="HTTPSampler.monitor">false</ boolProp > < stringProp name="HTTPSampler.embedded_url_re"></ stringProp > </ HTTPSamplerProxy > < hashTree > < HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息头管理器" enabled="true"> < collectionProp name="HeaderManager.headers"> < elementProp name="Charset" elementType="Header"> < stringProp name="Header.name">Charset</ stringProp > < stringProp name="Header.value">UTF-8</ stringProp > </ elementProp > < elementProp name="Content-Type" elementType="Header"> < stringProp name="Header.name">Content-Type</ stringProp > < stringProp name="Header.value">application/x-www-form-urlencoded</ stringProp > </ elementProp > < elementProp name="Accept-Encoding" elementType="Header"> < stringProp name="Header.name">Accept-Encoding</ stringProp > < stringProp name="Header.value">gzip</ stringProp > </ elementProp > < elementProp name="User-Agent" elementType="Header"> < stringProp name="Header.name">User-Agent</ stringProp > < stringProp name="Header.value">Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9502 Build/KOT49H)</ stringProp > </ elementProp > </ collectionProp > </ HeaderManager > < hashTree /> < ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="01 - 判断类目搜索结果状态码" enabled="true"> < collectionProp name="Asserion.test_strings"> < stringProp name="71131476">"errno":0</ stringProp > </ collectionProp > < stringProp name="Assertion.test_field">Assertion.response_data</ stringProp > < boolProp name="Assertion.assume_success">false</ boolProp > < intProp name="Assertion.test_type">2</ intProp > </ ResponseAssertion > < hashTree /> < RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="正则表达式提取器 -- 获取类目搜索结果商品列表中商品的系统编号" enabled="true"> < stringProp name="RegexExtractor.useHeaders">false</ stringProp > < stringProp name="RegexExtractor.refname">goodsid</ stringProp > < stringProp name="RegexExtractor.regex">"sysNo":"(.+?)","</ stringProp > < stringProp name="RegexExtractor.template">$1$</ stringProp > < stringProp name="RegexExtractor.default">ERROR</ stringProp > < stringProp name="RegexExtractor.match_number">1</ stringProp > </ RegexExtractor > < hashTree /> < DebugPostProcessor guiclass="TestBeanGUI" testclass="DebugPostProcessor" testname="Debug PostProcessor -- 所属 :HTTP请求" 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="察看结果树" 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 > </ jmeterTestPlan > |
其中的 Debug PostProcessor 为调试所用,一般用于查看变量值,添加方法同 正则表达式提取器。
脚本执行结果如下所示:
至此, JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一) 顺利完结,希望此文能够给初学 JMeter 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一)概述及简单实例
标签:dai vgg monitor dmp ide reg 正则表达式 gty halt
原文地址:http://www.cnblogs.com/liunaixu/p/7352025.html