吳老的《selenium webdriver 實戰寶典》出版了html
前天接到了一個測試任務,要求測試一下ES(elsticsearch)在不一樣併發下的查詢效率。如圖:web
業務場景是在客戶端根據具體車牌查詢相關車輛信息,結果返回前10條記錄。從圖中能夠看到,接口的請求參數和返回結果均是JSON字符串,請求能夠用POST或者GET方法。先說GET方法:json
1、GET方法測試數組
1. Insert - New step -選擇Custom Request - web_url服務器
2. 填入相應參數微信
3. 生成腳本,並修改以下網絡
Action()session
{併發
//添加集合點app
lr_rendezvous("jihedian");
lr_start_transaction("getTop10");
//插入檢查點,檢查返回值是否包含kakoTypeName
web_reg_find(
"Search=Body",
"Text=kakoTypeName",
LAST );
//發送get請求
web_url("www.abc.com",
"URL=http://192.168.3.33:9200/_search?{%22query%22:{%22bool%22:{%22must%22:[{%22term%22:{%22plateNumNond%22:%22%E9%B2%81{NewParam}%22}}],%22must_not%22:[],%22should%22:[]}},%22from%22:0,%22size%22:10,%22sort%22:[],%22aggs%22:{}}",
"TargetFrame=",
"Resource=0",
"RecContentType=application/json",
"Snapshot=t1.inf",
"Mode=HTML",
LAST );
lr_end_transaction("getTop10", LR_AUTO);
//打印本次取的車牌號
lr_output_message( "the platenum is #%s", lr_eval_string("{NewParam}" ) );
return 0;
}
4. 查看返回結果
Virtual User Script started at : 2016-09-22 14:16:53
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0.0 for Windows 7;build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143]
Run Mode: HTML [MsgId: MMSG-26000]
Run-Time Settings file:"F:\PassCarSearch\ESqueryByPlateNumNond_GET\\default.cfg" [MsgId: MMSG-27141]
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(4): Rendezvous jihedian
Action.c(5): Notify: Transaction "getTop10"started.
Action.c(7): Registering web_reg_find was successful [MsgId: MMSG-26390]
Action.c(12): Registered web_reg_find successful for"Text=kakoTypeName" (count=10) [MsgId: MMSG-26364]
Action.c(12): web_url("www.abc.com") wassuccessful, 9058 body bytes, 88 header bytes [MsgId: MMSG-26386]
Action.c(20): Notify: Transaction "getTop10"ended with "Pass" status (Duration: 3.1371 Wasted Time: 0.4776).
Action.c(22): the platenum is #UT5387
Ending action Action.
Ending iteration 1.
說明:
1. 返回結果中能夠看到檢查點和事務都成功了,代表咱們的腳本編寫無誤。
2. 查看服務器返回的結果需在Vuser-Runtime-settings的log選項下,勾選Enable-logging、Extended log、Data returned by server 。
2、POST方法測試
在用POST方法建立腳本時遇到了點波折——先是使用了函數web_submit_date,執行時報錯,查詢資料沒找到緣由,不知道是否是該函數不支持JSON串,有興趣的能夠本身試下。而後嘗試用web_custom_request函數,執行後返回的結果都正確,ok,就它了。
1. Insert - New step -選擇Custom Request - web_custom_request
2. 填入相應參數
3. 生成腳本,並修改以下(參數中的引號"前須要加斜槓\轉譯)
Action()
{
lr_start_transaction("querybypost");
//插入檢查點,檢查返回值是否包含t_query_data
web_reg_find(
"Text=max_score",
LAST );
web_custom_request("querybypost", //VuGen中樹形視圖中顯示的名稱
"Url=http://192.168.3.33:9200/_search", //請求url
"Method=POST",
"Resource=0",
"Mode=HTTP", //請求方式
"Referer=",
"EncType=application/json", //指定響應頭的Content-Type,這裏是JSON
"RecContentType=application/json", //指定請求頭的Content-Type,這裏是JSON
"Body={\"query\":{\"bool\":{\"must\":[{\"term\":{\"plateNumNond\":\"<PlateNumNond>\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"aggs\":{}}:", //body的內容
LAST);
lr_end_transaction("querybypost", LR_AUTO);
lr_output_message( "PlateNumNond on iteration#%s", lr_eval_string( "<PlateNumNond>" ) );
}
4. 查看返回結果
Virtual User Script started at : 2016-09-21 16:40:04
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0.0 for Windows 7;build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143]
Run Mode: HTML [MsgId: MMSG-26000]
Run-Time Settings file:"F:\PassCarSearch\ESqueryByPlateNumNond_POST\\default.cfg" [MsgId: MMSG-27141]
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(6): Notify: Transaction "querybypost"started.
Action.c(9): Registering web_reg_find was successful [MsgId: MMSG-26390]
Action.c(14): Warning: The string'"plateNumNond":"B23456"' with parameter delimiters is nota parameter.
Action.c(14): Warning: The string '' with parameterdelimiters is not a parameter.
Action.c(14): t=770ms: 87-byte response headers for"http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1)
Action.c(14): HTTP/1.1 200 OK\r\n
Action.c(14): Content-Type: application/json; charset=UTF-8\r\n
Action.c(14): Content-Length: 124\r\n
Action.c(14): \r\n
Action.c(14): t=808ms: 124-byte response body for"http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1)
Action.c(14): {"took":9,"timed_out":false,"_shards":{"total":40,"successful":40,"failed":0},"hits":{"tot
Action.c(14): al":0,"max_score":null,"hits":[]}}
Action.c(14): Error -26366: "Text=t_query_data"not found for web_reg_find [MsgId:MERR-26366]
Action.c(14): web_custom_request("querybypost")highest severity level was "ERROR", 124 body bytes, 87 headerbytes [MsgId: MMSG-26388]
Action.c(14): Notify: Transaction "querybypost"ended with "Fail" status (Duration: 0.9686 Wasted Time: 0.7094).
Ending action Action.
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.
好吧,報錯了。返回結果中的提示是Warning: The string'"plateNumNond":"B23456"' with parameter delimiters is nota parameter.
本着有問題找度孃的一向態度,將這句話複製到度娘中檢索,但結果不遂人願,沒有查詢到解決思路。中間不斷的思考是否是本身的代碼出了問題,但最終把懷疑一一排除。後來忽然想到在loadrunner中,參數化的標誌是{},我在body裏面的{}並非參數化,而是json的格式。。。終於找到緣由了,接下來就簡單了,只需在Tool - General Options - Parameterization 中將ParameterBraces 改成<>便可,如圖
1. 從新運行,查看結果。 看到如下結果,ok,搞定,收工!
Virtual User Script started at : 2016-09-21 17:49:10
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0.0 for Windows 7;build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143]
Run Mode: HTML [MsgId: MMSG-26000]
Run-Time Settings file:"F:\PassCarSearch\ESqueryByPlateNumNond_POST\\default.cfg" [MsgId: MMSG-27141]
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(3): Notify: Transaction "querybypost"started.
Action.c(6): Registering web_reg_find was successful [MsgId: MMSG-26390]
Action.c(11): Notify: Parameter Substitution: parameter"PlateNumNond" = "魯UTR294"
Action.c(11): t=1729ms: 87-byte response headers for"http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1)
Action.c(11): HTTP/1.1 200 OK\r\n
Action.c(11): Content-Type: application/json; charset=UTF-8\r\n
Action.c(11): Content-Length: 124\r\n
Action.c(11): \r\n
Action.c(11): t=1885ms: 124-byte response body for"http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1)
Action.c(11): {"took":2,"timed_out":false,"_shards":{"total":40,"successful":40,"failed":0},"hits":{"tot
Action.c(11): al":0,"max_score":null,"hits":[]}}
Action.c(11): Registered web_reg_find successful for"Text=max_score" (count=1) [MsgId: MMSG-26364]
Action.c(11): web_custom_request("querybypost")was successful, 124 body bytes, 87 header bytes [MsgId: MMSG-26386]
Action.c(22): Notify: Transaction "querybypost"ended with "Pass" status (Duration: 1.8611 Wasted Time: 0.6362).
Action.c(24): Notify: Parameter Substitution: parameter"PlateNumNond" = "魯UTR294"
Action.c(24): PlateNumNond on iteration #魯UTR294
Ending action Action.
Ending iteration 1.
3、web_custom_request和web_submit_data區別
在解決問題的過程當中查詢了web_custom_request和web_submit_data區別,現附錄以下:
web_custom_request方法能夠發送POST和GET類型的請求;
web_submit_data只能發送POST類型的請求;
全部web_submit_data方法發送的請求均可以使用web_custom_request來實現
web_custom_request能夠實現web_submit_data沒法實現的請求,好比「查詢全部郵件並刪除」這個案例中,查詢時咱們使用關聯把全部郵件對應的標識抓取成一個數組,若是使用web_submit_data來完成這個刪除的請求,須要不少個web_submit_data請求才能完成,但使用web_custom_request就能夠經過一個請求完成,方法是本身寫代碼拼一個eb_custom_request
方法POST請求的Body值。
1. web_submit_data
請求中提交的數據格式:「Name=屬性名稱,」,「Value=屬性值」
例如:
"Name=username″,"Value=12044″,
ENDITEM,
"Name=password″,"Value=123456″,
ENDITEM,
"Name=typeId″,"Value=1″,
ENDITEM,
若是想提交的某個屬性包含包含多個值(好比說批量刪除),單個web_submit_data就沒法處理了,只能經過多個web_submit_data來處理。
2. web_custom_request
提交的數據(body)格式:「Body=屬性名稱=屬性值&屬性名稱=屬性值&……」
下面是一個典型的web_submit_data和web_custom_request請求,能夠看到web_custom_request中提交的數據(body)是以這樣的方式存在的,以下:
web_submit_data("searchRecvOrgsname",
"Action=http://{url}/searchRecvOrgsname",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://{url}/login_wj;jsessionid={jsessionid}",
"Snapshot=t18.inf",
"Mode=HTML",
ITEMDATA,
"Name=orgsId",
"Value={orgsId}", ENDITEM,
"Name=code",
"Value={order_end_station_code}", ENDITEM,
LAST);
web_custom_request("searchVehiclePopUp",
"URL=http://{url}/searchVehiclePopUp",
"Method=POST",
"TargetFrame=",
"Resource=0",
"RecContentType=text/html",
"Referer=http://{url}/login_wanjia;jsessionid={jsessionid}",
"Snapshot=t19.inf",
"Mode=HTML",
"EncType=application/x-www-form-urlencoded;
charset=UTF-8",
"Body=&orgsId={orgsId}&order_start_station_id={order_start_station_id}&targetcode=order_truck_no&targetname=order_truck_name&targetid=order_truck_id",
LAST);
兩種狀況下的POST請求會被LoadRunner錄製爲web_custom_request:
上文提到的批量提交多條同屬性名稱的數據的請求
header屬性x-requested-by值爲XMLHttpRequest的POST請求
這兩種實現請求的方法還有一個須要注意的地方就是web_custom_request中body中的屬性值若是包含一些特殊字符,必須經過URL編碼,不然Web服務器會返回500錯誤,一個典型的例子是若是Body中包含ViewState,ViewState中經常有「=」之類的特殊字符,此時必須經過URL編碼,LoadRuner中提供了一個這樣的編碼轉換函數:
web_convert_param(「vs1″,
「SourceEncoding=HTML」,「TargetEncoding=URL」, LAST);
3. web_custom_request函數詳解
A.語法:
intweb_custom_request( const char
*RequestName, ,
[EXTRARES, ,] LAST );
B.返回值:返回LR_PASS(0)表明成功,LR_FAIL(1)表明失敗。
C.參數:
(1)RequestName:步驟的名稱,VuGen中樹形視圖中顯示的名稱。
(2)List of Attribute:屬性列表,支持的屬性有如下幾種:
a.URL:頁面地址。
b.Method:頁面的提交方式,POST或GET。
c.EncType:編碼類型。
d.TargetFrame:當前連接或資源所在Frame的名稱。
除了Frame的名字,還能夠指定下面的參數:
_BLANK:打開一個空窗口。
_PARENT:把最新更改過的的Frame替換爲它的上級。
_SELF:替換最新更改過的的Frame。
_TOP:替換整個頁面。
實時音頻請關注喜馬拉雅 光榮之路電臺 http://m.ximalaya.com/zhubo/44966139 訂閱專輯、評論、點贊、收藏及分享,通通都要!還能彈幕喲!快粉我!
若是您有想要吳老講的話題,請聯繫咱們!QQ羣中呼喚吳老或公衆號內留言「吳老電臺-您的話題」。
公益傳播測試知識、技能與正能量!感謝做者!
分享測試生活,思考測試人生!歡迎投稿!
文章圖片來自網絡,若有侵權請見諒,請聯繫咱們妥善處理。
735821166@qq.com
本文分享自微信公衆號 - 軟件測試經驗與教訓(udatest)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。