使用loadrunner编写webservice接口请求

时间:2023-03-09 14:36:39
使用loadrunner编写webservice接口请求

1、使用工具:

loadrunner12,本实例截图中都是loadrunner12工具

2、操作步骤:

1)、新建脚本,选择Web Services协议:

使用loadrunner编写webservice接口请求

2)、选择工具栏:

使用loadrunner编写webservice接口请求

3)、点击Import,输入wsdl地址:测试代码用的地址:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

使用loadrunner编写webservice接口请求

4)、点击工具栏的Add Service Call:

使用loadrunner编写webservice接口请求

5)、选择接口名称:

使用loadrunner编写webservice接口请求

将返回数据保存到变量中:

使用loadrunner编写webservice接口请求

6)、完成后,生成的原始代码如下:

Action()
{
web_service_call( "StepName=getWeatherbyCityName_101",
"SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName",
"ResponseParam=response",
"Service=WeatherWebService",
"ExpectedResponse=SoapResult",
"Snapshot=t1555599719.inf",
BEGIN_ARGUMENTS,
"theCityName=city_name",
END_ARGUMENTS,
BEGIN_RESULT,
"getWeatherbyCityNameResult/*[1]=Param_string",
END_RESULT,
LAST); return ;
}

7)、对接口请求进行完善,进行参数化,添加事务等操作

Action()
{ int result; //存放对比结果 lr_start_transaction("获取城市天气预报"); web_service_call( "StepName=getWeatherbyCityName_102", //步骤名称
"SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName", //服务器名称|soap|获取的接口名称(城市天气预报)
"ResponseParam=response", //返回的参数信息
"Service=WeatherWebService", //webservice服务
"ExpectedResponse=SoapResult", //请求返回
"Snapshot=t1555558737.inf", //快照
BEGIN_ARGUMENTS, //输入参数开始
"theCityName={city_name}", //请求输入
END_ARGUMENTS, //结束参数
BEGIN_RESULT, //返回值的开始
"getWeatherbyCityNameResult/*[2]=Param_string", //返回的参数保存在Param_string
END_RESULT, //返回值的结束
LAST); result = strcmp(lr_eval_string("{city_name}"),lr_eval_string("{Param_string}")); //对比输入参数是否与输出结果一致 //判断是否相等
if(result==)
{
lr_end_transaction("获取城市天气预报", LR_PASS);
}
else
{
lr_end_transaction("获取城市天气预报", LR_FAIL);
} lr_output_message("result:%d",result); //将返回信息进行转码
lr_convert_string_encoding(lr_eval_string("{response}"),"utf-8",NULL,"testruslt");
lr_output_message("接口返回信息为:%s",lr_eval_string("{testruslt}")); return ;
}

运行结果:

Starting iteration .
Starting action Action.
Action.c(): Notify: Transaction "获取城市天气预报" started.
Action.c(): Web service call "getWeatherbyCityName_102" started
Action.c(): Web service call "getWeatherbyCityName_102" was successful
Action.c(): Notify: Transaction "获取城市天气预报" ended with a "Pass" status (Duration: 0.4751 Wasted Time: 0.2191).
Action.c(): result:
Action.c(): 接口返回信息为:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/"><getWeatherbyCityNameResult><string>直辖市</string><string>北京</string><string></string><string>.jpg</string><string>// ::</string><string>℃/℃</string><string>4月18日 多云</string><string>东北风转南风3-4级</string><string>.gif</string><string>.gif</string><string>今日天气实况:气温:℃;风向/风力:西南风 3级;湿度:%;紫外线强度:弱。空气质量:良。</string><string>紫外线指数:弱,辐射较弱,涂擦SPF12-、PA+护肤品。
健臻·血糖指数:较易波动,血糖较易波动,注意监测。
穿衣指数:较舒适,建议穿薄外套或牛仔裤等服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
空气污染指数:良,气象条件有利于空气污染物扩散。
</string><string>℃/℃</string><string>4月19日 多云转小雨</string><string>南风小于3级</string><string>.gif</string><string>.gif</string><string>℃/℃</string><string>4月20日 小雨转多云</string><string>东北风转北风小于3级</string><string>.gif</string><string>.gif</string><string>北京位于华北平原西北边缘,市中心位于北纬39度,东经116度,四周被河北省围着,东南和天津市相接。全市面积一万六千多平方公里,辖12区6县,人口1100余万。北京为暖温带半湿润大陆性季风气候,夏季炎热多雨,冬季寒冷干燥,春、秋短促,年平均气温10-12摄氏度。北京是世界历史文化名城和古都之一。早在七十万年前,北京周口店地区就出现了原始人群部落“北京人”。而北京建城也已有两千多年的历史,最初见于记载的名字为“蓟”。公元前1045年北京成为蓟、燕等诸侯国的都城;公元前221年秦始皇统一中国以来,北京一直是中国北方重镇和地方中心;自公元938年以来,北京又先后成为辽陪都、金上都、元大都、明清国都。1949年10月1日正式定为*首都。北京具有丰富的旅游资源,对外开放的旅游景点达200多处,有世界上最大的皇宫紫禁城、祭天神庙天坛、皇家花园北海、皇家园林颐和园,还有八达岭、慕田峪、司马台长城以及世界上最大的四合院恭王府等各胜古迹。全市共有文物古迹7309项,其中国家文物保护单位42个,市级文物保护单位222个。北京的市树为国槐和侧柏,市花为月季和菊花。另外,北京出产的象牙雕刻、玉器雕刻、景泰蓝、地毯等传统手工艺品驰誉世界。</string></getWeatherbyCityNameResult></getWeatherbyCityNameResponse></soap:Body></soap:Envelope>
Ending action Action.
Ending iteration .

