loadrunner提高篇 - 插入檢查點與關聯函數


插入檢查點                                  

    靠LR自動生成的腳本是不夠的,很難達到業務要求,因此需要對錄制完的腳本進行完善,使其能達到業務模擬的要求 ,這樣盡可能地使虛擬用戶模擬時更接近用戶的實際使用。

  在進行壓力測試時,經常會有頁面間數據傳遞的操作。如果在測試過程中傳遞數據的次數逐漸增多,頁面就有可能發生傳遞混亂,或者客戶端與服務器端數據傳輸被中斷、傳輸過程中產生了錯誤的數據等情況。為了判斷數據傳遞的正確性,LR提供了插入檢查點的方法。之前在入門篇的博客中有提到插入檢查點的原因,這里就不再細說了,大概提一下,是因為當事務的結束狀態為成功時,並不能說明事務真的做成功了,比如用錯的賬號密碼去跑腳本,事務狀態仍被置為pass,所以你們懂的了。

  插入檢查 點的原理是通過檢查點函數將返回值的結果反映在controller的狀態面板上和analysis統計結果中。是基於LR中很多的API函數的返回值會改變腳本的運行結果。比如檢查點函數web_find,如果它檢查到的結果為空,它的返回值就為LR_FAIL,這樣整個結果置為FAIL;反之,返回值則為LR_PASS,整個結果置為PASS。

  在工作原理上是在VuGen中插入Text/Image檢查點。VuGen中包含Tree view和script view兩種視圖模式,一般情況下是使用script view模式,但在插入檢查點時,一般都是在tree view模式下插入的,這樣可以保證插入檢查點的位置正確。插入檢查點有text check和image check兩種,選擇的檢查點函數也有兩個,分別為web_find和web_reg_find,下面先介紹一下這2個函數,最后在關聯函數再附上詳細代碼。

一、web_find檢查點函數

  檢查點函數的參數格式:

 web_find("web_find",//檢查點步驟名稱
        "RightOf=go to",//定義查找字符串右邊界
        "LeftOf=page",//定義查找字符串左邊界
        "What=home",//定義檢查字符串內容
        LAST);

  該函數的作用是在頁面中查找相應的內容,有幾點要注意的問題:

1)只能對基於HTML模式錄制的腳本進行查找

2)該函數必須在頁面內容顯示出來以后才能進行查找,所以該函數必須寫在查找內容所在頁面的后面

3)必須啟用內容檢查選項,在run-tim setting->preferences頁面,把enable image and text check復選框選中,否則不執行該查找函數

4)在VB和java語法中不支持該函數

5)執行效率低,且不返回查找結果情況。例如要查看有多少個虛擬用戶登錄成功的情況,這個函數無法做到,必須進一步操作才能實現。

  插入步驟:將視圖模式設置為tree view->選擇在插入檢查點的位置,右鍵選擇insert after->依次選擇web checks/text check->在彈出的對話框的specification選項卡中設置檢查參數信息(主要有3個屬性)->然后切換到general選項卡,在輸入該操作的步驟名稱(即命名)。

  上面插入步驟中所講到的3個屬性:

a.search for:設置要檢查的字符串,也可以單擊ABC按鈕對檢查的內容進行參數化。

b.right of:設置右邊界值,也可以為空            c.left of:設置左邊界值,也可以為空

二、web_reg_find檢查點函數

  該函數是在緩存中查找相應的內容,是一個注冊函數。必須寫在查找內容的請求之前,一般情況下都會寫在以下6個函數之前 :web_custom_request()、web_image()、web_link、web_submit_data()、web_submit_form()、web_url()。

  與web_find函數相比:

1)web_find只是一個普通函數,而web_reg_find是一個注冊函數

2)web_find函數使用時必須開啟內容檢查選項,而web_reg_find函數沒有此限制

3)web_find函數錄制時只能基於HTML模式錄制的腳本中,而web_reg_find函數沒有此限制

