1、web services协议简介
web services协议是建立可交互操作的分布式应用程序的新平台,它通过一系列标准和协议来保证程序之间的动态链接,其中最基本的协议包括soap,wsdl,uddi。
2、若开发只给了一个webservices的url,未提供报文请求格式,要怎么操作?http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
用soap ui工具可获得
1)新建soap项目,命名且输入wsdl url,点击ok即可;
2)可查看到该url下的所有接口,双击你要查看的接口名,就可以在右边窗口查看到响应的请求报文;
3、loadrunner操作
创建web services脚本,以获取天气预报服务为例;http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
a、此次运用了三个模式来创建脚本请求:web_service_call、soap_request、web_custom_request
b、请求中带中文,不转码导致得到的响应非所期望的,此时需要转码,如下:
lr_convert_string_encoding("深圳",NULL,"utf-8","cityname");
lr_save_string(lr_eval_string("{cityname}"),"cityname");
c、响应中由于有中文显示出为乱码,转码如下:
lr_convert_string_encoding(lr_eval_string("{response}"),"utf-8",NULL,"msg");
d、若响应时一个xml,可用lr_xml_get_values()
例:lr_xml_get_values("XML={response}",
"Query=/Envelope/Body/getWeatherbyCityNameResponse/getWeatherbyCityNameResult/string[2]",
"ValueParam=getcityname",
LAST);
web_service_call模式:
1)在SOA Tools中选中Manage Services
2)导入url地址
3)直接点击ok
4)在SOA Tools里点击add service call
5)选择好服务名、接口和soap,再填写输入参数
6)填写输出参数名
点击ok后,在loadrunner里生成了web_service_call请求;
对脚本请求做修改,添加事物、if判断,脚本如下:
Action()
{
lr_start_transaction("获取天气预报城市名");
web_service_call( "StepName=getWeatherbyCityName_101",//步骤名称
"SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName",//服务名称 soap 获取那个接口 (城市天气预报)
"ResponseParam=response",//返回的参数信息
"Service=WeatherWebService",//webservice的服务
"ExpectedResponse=SoapResult",//请求的返回
"Snapshot=t1555823842.inf",//快照
BEGIN_ARGUMENTS,//输入参数 开始
"theCityName={cityname}",//请求输入,城市=深圳或者其它城市名称
END_ARGUMENTS,//结束参数
BEGIN_RESULT,//返回值的开始
"getWeatherbyCityNameResult/*[2]=Param_string",//返回参数保存在Param_string
END_RESULT,//返回值 的结束
LAST);
if (strcmp(lr_eval_string("{Param_string}"),lr_eval_string("{cityname}"))==0)
{
lr_end_transaction("获取天气预报城市名", LR_PASS);
}
else
{
lr_end_transaction("获取天气预报城市名", LR_FAIL);
}
return 0;
}
soap request模式:
1)在SOA Tools下选中import SOAP;
2)选中soap的xml文件,根据例子,填写好url和soap actions;
3)点击ok,生成一个soap_request请求在脚本里;
4)修改脚本,添加if判断,脚本如下:
Action()
{
lr_convert_string_encoding(lr_eval_string("{getcity}"),NULL,"utf-8","cityname");
lr_save_string(lr_eval_string("{cityname}"),"cityname1");
lr_start_transaction("获取城市天气预报");
soap_request("StepName=SOAP Request", //步骤名称
"URL=http://www.webxml.com.cn/WebServices/WeatherWebService.asmx",//请求的url地址
"SOAPEnvelope="//发送到服务器的xml包
"<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>{cityname1}</theCityName>"
"</getWeatherbyCityName>"
"</soap:Body>"
"</soap:Envelope>",
"SOAPAction=http://WebXml.com.cn/getWeatherbyCityName",
"ResponseParam=response", //存储服务器 响应的输出参数名称
"Snapshot=t1555845699.inf",
LAST);
//lr_convert_string_encoding(lr_eval_string("{response}"),"utf-8",NULL,"msg");
//lr_output_message("msg结果:%s",lr_eval_string("{msg}"));
lr_xml_get_values("XML={response}",
"Query=/Envelope/Body/getWeatherbyCityNameResponse/getWeatherbyCityNameResult/string[2]",
"ValueParam=getcityname",
LAST);
if (strcmp(lr_eval_string("{getcityname}"),lr_eval_string("{getcity}"))==0)
{
lr_end_transaction("获取城市天气预报", LR_PASS);
lr_output_message("获取指定城市天气成功");
}
else
{
lr_end_transaction("获取城市天气预报", LR_FAIL);
lr_output_message("获取指定城市天气失败");
}
return 0;
}
web_custom_request模式:
1)此模式较简单,与http协议post请求方法一致:
2)脚本Action()
{
lr_convert_string_encoding(lr_eval_string("{getcity}"),NULL,"utf-8","cityname0");
lr_save_string(lr_eval_string("{cityname0}"),"cityname1");
web_reg_save_param_ex(
"ParamName=recityname",
"LB=<string>",
"RB=</string>",
"Ordinal=ALL",
SEARCH_FILTERS,
LAST);
lr_start_transaction("获取城市天气预报");
web_custom_request("web_custom_request",
"URL=http://www.webxml.com.cn/WebServices/WeatherWebService.asmx",
"Method=POST",
"TargetFrame=",
"Resource=0",
"Referer=",
"Mode=HTTP",
"EncType=text/xml; charset=utf-8",
"Body=<?xml version="1.0" encoding="utf-8"?>"
"<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>{cityname1}</theCityName>"
"</getWeatherbyCityName>"
"</soap:Body>"
"</soap:Envelope>",
LAST);
lr_convert_string_encoding(lr_eval_string("{recityname_2}"),"utf-8",NULL,"recityname_2");
lr_save_string(lr_eval_string("{recityname_2}"),"recityname");
if (strcmp(lr_eval_string("{recityname}"),lr_eval_string("{cityname}"))==0)
{
lr_end_transaction("获取城市天气预报", LR_PASS);
lr_output_message("获取城市天气预报成功");
}
else
{
lr_end_transaction("获取城市天气预报", LR_PASS);
lr_error_message("获取城市天气预报失败");
}
return 0;
}