性能测试学习 第八课--LR12中针对WebServices协议的三种脚本开发模式
一,webservices协议简介
webservices是建立可交互操作的分布式应用程序的新平台,它通过一系列的标准和协议来保证程序之间的动态连接,
其中最基本的协议包括soap,wsdl,uddi.
1,SOAP(simple object access protocl)
SOAP是消息传递协议,它规定了web services之间如何传递消息。SOAP基于xml和xsd,xml是soap的数据编码方式。
2,WSDL(web services Description Language)
WSDL是web services的定义语言,和soap一起构成web服务的核心结构单元。wsdl协议规定了有关webservices描述的标准。
3,UDDI(Universal Description,Discovery,and Intergration)
UDDI是访问登记的标准,它建立了一个平台独立,开放的框架,通过英特网来描述服务,发现业务并整合业务服务。简单来说
UDDI用于集中存放和查找wsdl描述文件,起着目录服务器的作用。
二,我们在性能测试的工作中,难免会遇到webservices协议的接口,这里我简单介绍一下用loadrunner12来开发webservices协议脚本
的三种模式,咱们就以天气预报的网站来为例吧:
1,web_service_call模式
1)启动“virtual user Generator”,新建“web services”虚拟用户,
2)选择上方SOA Tool中的Add Service Call,如下图
3)弹出New Web Service Call对话框,选择Service-import service
4)输入要测试的网址
5)下面选择Operation,输入城市,以及设置获取返回值参数
6)点击OK后,得到下面的脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Action() { web_service_call( "StepName=getWeatherbyCityName_104" , //步骤名称 "SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName" , //服务名称|soap获取哪个接口 "ResponseParam=response" , //返回的参数信息 "Service=WeatherWebService" , //webservices的服务 "ExpectedResponse=SoapResult" , //请求返回的信息 "Snapshot=t1555545923.inf" , //快照 BEGIN_ARGUMENTS, //输入参数开始 "theCityName=广州" , //请求的参数与值 END_ARGUMENTS, //结束参数 BEGIN_RESULT, //返回值的开始 "getWeatherbyCityNameResult/*[1]=Param_string" , //保存返回参数 END_RESULT, //返回值结束 LAST); return 0; } |
6)然后做参数化,断言
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
|
Action() { lr_start_transaction( "获取天气预报" ); web_service_call( "StepName=getWeatherbyCityName_104" , //步骤名称 "SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName" , //服务名称|soap获取哪个接口 "ResponseParam=response" , //返回的参数信息 "Service=WeatherWebService" , //webservices的服务 "ExpectedResponse=SoapResult" , //请求返回的信息 "Snapshot=t1555545923.inf" , //快照 BEGIN_ARGUMENTS, //输入参数开始 "theCityName={city_name}" , //请求的参数与值 END_ARGUMENTS, //结束参数 BEGIN_RESULT, //返回值的开始 "getWeatherbyCityNameResult/*[2]=Param_string" , //保存返回参数 END_RESULT, //返回值结束 LAST); if (strcmp(lr_eval_string( "{Param_string}" ),lr_eval_string( "{city_name}" ))==0) { lr_end_transaction( "获取天气预报" ,LR_PASS); } else { lr_end_transaction( "获取天气预报" ,LR_FAIL); } return 0; } |
7)最后保存,设置日志级别,然后Replay我们的脚本
2,soap_request模式
1)打开WeatherWebService网站,
2)复制上面的代码,在notepad++中新建一个文件,将代码粘贴上去,保存到D盘,然后在loadrunner12中
点击下图的import SOAP
3)在对话框中,我们输入保存的xml文件地址,输入URL,URL=http://{Host}+{POST} 参数从
上图所示取,输入对应的SOAPAction,具体如下图所示
4)点击OK,生成下面的脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Action() { soap_request( "StepName=SOAP Request" , "URL=http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" , "SOAPEnvelope=" "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" "<soap:Body>" "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" "<theCityName>广州</theCityName>" "</getWeatherbyCityName>" "</soap:Body>" "</soap:Envelope>" , "SOAPAction=http://WebXml.com.cn/getWeatherbyCityName" , "ResponseParam=response" , "Snapshot=t1555549382.inf" , LAST); return 0; } |
5)这里有个难题就是如何做xml断言?我们看一下下图的响应示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <? xml version="1.0" encoding="utf-8"?> < soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> < soap12:Body > < getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/"> < getWeatherbyCityNameResult > < string >string</ string > < string >string</ string > </ getWeatherbyCityNameResult > </ getWeatherbyCityNameResponse > </ soap12:Body > </ soap12:Envelope > |
这里引入lr_xml_get_values函数来获取xml的返回值
XPath query中填入city值的xpath定位的绝对路径:/Envelope/Body/getWeatherbyCityNameResponse/getWeatherbyCityNameResult/string[2]
6)最后加入事物函数,做断言
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
|
Action() { lr_convert_string_encoding(lr_eval_string( "{city_name}" ), NULL , "utf-8" , "city" ); lr_save_string(lr_eval_string( "{city}" ), "cityName" ); lr_start_transaction( "获取天气预报" ); soap_request( "StepName=SOAP Request" , "URL=http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" , "SOAPEnvelope=" "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" "<soap:Body>" "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" "<theCityName>{cityName}</theCityName>" "</getWeatherbyCityName>" "</soap:Body>" "</soap:Envelope>" , "SOAPAction=http://WebXml.com.cn/getWeatherbyCityName" , "ResponseParam=response" , "Snapshot=t1555549382.inf" , LAST); lr_convert_string_encoding(lr_eval_string( "{response}" ), "utf-8" , NULL , "msg" ); lr_xml_get_values( "XML={response}" , "Query=/Envelope/Body/getWeatherbyCityNameResponse/getWeatherbyCityNameResult/string[2]" , "ValueParam=city_code" , LAST); lr_output_message( "返回城市名称:%s" ,lr_eval_string( "city_code" )); if (strcmp(lr_eval_string( "{city_code}" ),lr_eval_string( "{city_name}" ))==0) { lr_end_transaction( "获取天气预报" , LR_PASS); } else { lr_end_transaction( "获取天气预报" , LR_FAIL); } return 0; } |
7)保存后点击Replay,得到下面的结果:
3,web_custom_request模式
输入对应的URL,body里面输入SOAP的请求体,点击确定,最后生成如下的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
Action() { web_custom_request( "web_services" , "URL=http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" , "Method=POST" , "TargetFrame=" , "Resource=1" , "Referer=" , "Mode=HTTP" , "EncType=application/soap+xml; charset=utf-8" , "Body=<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" "<soap12:Body>" "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" "<theCityName>59287</theCityName>" "</getWeatherbyCityName>" "</soap12:Body>" "</soap12:Envelope>" , LAST); return 0; } |
后面做脚本强化就行了。