lr12中web services协议脚本开发应用
使用web_service_call,soap_request、web_custom_request函数完成脚本,并加上if判断
一、web_service_call函数使用
1.web_service_call脚本编写操作步骤(步骤与图片的顺序依次对应)
1)新建web services脚本;
2)点击【SOATools】,点击add service call;
3)service选择【import service】,会弹框显示【import service】,输入服务链接地址;
4)operation选择【getWeatherbyCityName】;
5)点击【theCityName】,填写value值,比如深圳;
6)点击【getWeatherbyCityNameResult】,选择保存返回结果;
7)点击ok,添加成功,如图所示。
2.该函数各参数注释说明如下:
Action() { web_service_call( "StepName=getWeatherbyCityName_101",//步骤的名称 "SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName",//服务器名称|soup|获取哪个接口(城市天气预报) "ResponseParam=response",//返回的参数信息 "Service=WeatherWebService",//webservice的服务 "ExpectedResponse=SoapResult",//请求的返回 "Snapshot=t1555558405.inf",//快照 BEGIN_ARGUMENTS,//输入参数开始 "theCityName=深圳",//选择的城市 END_ARGUMENTS,//输入参数结果 BEGIN_RESULT,//返回值的开始 "getWeatherbyCityNameResult[1]=Param_getWeatherbyCityNameResult",//返回的参数保存在Param_getWeatherbyCityNameResult END_RESULT,//返回值的结束 LAST); return 0; }
3.完善事物,进行优化并添加判断,代码如下:
Action() { lr_start_transaction("获取天气预报"); web_service_call( "StepName=getWeatherbyCityName_101",//步骤的名称 "SOAPMethod=WeatherWebService|WeatherWebServiceSoap|getWeatherbyCityName",//服务器名称|soup|获取哪个接口(城市天气预报) "ResponseParam=response",//返回的参数信息 "Service=WeatherWebService",//webservice的服务 "ExpectedResponse=SoapResult",//请求的返回 "Snapshot=t1555558405.inf",//快照 BEGIN_ARGUMENTS,//输入参数开始 "theCityName={city_name}",//选择的城市 END_ARGUMENTS,//输入参数结果 BEGIN_RESULT,//返回值的开始 "getWeatherbyCityNameResult/*[2]=Param_getWeatherbyCityNameResult",//返回的参数保存在Param_getWeatherbyCityNameResult END_RESULT,//返回值的结束 LAST); //判断是否获取天气预报成功 //判断返回结果的城市名称是否与查询天气的城市一致 if(strcmp(lr_eval_string("{Param_getWeatherbyCityNameResult}"),lr_eval_string("{city_name}"))==0) { lr_end_transaction("获取天气预报",LR_PASS); lr_error_message("返回结果:%s&&&&期望结果:%s",lr_eval_string("{Param_getWeatherbyCityNameResult}"),lr_eval_string("{city_name}")); } else { lr_end_transaction("获取天气预报",LR_FAIL); } return 0;
结果如图所示:
二、soap_request函数使用
1.创建脚本步骤
1)新建web services
2)选择【import SOAP】
3)选择【import SOAP】,打开WeatherWebService网站,将红色框中的内容,复制到记事本中,将记事本扩展名改为xml文件
4)引入刚刚创建的xml文件
5)填写信息
6)点击OK,完成本次脚本创建
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>string</theCityName>" "</getWeatherbyCityName>" "</soap:Body>" "</soap:Envelope>", "SOAPAction=http://WebXml.com.cn/getWeatherbyCityName", "ResponseParam=response", "Snapshot=t1555571488.inf", LAST); return 0; }
2.主要操作:对返回结果进行解析取值,红框是将返回数据保存在参数response中,如何操作取值?
方法:通过lr_xml_get_values函数对返回的response进行取值
1)将response取出放到记事本中,整理格式,方便分析;
2)像返回的json数据一样,逐层进入取值,红色框中为取值的字段;
3)再取getWeatherbyCityNameResult里面的值时,需要像元组一样,通过下标进行取值
最后得到取值的表达式:/Envelope/Body/getWeatherbyCityNameResponse/getWeatherbyCityNameResult/string[2]
3.进行断言,完善事务代码如下:
Action() { lr_convert_string_encoding(lr_eval_string("{city_name01}"),NULL,"utf-8","city");//将需要查询的城市进行转码 lr_save_string(lr_eval_string("{city}"),"cityname02"); //将转码后的城市进行保存 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>{cityname02}</theCityName>" //输入要查询的城市名称 "</getWeatherbyCityName>" "</soap:Body>" "</soap:Envelope>", "SOAPAction=http://WebXml.com.cn/getWeatherbyCityName", "ResponseParam=response", //将返回结果保存到response中 "Snapshot=t1555571488.inf", LAST); lr_output_message("转码后的查询城市:%s",lr_eval_string("{cityname02}")); //打印转码后的查询城市,确认是否转码成功 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_name_", LAST); //判断返回结果取值到的城市名称是否跟查询输入时的城市名称一致 if(strcmp(lr_eval_string("{city_name_}"),lr_eval_string("{city_name01}"))==0) { lr_end_transaction("获取天气预报", LR_PASS); lr_output_message("从返回结果取到的城市名称:%s",lr_eval_string("{city_name_}")); } else { lr_end_transaction("获取天气预报", LR_FAIL); } return 0; }
二、web_custom_request函数
1.点击打开该函数
2.按照要求输入必要的信息(步骤名称、请求方式、请求地址、请求参数、编码类型等),点击确定
3.通过web_reg_save_param_ex函数对关联接口返回的城市代码;
4.将请求的城市代码跟接口返回的城市代码进行比较判断;
完成的事务代码如下:
Action() { //关联接口返回的城市代码 web_reg_save_param_ex( "ParamName=res_city_code", "LB=娣卞湷</string><string>", "RB=</string>", "Ordinal=1", SEARCH_FILTERS, "Scope=ALL", LAST ); lr_start_transaction("获取天气预报"); web_custom_request( "web_custom_request", "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>{city_code}</theCityName>" "</getWeatherbyCityName>" "</soap12:Body>" "</soap12:Envelope>", LAST); //判断返回结果取值到的城市代码是否跟查询输入时的城市代码一致 if(strcmp(lr_eval_string("{res_city_code}"),lr_eval_string("{city_code}"))==0) { lr_end_transaction("获取天气预报", LR_PASS); lr_output_message("返回结果的城市代码:%s:",lr_eval_string("{res_city_code}")); //打印关联接口返回的城市代码 } else { lr_end_transaction("获取天气预报", LR_FAIL); } return 0; }
针对上面脚本实现的思路,有一个缺点,对应这种关联参数的设置,如果左边界的城市发生变化,那么就找不到城市代码了,所以最好通过设置检查点的方式写脚本,优化后脚本如下:
Action() { //设置检查点,检查 web_reg_find("Search=All", "SaveCount=res_code_count", "Text=59493", LAST); lr_start_transaction("获取天气预报"); web_custom_request( "web_custom_request", "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>{city_code}</theCityName>" "</getWeatherbyCityName>" "</soap12:Body>" "</soap12:Envelope>", LAST); //对城市代码出现的次数进行判断,出现次数大于1,即通过。 if(atoi(lr_eval_string("{res_code_count}"))>=1) //将城市代码出现的次数转换为int类型与1进行比较,并且此处结尾没有分号 { lr_end_transaction("获取天气预报", LR_PASS); lr_output_message("返回结果出现城市代码次数:%s:",lr_eval_string("{res_code_count}")); } else { lr_end_transaction("获取天气预报", LR_FAIL); } return 0; }