Wednesday, December 30, 2015

String Manipulation Functions in Loadrunner scripting.


Business Scenario: correlate middle name from xml response. appending space to the string, string manipulation functions used

xml response :

<FullName>first middle last</FullName>
<FirstName>first</FirstName>
<MiddleName>middle</MiddleName>
<LastName>last</LastName>

OR 

<FullName>first middle last</FullName>
<FirstName>first</FirstName>
<\MiddleName>  (No Middle name)
<LastName>last</LastName>

place to substitute:

some places "first middle last" (2 space appearing in full name)
if there is no middle name "first last" (only 1 space)

so we decided to correlate the middle name (with "Notfound=warning")
if it exist append a space to it else no operation to be done.

below is the code:

Put a text checkpoint for <MiddleName> with savecount. if the middlename exists for the customer, "MiddleNameCount" will have a +ve non zero number. 


 if (atoi(lr_eval_string("{MiddleNameCount}")) > 0) //atoi function converts string to number.
    { 
lr_output_message ("MiddleName %s", lr_eval_string("{MiddleName}"));

                lr_output_message("appending space as middlename exist"); 

strcpy(Mname,lr_eval_string("{MiddleName}")); //copying the value

strcat(Mname,"%20"); //appendign space, %20 is the url format of space.

lr_save_string(Mname,"MiddleName"); //saving the appended string.

                lr_output_message("MiddleName is %s", "{MiddleName}");
    }  

explanation of notfound attribute in web reg save param : 
NOTFOUND: The handling option when a boundary is not found and an empty string is generated. 
"Notfound=error", the default value, causes an error to be raised when a boundary is not found. 
"Notfound=warning" ("Notfound=empty" in earlier versions), does not issue an error. If the boundary is not found, it sets the parameter count to 0, and continues executing the script. The "warning" option is ideal if you want to see if the string was found, but you do not want the script to fail.
Note: If Continue on Error is enabled for the script, then even when NOTFOUND is set to "error", the script continues when the boundary is not found, but an error message is written to the Extended log file. 
This attribute is optional.