loadrunner之WebServices协议脚本编写(三种请求模式)
以天气预报网站为例:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
一、web_service_call模式
步骤如下:
经过增加事务和if 判断的脚本如下:
Action() { lr_start_transaction("获取天气预报"); web_service_call( "StepName=getWeatherbyCityName_102", "SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName", //服务名称|soap|获取哪个接口(城市天气预报) "ResponseParam=response", //返回的参数信息 "Service=WeatherWebService", // webservice的服务 "ExpectedResponse=SoapResult", //请求的返回 "Snapshot=t1555506477.inf", BEGIN_ARGUMENTS, //输入参数开始 "theCityName={NewParam}", //请求输入 END_ARGUMENTS, //结束参数 BEGIN_RESULT, //返回值得开始 "getWeatherbyCityNameResult/*[2]=Param_string", //返回的参数保存在Param_string END_RESULT, //返回值结束 LAST); if(strcmp(lr_eval_string("{NewParam}"),lr_eval_string("{Param_string}"))==0){ lr_end_transaction("获取天气预报", LR_PASS); }else{ lr_end_transaction("获取天气预报", LR_FAIL); } return 0; }
二、soap_request模式 (外加查看请求报文的小插曲)
当开发只给你一个地址时,我们可以使用 soap UI 工具查看 步骤如下:
lr12 操作步骤如下:
根据城市名获取天气预报的接口文档地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getSupportCity
脚本加上改造之后的如下:
Action() { lr_convert_string_encoding(lr_eval_string("{city_name}"),NULL,"utf-8","city"); lr_save_string(lr_eval_string("{city}"),"cityName"); // lr_think_time(10); 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=t1555573201.inf", LAST); lr_xml_get_values("XML={response}", "Query=/Envelope/Body/getWeatherbyCityNameResponse/getWeatherbyCityNameResult/string[2]", "ValueParam=responseValue", LAST); lr_output_message("返回城市名称:%s",lr_eval_string("{responseValue}")); if(strcmp(lr_eval_string("{responseValue}"),lr_eval_string("{city_name}"))==0){ //响应的参数 与参数化转码之前的参数对比 lr_end_transaction("获取天气预报", LR_PASS); }else{ lr_end_transaction("获取天气预报", LR_FAIL); } return 0; }
运行结果:
三、web_custom_request函数,创建HTTP协议完成webserive协议
脚本如下:
Action() { web_custom_request("web_custom_request", "URL=http://www.webxml.com.cn/WebServices/WeatherWebService.asmx", "Method=POST", "TargetFrame=", "Resource=0", "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>59134</theCityName>" "</getWeatherbyCityName>" "</soap12:Body>" "</soap12:Envelope>", LAST); return 0; }