3、通过引入soap的方式,编写webservices接口,测试链接:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName

      1)、将以上链接地址打开后,将一些部分代码复制粘贴到xml文件中:

<?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>string</theCityName>
</getWeatherbyCityName>
</soap:Body>
</soap:Envelope>

    2)、通过工具栏,引入soap:

使用loadrunner编写webservice接口请求

3)、输入相关信息:

使用loadrunner编写webservice接口请求

使用loadrunner编写webservice接口请求

4)、代码经过处理后:

Action()
{ int result; //存放对比结果 lr_start_transaction("获取城市天气预报"); //lr_convert_string_encoding("北京",LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"cityname"); //将中文的utf-8转换成lr的编码方式
lr_convert_string_encoding(lr_eval_string("{send_city}"),NULL,"utf-8","cityname");
lr_save_string(lr_eval_string("{cityname}"),"city_name");
soap_request(
"StepName=SOAP Request", //步骤名称
"URL=http://www.webxml.com.cn//WebServices/WeatherWebService.asmx", //请求的url地址
"SOAPEnvelope=" //发送到服务器的xml包
"<?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>{city_name}</theCityName>"
"</getWeatherbyCityName>"
"</soap:Body>"
"</soap:Envelope>",
"SOAPAction=http://WebXml.com.cn/getWeatherbyCityName",
"ResponseParam=response", //存储服务器响应的输出参数的名称
"Snapshot=t1555584007.inf",
LAST); lr_convert_string_encoding(lr_eval_string("{response}"),"utf-8",NULL,"testruslt"); lr_output_message("接口返回信息为:%s",lr_eval_string("{testruslt}")); lr_xml_get_values(
"XML={response}", //获取返回消息体
"Query=/Envelope/Body/getWeatherbyCityNameResponse/getWeatherbyCityNameResult/string[2]", //对输入字符串xml的查找或快速查找,指定元素或属性
"ValueParam=response_ctiy", //存储查询结果的输出参数的名称
LAST); //lr_output_message("返回的城市名称:%s",lr_eval_string("{response_ctiy}")); result = strcmp(lr_eval_string("{send_city}"),lr_eval_string("{response_ctiy}")); //对比输入参数是否与输出结果一致 //判断是否相等
if(result==)
{
lr_end_transaction("获取城市天气预报", LR_PASS);
}
else
{
lr_end_transaction("获取城市天气预报", LR_FAIL);
} return ;
}

运行结果:

Starting iteration .
Starting action Action.
Action.c(): Notify: Transaction "获取城市天气预报" started.
Action.c(): SOAP request "SOAP Request" started
Action.c(): SOAP request "SOAP Request" was successful
Action.c(): 接口返回信息为:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/"><getWeatherbyCityNameResult><string>直辖市</string><string>北京</string><string></string><string>.jpg</string><string>// ::</string><string>℃/℃</string><string>4月18日 多云</string><string>东北风转南风3-4级</string><string>.gif</string><string>.gif</string><string>今日天气实况:气温:℃;风向/风力:南风 3级;湿度:%;紫外线强度:弱。空气质量:良。</string><string>紫外线指数:弱,辐射较弱,涂擦SPF12-、PA+护肤品。
健臻·血糖指数:较易波动,血糖较易波动,注意监测。
穿衣指数:较舒适,建议穿薄外套或牛仔裤等服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
空气污染指数:良,气象条件有利于空气污染物扩散。
</string><string>℃/℃</string><string>4月19日 多云转小雨</string><string>南风小于3级</string><string>.gif</string><string>.gif</string><string>℃/℃</string><string>4月20日 小雨转多云</string><string>东北风转北风小于3级</string><string>.gif</string><string>.gif</string><string>北京位于华北平原西北边缘,市中心位于北纬39度,东经116度,四周被河北省围着,东南和天津市相接。全市面积一万六千多平方公里,辖12区6县,人口1100余万。北京为暖温带半湿润大陆性季风气候,夏季炎热多雨,冬季寒冷干燥,春、秋短促,年平均气温10-12摄氏度。北京是世界历史文化名城和古都之一。早在七十万年前,北京周口店地区就出现了原始人群部落“北京人”。而北京建城也已有两千多年的历史,最初见于记载的名字为“蓟”。公元前1045年北京成为蓟、燕等诸侯国的都城;公元前221年秦始皇统一中国以来,北京一直是中国北方重镇和地方中心;自公元938年以来,北京又先后成为辽陪都、金上都、元大都、明清国都。1949年10月1日正式定为*首都。北京具有丰富的旅游资源,对外开放的旅游景点达200多处,有世界上最大的皇宫紫禁城、祭天神庙天坛、皇家花园北海、皇家园林颐和园,还有八达岭、慕田峪、司马台长城以及世界上最大的四合院恭王府等各胜古迹。全市共有文物古迹7309项,其中国家文物保护单位42个,市级文物保护单位222个。北京的市树为国槐和侧柏,市花为月季和菊花。另外,北京出产的象牙雕刻、玉器雕刻、景泰蓝、地毯等传统手工艺品驰誉世界。</string></getWeatherbyCityNameResult></getWeatherbyCityNameResponse></soap:Body></soap:Envelope>
Action.c(): "lr_xml_get_values" succeeded, match processed
Action.c(): Notify: Transaction "获取城市天气预报" ended with a "Pass" status (Duration: 0.2918 Wasted Time: 0.0522).
Ending action Action.
Ending iteration .