4)web_find函數是在返回的頁面中進行內容查找,而web_reg_find函數是在緩存中進行查找

5)web_reg_find函數在執行效率上要比web_find函數高

  插入步驟:將視圖模式設置為tree view->選擇在插入檢查點的位置,右鍵選擇insert after或insert before(如圖1所示)->依次選擇services/web_reg_find->在彈出的對話框中設置檢查參數信息(如圖2所示)。

圖1(選擇要插入檢查點位置)

圖2(檢查點屬性設置說明)

  如何確定查找范圍?答案如圖3所示,以登錄成功的頁面為例,在該選項卡中可以看到錄制的過程LR所抓到客戶端向服務器發送的請求和服務器向客戶端返回的請求,而在response body中就可以看到待查找的用戶名(test1),所以檢查范圍就應設置為body。其實一般在實際測試過程中待查找的內容都會在body中的。

圖3(查找待檢查的內容)

  檢查點函數的參數格式:

web_reg_find("Search=Body",//定義查找范圍
        "SaveCount=num",//定義查找計數變量名
        "Text=home",//定義要查找的內容
        LAST);

    web_submit_data("login.pl",
        "Action=http://127.0.0.1:1080/WebTours/login.pl",
        "Method=POST",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
        "Snapshot=t15.inf",
        "Mode=HTTP",
        ITEMDATA,
        "Name=userSession", "Value=120746.88035642zcVAQVHpzfiDDDDDDHczfpfAfQf", ENDITEM,
        "Name=username", "Value=test1", ENDITEM,
        "Name=password", "Value=test", ENDITEM,
        "Name=JSFormSubmit", "Value=off", ENDITEM,
        "Name=login.x", "Value=52", ENDITEM,
        "Name=login.y", "Value=11", ENDITEM,
        LAST);

    if(atoi(lr_eval_string("{num}"))>=1{
        lr_end_transaction("login",LR_PASS);//login是錄制腳本時插入的事務,這里自然是結束事務。當事務通過時,結束狀態置為LR_PASS
    }
    else{
        lr_end_transaction("login",LR_FAIL);//否則,結束狀態置為LR_FAIL
    }
web_reg_find

三、web_image_check檢查點函數

  插入步驟:將視圖模式設置為tree view->選擇在插入檢查點的位置,右鍵選擇insert after或insert before->依次選擇web checks/image check->在彈出的對話框的specification選項卡中設置檢查參數信息(如圖4所示)->然后切換到general選項卡,在輸入該操作的步驟名稱(即命名)。

  如果web窗體中包含有JS腳本,那么在tree view中顯示可能會有問題,這時需要進行設置(tool->general options->correlation選項卡->勾選enable scripting and java apples on snapshots viewer)

圖4(image檢查點屬性設置)

關聯函數                                      

一、關聯的原理

1、什么情況下需要關聯?

如果錄制腳本過程中,服務器會返回一個動態的、變化的值給客戶端時,那么就必須對腳本進行關聯,否則事務不可能正常地完成。

2、LR使用關聯函數進行函數時是如何獲得這些動態數據的?即LR的關聯原理是什么?

對於這些動態的值,如果想直接獲得是不可能的,因為它是變化的,所以不可能直接獲得每次回放時現成的值。這樣就必須使用其他的方法來獲得這串變化的值,LR關聯則是通過左右邊界值來查找服務器返回給客戶端的值,所以關聯最難的是如何查找待關聯值的左右邊界。

二、3種關聯技術

注:不知道什么原因用HTML模式錄制后在tree view視圖下沒有顯示想要插入檢查點的的url(比如登錄),嘗試過直接在代碼插入檢查點函數web_find,運行也不成功,所以下面的代碼展示用的都是URL錄制模式錄的腳本。

1、錄制中關聯

  VuGen內自動關聯引擎,可以自動找到需要關聯的值,並且自動關聯函數建立關聯。

1)建立規則。如錄制前已經知道關聯規則,那么可以先建立規則,再進行錄制,這樣在錄制過程中會自動關聯。關聯規則最重要的是指定兩個邊界,即被關聯量的左邊界和右邊界。

a.內建關聯規則。在錄制前可以在 tools->recording options->http properties->correlation 啟動需要的規則。

b.用戶自定義關聯規則。如內建規則不能滿足要求,可自定義,如圖5所示:

圖5(新建關聯規則)

c.圖6所示是規則測試功能,即對已定義好的規則進行測試,測試規則的匹配是否正確:

圖6(規則測試對話框)

2)規則使用。錄制前必須啟用自動關聯,如圖5所示,勾選enable correlation during recording后,在錄制過程中,當VuGen檢查到符合關聯規則的數據時,會依照設定建立關聯,即自動產生關聯函數web_reg_param,完整代碼示例如下:

Action()
{
    //自動生成的存儲參數長度的函數
    web_set_max_html_param_len("1024");

    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t2.inf", 
        "Mode=HTTP", 
        LAST);

    web_url("header.html", 
        "URL=http://127.0.0.1:1080/WebTours/header.html", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/", 
        "Snapshot=t3.inf", 
        "Mode=HTTP", 
        LAST);

    web_concurrent_start(NULL);

    web_url("hp_logo.png", 
        "URL=http://127.0.0.1:1080/WebTours/images/hp_logo.png", 
        "Resource=1", 
        "RecContentType=image/png", 
        "Referer=http://127.0.0.1:1080/WebTours/header.html", 
        "Snapshot=t4.inf", 
        LAST);

    web_url("webtours.png", 
        "URL=http://127.0.0.1:1080/WebTours/images/webtours.png", 
        "Resource=1", 
        "RecContentType=image/png", 
        "Referer=http://127.0.0.1:1080/WebTours/header.html", 
        "Snapshot=t5.inf", 
        LAST);

    web_concurrent_end(NULL);

    web_url("welcome.pl", 
        "URL=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/", 
        "Snapshot=t9.inf", 
        "Mode=HTTP", 
        LAST);

    web_url("home.html", 
        "URL=http://127.0.0.1:1080/WebTours/home.html", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true", 
        "Snapshot=t12.inf", 
        "Mode=HTTP", 
        LAST);

    //下面的三行注釋與web_reg_save_param關聯函數是錄制中自動生成的

    /* Registering parameter(s) from source task id 53
    // {BL2} = "120705.245200486zcAiHHzpAfDHcfcpzfzD"
    // */

    web_reg_save_param("BL2", //BL是在建立關聯規則時定義的變量參數前綴,BL2則是自動創建的變量參數
        "LB/IC=userSession value=", //LB是左邊界值,IC是否匹配大小寫
        "RB/IC=>", //RB是右邊界值
        "Ord=1", //所匹配的內容出現的位置,默認為1(指查找待匹配的內容第一次出現的情況)
        "Search=Body",//查找范圍 
        "RelFrameId=1", //幀ID參數,可以通過幀ID號指定訪問不同的幀
        LAST);

    web_url("nav.pl", 
        "URL=http://127.0.0.1:1080/WebTours/nav.pl?in=home", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true", 
        "Snapshot=t14.inf", 
        "Mode=HTTP", 
        LAST);

    web_url("mer_login.gif", 
        "URL=http://127.0.0.1:1080/WebTours/images/mer_login.gif", 
        "Resource=1", 
        "RecContentType=image/gif", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home", 
        "Snapshot=t15.inf", 
        LAST);

    web_concurrent_start(NULL);

    web_url("8afc2fe48db9060fe1bdda2089e1d950.png", 
        "URL=http://act.cmcmcdn.com/upload/201507/8afc2fe48db9060fe1bdda2089e1d950.png", 
        "Resource=1", 
        "RecContentType=image/png", 
        "Referer=http://127.0.0.1:1080/WebTours/", 
        "Snapshot=t16.inf", 
        LAST);

    web_url("3b491068507d8f85ea7b35d756da7215.png", 
        "URL=http://act.cmcmcdn.com/upload/201507/3b491068507d8f85ea7b35d756da7215.png", 
        "Resource=1", 
        "RecContentType=image/png", 
        "Referer=http://127.0.0.1:1080/WebTours/", 
        "Snapshot=t17.inf", 
        LAST);

    web_concurrent_end(NULL);

    lr_start_transaction("login");

    lr_think_time(13);

    web_submit_data("login.pl", 
        "Action=http://127.0.0.1:1080/WebTours/login.pl", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home", 
        "Snapshot=t18.inf", 
        "Mode=HTTP", 
        ITEMDATA, 
        "Name=userSession", "Value={BL2}", ENDITEM, //因關聯函數創建了變量參數BL2,所以這里value的值自動就用變量參數BL2代替了
        "Name=username", "Value=test1", ENDITEM, 
        "Name=password", "Value=test1", ENDITEM,//password的值如果是正確的,返回 "we find the string!";如果是錯誤的,則返回"sorry,don't find the string!"
        "Name=JSFormSubmit", "Value=off", ENDITEM, 
        "Name=login.x", "Value=0", ENDITEM, 
        "Name=login.y", "Value=0", ENDITEM, 
        LAST);

    web_concurrent_start(NULL);

    web_url("nav.pl_2", 
        "URL=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/login.pl", 
        "Snapshot=t19.inf", 
        "Mode=HTTP", 
        LAST);

    //插入檢查點
    web_reg_find("Search=Body",//檢查點插入的范圍
        "SaveCount=username",//用變量username統計查找內容test1
        "Text/DIG=test1",//查找的內容
        LAST);

    web_url("login.pl_2", 
        "URL=http://127.0.0.1:1080/WebTours/login.pl?intro=true", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/login.pl", 
        "Snapshot=t20.inf", 
        "Mode=HTTP", 
        LAST);

    web_concurrent_end(NULL);

    web_concurrent_start(NULL);

    web_url("flights.gif", 
        "URL=http://127.0.0.1:1080/WebTours/images/flights.gif", 
        "Resource=1", 
        "RecContentType=image/gif", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home", 
        "Snapshot=t21.inf", 
        LAST);

    web_url("itinerary.gif", 
        "URL=http://127.0.0.1:1080/WebTours/images/itinerary.gif", 
        "Resource=1", 
        "RecContentType=image/gif", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home", 
        "Snapshot=t22.inf", 
        LAST);

    web_url("in_home.gif", 
        "URL=http://127.0.0.1:1080/WebTours/images/in_home.gif", 
        "Resource=1", 
        "RecContentType=image/gif", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home", 
        "Snapshot=t23.inf", 
        LAST);

    web_url("signoff.gif", 
        "URL=http://127.0.0.1:1080/WebTours/images/signoff.gif", 
        "Resource=1", 
        "RecContentType=image/gif", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home", 
        "Snapshot=t24.inf", 
        LAST);

    web_concurrent_end(NULL);

    //判斷事務結束狀態

    if (atoi(lr_eval_string("{username}"))>0){//判斷變量username(即查找內容的次數)是否大於或等於1
        lr_output_message("we find the string!");//如果大於0,則返回we find the string!
    }

    else{
        lr_output_message("sorry,don't find the string!");//否則,返回sorry,don't find the string!
    }

    lr_end_transaction("login",LR_AUTO);//下面這個事務結束狀態是LR_AUTO

    return 0;
}
url錄制中關聯

