吴老的《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源创计划”,欢迎正在阅读的你也加入,一块儿分享。