精通軟件性能測試與LoadRunner最佳實戰 連載十四
3.13.3 Web Services腳本
完成Web Services服務端和客戶端兩個小程序的編寫、編譯、執行后,您需要確保服務端程序是開啟的,關閉已運行的客戶端小程序。
接下來,您啟動LoadRunner 11.0,選擇“Web Services”協議,如圖13-122所示。
圖13-121 “樣例演示-客戶端”對話框 圖13-122 “New Virtual User”對話框
單擊【Create】按鈕,創建“Web Services”協議,在LoadRunner 腳本編輯環境單擊錄制紅色按鈕,在彈出的“Recording Wizard”對話框中單擊【Import】按鈕,在彈出的“Import Service”對話框中選擇“URL”選項,在文本框中輸入“http://localhost:5678/wsdl/IMyHello”,單擊【Import】按鈕,如圖13-123所示。
圖13-123 “Recording Wizard-Add Services頁”對話框
稍等片刻,將出現圖13-124所示界面信息,單擊【下一步(N)>】按鈕。
圖13-124 “Recording Wizard”對話框
在圖13-125所示對話框中輸入客戶端路徑相關信息“C:\mytest\client\Client.exe”,工作目錄為“C:\mytest\client”,(如果您的應用是基于B/S的,則選擇“Record default web browser”選項并輸入相應URL)單擊【完成】按鈕。
此時將會在輸入框下方出現“歡迎于涌同學!”字符串,如圖13-126所示。
圖13-125 “Recording Wizard-Specify Application頁”對話框 圖13-126 “樣例演示-客戶端”對話框
此時將會產生如下代碼:
Action() { web_add_header("Content-Type", "text/xml"); web_add_header("SOAPAction", "\"urn:MyHelloIntf-IMyHello#Welcome\""); web_add_header("User-Agent", "Borland SOAP 1.2"); soap_request("StepName=Welcome", "URL=http://localhost:5678/soap/IMyHello", "SOAPEnvelope=<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\"?" "><SOAP-ENV:Envelope xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/" "encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http:/" "/schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body SOAP-ENV" ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><NS1" ":Welcome xmlns:NS1=\"urn:MyHelloIntf-IMyHello\"><name xsi:type=\"xsd" ":string\">于涌</name></NS1:Welcome></SOAP-ENV:Body></SOAP-ENV:Envelope>", "Snapshot=t1.inf", "ResponseParam=response", LAST); return 0; } |
我們最關心的內容應該是Web Services服務接口返回的內容,所以需要對腳本進行完善,腳本信息如下,黑色字體為新添加內容。
Action() { web_add_header("Content-Type", "text/xml"); web_add_header("SOAPAction", "\"urn:MyHelloIntf-IMyHello#Welcome\""); web_add_header("User-Agent", "Borland SOAP 1.2"); soap_request("StepName=Welcome", "URL=http://localhost:5678/soap/IMyHello", "SOAPEnvelope=<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\"?" "><SOAP-ENV:Envelope xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/" "encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http:/" "/schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body SOAP-ENV" ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><NS1" ":Welcome xmlns:NS1=\"urn:MyHelloIntf-IMyHello\"><name xsi:type=\"xsd" ":string\">于涌</name></NS1:Welcome></SOAP-ENV:Body></SOAP-ENV:Envelope>", "Snapshot=t1.inf", "ResponseParam=response", LAST); lr_save_string(lr_eval_string("{response}"), "XML_Input_Param"); lr_xml_get_values("XML={XML_Input_Param}", "ValueParam=OutputParam", "Query=/*/*/*/return", LAST ); lr_output_message(lr_eval_string("返回結果 = {OutputParam}")); return 0; } |
腳本的執行信息如下:
Virtual User Script started at : 2012-11-11 14:56:30 Starting action vuser_init. Ending action vuser_init. Running Vuser... Starting iteration 1. Starting action Action. Action.c(4): Warning -26593: The header being added may cause unpredictable results when applied to all ensuing URLs. It is added anyway [MsgId: MWAR-26593] Action.c(4): web_add_header("Content-Type") highest severity level was "warning" [MsgId: MMSG-26391] Action.c(6): web_add_header("SOAPAction") was successful [MsgId: MMSG-26392] Action.c(8): web_add_header("User-Agent") was successful [MsgId: MMSG-26392] Action.c(10): SOAP request "Welcome" started Action.c(10): SOAP request "Welcome" was successful Action.c(26): "lr_xml_get_values" succeeded, 1 match processed Action.c(31): 返回結果 = 歡迎于涌同學! Ending action Action. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated. |
也許有的讀者會說:“我想先看看服務端返回的信息,這可不可以呢?”。“當然沒問題”,您只需要在腳本中加入“lr_output_message("%s",lr_eval_string("{response}")); ”(lr_xml_get_values()函數在前面已經介紹過,其在進行該協議類型腳本關聯時非常重要,請讀者朋友務必掌握),執行腳本時就會打印出響應的相關數據信息,如圖13-127所示。
圖13-127 WebServices相關腳本及執行結果
為了讓大家對返回的內容看得更加清楚,這里我將其拷貝出來,放置到my.xml文件中,其內容如圖13-128所示。
圖13-128 服務返回的XML相關信息
從圖13-128您會看到有一串亂碼,憑直覺這應該是“歡迎于涌同學!”,那么直覺對不對呢?您可以在腳本中加入以下兩行代碼,如圖13-129所示。
圖13-129 轉碼及輸出函數
其輸出結果果真為“歡迎于涌同學!”。圖13-129 中使用“lr_convert_string_encoding()”的函數就是將UTF8編碼轉碼為系統語言編碼的函數,經過轉碼后信息顯示正確了。也許有的讀者會問:“系統返回的xml響應信息有些顯得十分混亂,有沒有什么方法直接能看出其層次關系呢?”您可以借助樹視圖,單擊“Tree”按鈕,將出現圖13-130所示界面信息,是不是一目了然了呢!這也方便了您應用lr_xml_get_values()函數時填寫“Query”部分內容。
圖13-130 WebServices響應結果樹視圖
【重要提示】
(1)從上面Web Services程序編寫和腳本應用中,相信大家對該協議的應用已經有了非常清晰的認識,我們給大家演示的是基于C/S的小程序,在實際工作中您測試的應用可能是基于B/S的,它們在實際應用上并沒有大的區別,所以請讀者朋友們舉一反三。
(2)也許您也會像我一樣,在機器上安裝有殺毒軟件,如果您應用本書此客戶端和服務器端小程序時,有可能您的殺毒軟件會對其產生干擾,如:將小程序自動刪除、運行小程序沒反應等情況,此時應暫時關閉殺毒軟件,確保小應用可以正常運行。
(未完待續)
版權聲明:51Testing軟件測試網及相關內容提供者擁有51testing.com內容的全部版權,未經明確的書面許可,任何人或單位不得對本網站內容復制、轉載或進行鏡像。51testing軟件測試網歡迎與業內同行進行有益的合作和交流,如果有任何有關內容方面的合作事宜,請聯系我們。
相關鏈接:
posted on 2013-07-25 10:50 順其自然EVO 閱讀(775) 評論(0) 編輯 收藏 所屬分類: loadrunner