2、錄制后關聯

  當錄制前內建關聯規則和新建規則都不能滿足需要時,或者不知道哪個地方需要關聯時,只能采取錄制后進行關聯。錄制后關聯與內建關聯是有區別的,錄制后關聯是在執行腳本后才會建立,也就是說,當錄制完腳本后,腳本至少要執行一次,錄制后關聯才會產生效果。錄制后會嘗試找到錄制與執行時服務器響應的差異部分,找到需要關聯的數據,並建立關聯。

  腳本執行后,vuser->scan script for correlations,掃描后,選擇需要關聯的數據,單擊correlate,這時被關聯的數據前面會多出一個綠色的勾,即表示關聯創建完成了,如圖7所示:

圖7(關聯結果) 

  關聯完成后也是會自動生成web_reg_param關聯函數,完整代碼示例如下(期間遇到報錯error-35061,在代碼中有說明解決方法):

Action()
{

    web_url("WebTours",
        "URL=http://127.0.0.1:1080/WebTours",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t1.inf",
        "Mode=HTTP",
        LAST);

    web_url("header.html",
        "URL=http://127.0.0.1:1080/WebTours/header.html",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t2.inf",
        "Mode=HTTP",
        LAST);

    web_url("webtours.png",
        "URL=http://127.0.0.1:1080/WebTours/images/webtours.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/header.html",
        "Snapshot=t4.inf",
        LAST);

    web_url("hp_logo.png",
        "URL=http://127.0.0.1:1080/WebTours/images/hp_logo.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/header.html",
        "Snapshot=t5.inf",
        LAST);

    web_url("welcome.pl",
        "URL=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t6.inf",
        "Mode=HTTP",
        LAST);

    web_concurrent_start(NULL);

    web_url("home.html",
        "URL=http://127.0.0.1:1080/WebTours/home.html",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
        "Snapshot=t7.inf",
        "Mode=HTTP",
        LAST);

    //下面的一行注釋和web_reg_save_param_ex關聯函數是錄制后關聯所自動創建的

//Correlation comment - Do not change!Original value='120705.370676387zcAiHicpfAtVzzzHDHcfcpzQfAcf' Name ='CorrelationParameter_1'

    web_reg_save_param_ex(//這個關聯函數與web_reg_save_param函數實質差別不大
        "ParamName=CorrelationParameter_1",//變量名稱,表示將緩存區中查找到的內容保存到這個變量中
        "LB=userSession value=",//左邊界值
        "RB=>\n<table border",//右邊界值
        SEARCH_FILTERS,//指定緩沖區中需要查找的字符串
        "Scope=Body",
        //"RequestUrl=*/nav.pl*",     /*因為報錯error-35061,所以要注釋掉自動生成的關聯函數中的這一行*/
        LAST);//結束參數標志

    web_url("nav.pl",
        "URL=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
        "Snapshot=t8.inf",
        "Mode=HTTP",
        LAST);

    web_concurrent_end(NULL);

    web_url("mer_login.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/mer_login.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
        "Snapshot=t9.inf",
        LAST);

    web_concurrent_start(NULL);

    web_url("8afc2fe48db9060fe1bdda2089e1d950.png",
        "URL=http://act.cmcmcdn.com/upload/201507/8afc2fe48db9060fe1bdda2089e1d950.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t11.inf",
        LAST);

    web_url("3b491068507d8f85ea7b35d756da7215.png",
        "URL=http://act.cmcmcdn.com/upload/201507/3b491068507d8f85ea7b35d756da7215.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t12.inf",
        LAST);

    web_concurrent_end(NULL);

    lr_start_transaction("登錄");

    lr_think_time(6);

    web_submit_data("login.pl",
        "Action=http://127.0.0.1:1080/WebTours/login.pl",
        "Method=POST",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
        "Snapshot=t15.inf",
        "Mode=HTTP",
        ITEMDATA,
        //用CorrelationParameter_1變量代替常量,即value的值要改為變量值
        "Name=userSession", "Value={CorrelationParameter_1}", ENDITEM,
        "Name=username", "Value=test1", ENDITEM,
        "Name=password", "Value=test1", ENDITEM,//password的值如果是正確的,返回 "PASS";如果是錯誤的,則返回"FAIL"
        "Name=JSFormSubmit", "Value=off", ENDITEM,
        "Name=login.x", "Value=0", ENDITEM,
        "Name=login.y", "Value=0", ENDITEM,
        LAST);

    web_concurrent_start(NULL);

//在該位置插入檢查點
    web_reg_find("Text=test1",
        "SaveCount=num",//用變量num統計查找內容test1
        "Search=Body",
        LAST);

    web_url("login.pl_2",
        "URL=http://127.0.0.1:1080/WebTours/login.pl?intro=true",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/login.pl",
        "Snapshot=t16.inf",
        "Mode=HTTP",
        LAST);

    web_url("nav.pl_2",
        "URL=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/login.pl",
        "Snapshot=t17.inf",
        "Mode=HTTP",
        LAST);

    web_concurrent_end(NULL);

    web_concurrent_start(NULL);

    web_url("flights.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/flights.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t18.inf",
        LAST);

    web_url("itinerary.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/itinerary.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t19.inf",
        LAST);

    web_url("signoff.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/signoff.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t20.inf",
        LAST);

    web_url("in_home.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/in_home.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t21.inf",
        LAST);

    web_concurrent_end(NULL);

//判斷事務結束狀態
    if(atoi(lr_eval_string("{num}"))>=1){//判斷變量num(即查找內容的次數)是否大於或等於1
        lr_end_transaction("登錄", LR_PASS);//如果是,則說明找到了,即登錄成功,返回LR_PASS
    }
    else{
        lr_end_transaction("登錄", LR_FAIL);//否則,返回LR_FAIL
    }

return 0;
}
url錄制后關聯

