使用loadrunner編寫webservice接口請求


1、使用工具:

     loadrunner12,本實例截圖中都是loadrunner12工具

2、操作步驟:

      1)、新建腳本,選擇Web Services協議:

             

        2)、選擇工具欄:

               

         3)、點擊Import,輸入wsdl地址:測試代碼用的地址:

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

               

        4)、點擊工具欄的Add Service Call:

              

 

        5)、選擇接口名稱:

              

             將返回數據保存到變量中:

             

           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 0;
}

                    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==0)
    {
        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 0;
}

 

                     運行結果:

Starting iteration 1.
Starting action Action.
Action.c(6): Notify: Transaction "獲取城市天氣預報" started.
Action.c(8): Web service call "getWeatherbyCityName_102" started
Action.c(8): Web service call "getWeatherbyCityName_102" was successful
Action.c(28): Notify: Transaction "獲取城市天氣預報" ended with a "Pass" status (Duration: 0.4751 Wasted Time: 0.2191).
Action.c(35): result:0
Action.c(39): 接口返回信息為:<?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>54511</string><string>54511.jpg</string><string>2019/4/18 22:53:05</string><string>10℃/23℃</string><string>4月18日 多雲</string><string>東北風轉南風3-4級</string><string>1.gif</string><string>1.gif</string><string>今日天氣實況:氣溫:12℃;風向/風力:西南風 3級;濕度:36%;紫外線強度:弱。空氣質量:良。</string><string>紫外線指數:弱,輻射較弱,塗擦SPF12-15、PA+護膚品。
健臻·血糖指數:較易波動,血糖較易波動,注意監測。
穿衣指數:較舒適,建議穿薄外套或牛仔褲等服裝。
洗車指數:較適宜,無雨且風力較小,易保持清潔度。
空氣污染指數:良,氣象條件有利於空氣污染物擴散。
</string><string>10℃/17℃</string><string>4月19日 多雲轉小雨</string><string>南風小於3級</string><string>1.gif</string><string>7.gif</string><string>10℃/16℃</string><string>4月20日 小雨轉多雲</string><string>東北風轉北風小於3級</string><string>7.gif</string><string>1.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 1.

 

 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:

                 

           3)、輸入相關信息:

                 

                 

            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==0)
    {
        lr_end_transaction("獲取城市天氣預報", LR_PASS);
    }
    else
    {
        lr_end_transaction("獲取城市天氣預報", LR_FAIL);
    }

    
    return 0;
}

 

                         運行結果:

Starting iteration 1.
Starting action Action.
Action.c(6): Notify: Transaction "獲取城市天氣預報" started.
Action.c(11): SOAP request "SOAP Request" started
Action.c(11): SOAP request "SOAP Request" was successful
Action.c(31): 接口返回信息為:<?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>54511</string><string>54511.jpg</string><string>2019/4/18 23:14:10</string><string>10℃/23℃</string><string>4月18日 多雲</string><string>東北風轉南風3-4級</string><string>1.gif</string><string>1.gif</string><string>今日天氣實況:氣溫:12℃;風向/風力:南風 3級;濕度:36%;紫外線強度:弱。空氣質量:良。</string><string>紫外線指數:弱,輻射較弱,塗擦SPF12-15、PA+護膚品。
健臻·血糖指數:較易波動,血糖較易波動,注意監測。
穿衣指數:較舒適,建議穿薄外套或牛仔褲等服裝。
洗車指數:較適宜,無雨且風力較小,易保持清潔度。
空氣污染指數:良,氣象條件有利於空氣污染物擴散。
</string><string>10℃/17℃</string><string>4月19日 多雲轉小雨</string><string>南風小於3級</string><string>1.gif</string><string>7.gif</string><string>10℃/16℃</string><string>4月20日 小雨轉多雲</string><string>東北風轉北風小於3級</string><string>7.gif</string><string>1.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(33): "lr_xml_get_values" succeeded, 1 match processed
Action.c(46): Notify: Transaction "獲取城市天氣預報" ended with a "Pass" status (Duration: 0.2918 Wasted Time: 0.0522).
Ending action Action.
Ending iteration 1.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM