1. 程式人生 > >jmeter中mail reader sampler郵件層級多,正則表示式提取器(regular expression extror)無法提取,使用BeanShellPostProcessor解決

jmeter中mail reader sampler郵件層級多,正則表示式提取器(regular expression extror)無法提取,使用BeanShellPostProcessor解決

問題描述:

1、jmeter通過“MailReader Sampler”連線伺服器,將傳送到郵箱的包含註冊驗證碼郵件下載到jmeter中,在郵件中提取驗證碼,進行後續註冊驗證,即:


2、使用“正則表示式提取器”進行驗證碼提取:


結果無法取到驗證碼;

3、使用“BeanShellPostProcessor”解決:


在“script”中編寫程式碼:

String  response =prev.getSubResults()[0].getSubResults()[0].getSubResults()[0].getResponseDataAsString(); 

vars.put("response",response.substring(70,76));


說明:

“prev”- is a shorthand for previous sampleresult. It returns Mail Reader Sampler

first getSubResults()[0] - stands forMessage 1.

second getSubResults()[0] - upper Part 0

third getSubResults[0] - Part 0 which I'mlooking for

vars.put("response", response.substring(70,76)) -stores message content as "response" variable. You can apply assertionon JMeter Variable as well. 

在之後的執行緒中引用引數:${response},即可取到驗證碼;

response.substring(70,76):這是郵件正文開頭到驗證碼前字元數量統計,總計69個,包括空格,符號:

<h3>尊敬的使用者您好:</h3><br><PRE>&#9;您於20161109 154247秒提交的賬戶安全郵箱驗證,驗證碼為:733486”

上面標紅的字元即為69個,漢字也按一個字元進行計算


參考:http://jmeter.512774.n5.nabble.com/Question-about-mail-reader-sampler-td5721461.html