3、手動關聯

  手動關聯與前面所提到的兩種自動關聯不同,但原理是一樣的,需要先找到需要關聯的量,然后使用LR提供的關聯函數進行關聯。這方法是用於上面兩種方法都不能解決的情況,步驟如下:

1)錄制兩份腳本,保證事務流程和使用的數據相同;

2)使用WinDiff工具比較兩份腳本,找到需要關聯的數據。(WinDiff是LR自帶的文件比較工具)tools->compare with vuser,彈出的對話框中選擇要和當前腳本進行比較的腳本,WinDiff會顯示有差異的地方,每個差異的地方都可能是需要關聯的地方,但是lr_think_time的差異部分是不需要關聯的;

3)找到左邊界和右邊界字符串;

4)使用web_reg_save_param函數手動建立關聯。首先找到關聯函數插入的位置,選擇vuser->run-time settings->general->log->extended log,將下面所有選項都選中。在replay log中找到windiff中比較不同的字符串,雙擊后,那行代碼前面就是為要插入關聯函數的位置,如圖8所示:

圖8(關聯函數插入位置)

5)將腳本中的該關聯數據以參數取代。當使用web_reg_save_param建立參數后,接下來就是用參數去取代腳本中的常量,如圖9所示:

  完整代碼示例如下(期間遇到報錯error-27216及執行失敗,代碼中有說明解決方法):

