JMeter學習-007-JMeter 後置處理器實例之 - 正則表達式提取器(一)概述及簡單實例

上文我們講述瞭如何對 HTTP請求 的響應數據進行斷言,以判斷響應是否符合我們的預期,

那麼我們如何獲取 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 等。當下以 正則表達式提取器爲例演示講解,對應的添加路徑爲:【添加/後置處理器/正則表達式提取器】,添加後目錄結果如下:

對應添加的正則表達式提取器如下所示:

正則表達式提取器說明:

  • Apply to:應用範圍
  • 要檢查的響應字段:樣本數據源。
  • 引用名稱:其他地方引用時的變量名稱,引用方法:${引用名稱}
  • 正則表達式:數據提取器,如上圖的 "sysNo":"(.+?)"," 其中 (.+?) :爲非貪婪匹配,建議均使用非貪婪匹配,除非特殊情況。不熟悉正則的,勞煩聯繫度娘或者谷大爺,謝謝!
  • 模板:對應正則表達式提取器類型,樣式爲:11。若爲:00,則爲所有的匹配數據,例如:"sysNo":"123453463"," 其中 123453463 爲 (.+?) 匹配的數據,即最終提取的目標部分。若模板爲:11,則 1 對應正則表達式中的 (.+?)
  • 匹配數字:正則表達式匹配數據的最終結果可以看做一個數組,匹配數字即可看做是數組的第幾個元素。當爲 0 時,隨機返回匹配的數據,當爲 1 時,表示返回匹配結果數組的第一個元素。
  • 缺省值:匹配失敗時的默認值。通常用於後續的邏輯判斷,一般通常爲特定含義的英文大寫單詞組合,簡單可寫爲 ERROR。

對應的腳本源碼如下所示:

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 爲調試所用,一般用於查看變量值,添加方法同 正則表達式提取器。

腳本執行結果如下所示:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章