性能測試學習08_lr(web service協議)

lr12中web services協議腳本開發應用web

使用web_service_call,soap_request、web_custom_request函數完成腳本,並加上if判斷json

1、web_service_call函數使用服務器

  1.web_service_call腳本編寫操做步驟(步驟與圖片的順序依次對應)app

    1)新建web services腳本;函數

    2)點擊【SOATools】,點擊add service call;優化

    3)service選擇【import service】,會彈框顯示【import service】,輸入服務連接地址;網站

    4)operation選擇【getWeatherbyCityName】;編碼

    5)點擊【theCityName】,填寫value值,好比深圳;spa

    6)點擊【getWeatherbyCityNameResult】,選擇保存返回結果;3d

    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;

 結果如圖所示:

2、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;
}

2、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;
}
相關文章
相關標籤/搜索