Action()
{
    web_url("WebTours",
        "URL=http://127.0.0.1:1080/WebTours",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t2.inf",
        "Mode=HTTP",
        LAST);

    web_url("header.html",
        "URL=http://127.0.0.1:1080/WebTours/header.html",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t3.inf",
        "Mode=HTTP",
        LAST);

    web_url("hp_logo.png",
        "URL=http://127.0.0.1:1080/WebTours/images/hp_logo.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/header.html",
        "Snapshot=t5.inf",
        LAST);

    web_url("welcome.pl",
        "URL=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t6.inf",
        "Mode=HTTP",
        LAST);

    web_url("webtours.png",
        "URL=http://127.0.0.1:1080/WebTours/images/webtours.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/header.html",
        "Snapshot=t7.inf",
        LAST);

    web_url("home.html",
        "URL=http://127.0.0.1:1080/WebTours/home.html",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
        "Snapshot=t11.inf",
        "Mode=HTTP",
        LAST);

    //下面是手動寫的關聯函數,但是運行報錯(error-27216)
    //web_reg_save_param("BL",
    //    "LB=userSession value=",
    //    "RB=>",
    //    "Search=Body",
    //    LAST);

    //所以嘗試在樹模型下插入關聯函數,結果運行成功了,下面是用插入方式插入的關聯函數
        web_reg_save_param("BL",//創建變量BL
        "LB=userSession value=",
        "RB=>",
        "Search=Body",
        LAST);

    web_url("nav.pl",
        "URL=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true",
        "Snapshot=t12.inf",
        "Mode=HTTP",
        LAST);

    web_url("mer_login.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/mer_login.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
        "Snapshot=t13.inf",
        LAST);

    web_concurrent_start(NULL);

    web_url("8afc2fe48db9060fe1bdda2089e1d950.png",
        "URL=http://act.cmcmcdn.com/upload/201507/8afc2fe48db9060fe1bdda2089e1d950.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t18.inf",
        LAST);

    web_url("3b491068507d8f85ea7b35d756da7215.png",
        "URL=http://act.cmcmcdn.com/upload/201507/3b491068507d8f85ea7b35d756da7215.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://127.0.0.1:1080/WebTours/",
        "Snapshot=t19.inf",
        LAST);

    web_concurrent_end(NULL);

    lr_start_transaction("登錄");

    lr_think_time(15);

    web_submit_data("login.pl",
        "Action=http://127.0.0.1:1080/WebTours/login.pl",
        "Method=POST",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home",
        "Snapshot=t20.inf",
        "Mode=HTTP",
        ITEMDATA,
        //用BL變量代替常量,即value的值要改為變量值
        //如果直接輸入session的參數變量值時執行失敗的話,可以嘗試右鍵replace with a parameter(我就有遇到這個情況)
        "Name=userSession", "Value={BL}", ENDITEM,
        "Name=username", "Value=test1", ENDITEM,
        "Name=password", "Value=test1", ENDITEM,//password的值如果是正確的,返回 "PASS";如果是錯誤的,則返回"FAIL"
        "Name=JSFormSubmit", "Value=off", ENDITEM,
        "Name=login.x", "Value=48", ENDITEM,
        "Name=login.y", "Value=13", ENDITEM,
        LAST);

    web_concurrent_start(NULL);

    web_url("nav.pl_2",
        "URL=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/login.pl",
        "Snapshot=t21.inf",
        "Mode=HTTP",
        LAST);

    //在樹模型下插入的檢查點
    web_reg_find("Text=test1",
        "SaveCount=num",//用變量num統計查找內容test1
        "Search=Body",
        LAST);

    web_url("login.pl_2",
        "URL=http://127.0.0.1:1080/WebTours/login.pl?intro=true",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/WebTours/login.pl",
        "Snapshot=t22.inf",
        "Mode=HTTP",
        LAST);

    web_concurrent_end(NULL);

    web_concurrent_start(NULL);

    web_url("flights.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/flights.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t23.inf",
        LAST);

    web_url("itinerary.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/itinerary.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t24.inf",
        LAST);

    web_url("signoff.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/signoff.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t25.inf",
        LAST);

    web_url("in_home.gif",
        "URL=http://127.0.0.1:1080/WebTours/images/in_home.gif",
        "Resource=1",
        "RecContentType=image/gif",
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?page=menu&in=home",
        "Snapshot=t26.inf",
        LAST);

    web_concurrent_end(NULL);

    //判斷事務結束狀態
    if(atoi(lr_eval_string("{num}"))>=1){//判斷變量Num(即查找內容的次數)是否大於或等於1
        lr_end_transaction("登錄", LR_PASS);//如果是,則返回LR_PASS
    }
    else{
        lr_end_transaction("登錄", LR_FAIL);//否則,返回LR_FAIL
    }
    
return 0;
}

url手動關聯
url手動關聯

備注:文字講解來自《深入性能測試--LoadRunner性能測試、流程、監控、調優全程實戰剖析》(黃文高、何月順編著)一書,我是新手,參照此教程做了下實踐,順便將學到的東西寫下來。


免責聲明!

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



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