Tuesday 25 February 2014

Using Vuser Functions in GUI Vuser Scripts

This section lists the Vuser functions that you can use to enhance your GUI user scripts.

declare_rendezvous: Declares a rendezvous.

declare_transaction :Declares a transaction.

end_transaction :Marks the end of a transaction for performance analysis.

error_message: Sends an error message to the Controller.

get_host_name: Returns the name of a load generator.

get_master_host_name :Returns the name of the Controller load generator.

lr_whoami :Returns information about the Vuser executing the script.

output_message: Sends a message to the Controller.

rendezvous: Sets a rendezvous point in a Vuser script.

start_transaction: Marks the beginning of a transaction for performance analysis.

user_data_point: Records a user-defined data sample.

lr_eval_string in load Runner

To correlate statements for protocols that do not have specific functions, you can use the C Vuser
correlation functions. These functions can be used for all C-type Vusers, to save a string to a
parameter and retrieve it when required.

lr_eval_string :Replaces all occurrences of a parameter with its current value.
lr_save_string :Saves a null-terminated string to a parameter.
lr_save_var :Saves a variable length string to a parameter.

Using lr_eval_string:

In the following example, lr_eval_string replaces the parameter row_cnt with its current value. This value is sent to the Output window using lr_output_message.

lrd_stmt(Csr1, "select count(*) from employee", -1, 1 /*Deferred*/,
...);
lrd_bind_col(Csr1, 1, =;COUNT_D1, 0, 0);
lrd_exec(Csr1, 0, 0, 0, 0, 0);
lrd_save_col(Csr1, 1, 1, 0, "row_cnt");
lrd_fetch(Csr1, 1, 1, 0, PrintRow2, 0);
lr_output_message("value: %s" , lr_eval_string("The row count is:
"));

Removing Zero from the Date in VuGen LoadRunner

Enter start date and end date and date should not include zero before actual value of month and day.

for example: Invalid date format: 04/27/2011

                       Valid date format: 4/27/2011

Script:
//variable declaration
char month[10], day[10], year[10], startDate[10], endDate[10];
int nonZeroDay, nonZeroMonth;

/*if you want to run script for multiple interactions then you have to nullify
the concatenated string before starting new iteration otherwise it
will keep the old value*/
strcpy(startDate, "");
strcpy(endDate, "");

//original date which contain zero in the date
lr_save_datetime("Today’s Date is: %m/%d/%Y", DATE_NOW, "normalDate");
lr_output_message(lr_eval_string("{normalDate}"));

//for generating non zero start date

//to assign the current date parts to a parameter
lr_save_datetime("%m", DATE_NOW, "month");
lr_save_datetime("%d", DATE_NOW, "day");
lr_save_datetime("%Y", DATE_NOW, "year");

//to remove zero from the string. e.g.getting '4' from '04'

nonZeroMonth = atoi(lr_eval_string("{month}"));
lr_save_int(nonZeroMonth, "month");

nonZeroDay = atoi(lr_eval_string("{day}"));
lr_save_int(nonZeroDay, "day");

//creating a date string using concatenation of nonzero date parts and separator '/'
strcat(startDate, lr_eval_string("{month}"));
strcat(startDate, "/");
strcat(startDate, lr_eval_string("{day}"));
strcat(startDate, "/");
strcat(startDate, lr_eval_string("{year}"));

lr_save_string(startDate, "p_StartDate");
lr_output_message("Start Date is: %s", lr_eval_string("{p_StartDate}"));

//for generating non zero end date by incrementing date by one day offset
lr_save_datetime("%m", DATE_NOW + ONE_DAY, "month");
lr_save_datetime("%d", DATE_NOW + ONE_DAY, "day");

lr_save_datetime("%Y", DATE_NOW + ONE_DAY, "year");
nonZeroMonth = atoi(lr_eval_string("{month}"));
lr_save_int(nonZeroMonth, "month");

nonZeroDay = atoi(lr_eval_string("{day}"));
lr_save_int(nonZeroDay, "day");

strcat(endDate, lr_eval_string("{month}"));
strcat(endDate, "/");
strcat(endDate, lr_eval_string("{day}"));
strcat(endDate, "/");
strcat(endDate, lr_eval_string("{year}"));

lr_save_string(endDate, "p_endDate");
lr_output_message("End Date is: %s", lr_eval_string("{p_endDate}"));


Output:


Today’s Date is: 04/27/2011
Start Date is: 4/27/2011
End Date is: 4/28/2011

Scripting for Microsoft ASP.NET VIEWSTATE in VuGen LoadRunner

ASP.NET web applications maintain state of data by passing hidden _VIEWSTATE field encoded using base64 algorithm. The state of the page is restored based on the VIEWSTATE. If on the client side, the value of textbox has been changed, VIEWSTATE will now have the new value. (Obviously, this stage does not happen when the page is requested for the first time).

Let’s see how Web Applications developer store objects in VIEWSTATE,

// Storing a Customer object in view state.
Customer cust = new Customer ("Dilip", "Kutarmare");
ViewState["CurrentCustomer"] = cust;

// Retrieve a student from view state.
Customer cust = (Customer) ViewState["CurrentCustomer"];

Now we will see how to capture and replace VIEWSTATE in a recorded script,

1. First set the html parameter length to maximum so that it can store VIEWSTATE, You can adjust this number after examining the captured value.

web_set_max_html_param_len("50000");

2. Capture the VIEWSTATE value when it is returned from the server using web_reg_save_param() function

web_reg_save_param("MyViewState","LB=\"__VIEWSTATE\" value=\"","RB=\"","ORD=ALL",LAST);

3. Substitute the captured value with the parameter name in which you captured the VIEWSTATE:

"Name=__VIEWSTATE", "value={MyViewState}", ENDITEM,

4. Run the script (with enabled advanced log) in VuGen and verify that the correlation function is capturing the right value.

Creating Custom Request in VuGen LoadRunner

There are some situations where you need to create custom request so that you can manipulate the request according to your need. Here we will see how to create custom request and tamper with it to get our job done.

Basic:When we record the script for any web application, we generally choose HTML-based recording because it is easy and minimizes the correlations you have to handle in the script.LoadRunner do generate custom request when some request does not fall under category of lr_web_submit_data() or lr_web_submit_form() requests category.Challenge:Suppose there is one request where you are sending some data items with it and every time there will be random number of data items like, first time there will be four data items second time there will be five data items in the same request. Here you cannot use same recorded request as number of data items are going to be different every time.In below example you can see that there are only four data items in the request next time there may be five data item.
"Name=SearchResultsGrid_ctl04_ViewPackageDialogResult", "Value=291570", ENDITEM,"Name=SearchResultsGrid_ctl08_ViewPackageDialogResult", "Value=291566", ENDITEM,"Name=SearchResultsGrid_ctl11_ViewPackageDialogResult", "Value=291563", ENDITEM,"Name=SearchResultsGrid_ctl12_ViewPackageDialogResult", "Value=291562", ENDITEM,

Solution:Here you can create a custom request and use a parameter which will hold the manipulated string of data items. In that string you can save different number of data items and you can change it as required.Procedure:First record the script with HTML-based recording and with proper transaction names for every action.Save that script and then click on Tools> Regenerate Script…





Below popup will come, click on Options…



It will show you the Regenerate options, Click on General> Recoding



    There are two options available, HTML-based script and URL-based script. Choose URL-based script.
    For URL-based script there is a button for advanced settings named URL Advanced, click on it.
    From the new popup select Use web_custom_request only.
    Save the settings and regenerate the script.
    In the regenerated script you can see that new script is generated which contain only web_custom_request.
    Now find the particular request which you want to manipulate with the help of transaction names which you have given while recording the script.

    Below is the HTML-based recorded request, You can see that there are four data items of grid results,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
web_submit_data("managepackages.aspx_4",
"Action=http://{p_Url}/site/secure/reportcenter/managepackages.aspx",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://{p_Url}/site/secure/reportcenter/managepackages.aspx",
"Snapshot=t50.inf",
"Mode=HTML",
ITEMDATA,
"Name=SearchResultsGrid_ctl03_ViewPackageDialogResult", "Value={c_PackageID}", ENDITEM,
"Name=SearchResultsGrid_ctl04_ViewPackageDialogResult", "Value=291570", ENDITEM,
"Name=SearchResultsGrid_ctl08_ViewPackageDialogResult", "Value=291566", ENDITEM,
"Name=SearchResultsGrid_ctl11_ViewPackageDialogResult", "Value=291563", ENDITEM,
"Name=SearchResultsGrid_ctl12_ViewPackageDialogResult", "Value=291562", ENDITEM,
LAST);
Below is the custom request which is URL-based,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
web_custom_request("managepackages.aspx_4",
"URL=http:// {p_Url}/site/secure/reportcenter/managepackages.aspx",
"Method=POST",
"Resource=0",
"RecContentType=text/html",
"Referer= http://{p_Url}/site/secure/reportcenter/managepackages.aspx",
"Snapshot=t135.inf",
"Mode=HTTP",
"Body=SearchResultsGrid_ctl03_ViewPackageDialogResult={c_PackageID}"
"{final_string}",
/*//below string is going to vary every time so we are going to manipulate it every time using string manipulation functions//

"&SearchResultsGrid_ctl04_ViewPackageDialogResult=291570"
"&SearchResultsGrid_ctl08_ViewPackageDialogResult=291566"
"&SearchResultsGrid_ctl11_ViewPackageDialogResult=291563"
"&SearchResultsGrid_ctl12_ViewPackageDialogResult=291562"
*/

LAST);


In custom request you can manipulate final string every time using string manipulation functions available in VuGen.

final_sting= “&SearchResultsGrid_ctl04_ViewPackageDialogResult=291570&SearchResultsGrid_ctl08_ViewPackageDialogResult=291566&SearchResultsGrid_ctl11_ViewPackageDialogResult=291563&SearchResultsGrid_ctl12_ViewPackageDialogResult=291562″