Parameterize the object xpath in TruClient Protocol Loadrunner - JavaScript Based

Scenario:

1. Search a Company in the search field.
2. Click that Company from the search results.
3. Repeat Step 1 & 2 for different companies.


  • I searched for a company PERFORG60.
  • In the click step I found that the objects xpath had company name PERFORG60 and that is used to select that company.
  • Now I need to make the above steps work for different companies that I input.
  • Since the Company names are user inputs, I need to parameterize the Company name PERFORG60 in the xpath.

Steps:

1. Expand the Click Step --> Goto Objects --> JavaScript

2. Actual xpath generated in the step - evalXPath("//h3[text()=\"PERFORG60\"]");

3. Parameterized xpath to be replaced - evalXPath("//h3[text()=\""+ArgsContext.TC.getParam("P_OrgName")+"\"]");

where P_OrgName is the company name file parameter.



Now your script will click different company names which you input.

Parameterization in TruClient Protocol Loadrunner - JavaScript Based (Method 1 - TC.getParam)

Step 1 : Create a parameter under Parameters Dialog Box. I have created a File Parameter of name P_MPID. (Highlighted in RED)


Step 2 : Click the Develop Script button

Step 3 : Goto Tools --> Miscellaneous --> Evaluate JavaScript. Click and drag Evaluate JavaScript and place it above the step where the parameter P_MPID has to be passed. (Step Highlighted in BROWN - Last Image)



Step 4 : Fetch the parameters from P_MPID using TC.getParam function and assign it to the variable MPID. Follow the code in Arguments section. (Code Highlighted in RED)

Step 5 : Goto the step where the parameter P_MPID has to be passed and replace the original value with the variable MPID in Value field under Arguments section. (Highlighted in RED)

Step 6 : By default Value and Typing Intervals fields will be in Plain Text. Now click on the drop down arrow and select JavaScript <JS>. This has to be done because we are passing the parameter using JavaScript. (Highlighted in GREEN)



C Program (using strstr function) equivalent of web_reg_save_param in loadrunner.

I had a situtaion in which I was not able to retrieve the session id from the resposne using web_reg_save_param function. The session id was available in resposne header.
So what I did is, captured the entire response header and from that I retrieved the session id using the Left & Right boundary via C Program. Pls find the code below.

char *CStr; //variable in which the response header will be saved
char *LB = "SMSESSION="; //Left Boundary of Session ID
char *RB = "; path"; //Right Boundary of Session ID
char *CorrParam = NULL; //variable in which Session ID will be saved
char *start, *end;

web_save_header(RESPONSE, "Input"); //LR function to capture the response header

/* login request */
web_submit_data("login",

lr_output_message("# Response Header:\n %s", lr_eval_string("{Input}")); //Print the response header
 
    CStr = (char *)calloc(8000000,sizeof(char));
 
   strcpy(CStr, lr_eval_string(lr_eval_string("{Input}"))); //Save it to the C variable

    if ( start = (char *)strstr( CStr, LB ) ) //Finds the first occurance of LB in CStr
    {
        start += strlen( LB );
        if ( end = (char *)strstr( start, RB ) ) //Finds the first occurance of RB in CStr
        {
            CorrParam = ( char * )malloc( end - start + 1 );
            memcpy( CorrParam, start, end - start );
            CorrParam[end - start] = '\0';
        }
    }

    if ( CorrParam )
   
   // printf( "%s\n", CorrParam );
    lr_save_string(CorrParam,"CSMSESSIONID");
    lr_output_message("%s", lr_eval_string("{CSMSESSIONID}"));

    free( CorrParam );
 
    web_save_header(RESPONSE,"");

lr_save_float - C Program to convert Integer to Float in loadrunner

I have written a C program - user defined function to convert integer values to desired float decimal values in loadrunner.


Executing a client side session creating Java Script in loadrunner using web_js_run (web protocol)

In web applications unique user Session Id's are used for security reason. These Session ID's can be created either on the server side or on the client side. If they are created on the server side then we can correlate those session ID's in our loadrunner scripts. But in my case these Session ID's were created using Java Script in the client side (Browser). So these Java Scripts have to be executed in loadrunner to create the session id's and they have to be consumed in our script.

Lets see how we can do it.

When the application launch URL is executed it gets a response with the below Java Script embedded in it to create session id.

Java Script to create client side session id:

        function uuid() {
            var chars = '0123456789abcdef'.split('');

            var uuid = [], rnd = Math.random, r;
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
            uuid[14] = '4'; // version 4

            for (var i = 0; i < 36; i++) {
                if (!uuid[i]) {
                    r = 0 | rnd() * 16;

                    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
                }
            }

            return uuid.join('');
        }


  • Copy this code in a notepad and save the file with extension '.js'. In my case I have saved the file as 'Cookie1.js'
  • Place this file inside the loadrunner script where it need to be executed.
  • In Vugen goto Design --> Insert in Script --> New Step --> Steps Toolbox --> Search & Select web_js_run. Below dialog box opens.


Under General Tab

  • Select the Code option and call the function using its name. In my case it is uuid(). Since there are no arguments the function is blank.
  • Enter a parameter name in Result parameter field to save the output of the Java Script. I have given 'output' as the parameter name. This is the parameter which has to be passed in the place of session id inside the script.


Under Sources Tab
  • Click Add. In the resulting pop up select File and enter the file path. Since we have placed file directly inside the script folder, it is sufficient to mention only the file name. In my case is 'Cookie1.js'




  • Thats it now click OK and the below web_js_run function will be generated in the script. When we run the script, session id will be created and saved in the parameter output, which can be seen in the replay log.






For loop to iterate a block of script in loadrunner


C Program:

int Max = 10; //no. of times for loop should run
int i =0; //initializing the loop

Action()
{

lr_start_transaction();
Web Request
lr_end_transaction();


for(i = 0; i<Max;i++) //For loop will run 10 times
{
lr_start_transaction(); //Block of the loadrunner script
Web Request
lr_end_transaction();
}

lr_start_transaction();
Web Request
lr_end_transaction();

return 0;
}

Loadrunner scripting for Rest API services - web_custom_request

In this post we will see how to script a Rest API call in load runner.

Rest API details:

URL : https://10.36.133.105:8104/belocc/v2/belea/dealer/B1234/rep/B1234/smsession/10/consignments?access_token=800004e-c7ca-400b-9004-48c8aef00000

Method Type: 
POST (Apart from POST other methods used in Rest API's are GET, PUT, PATCH & DELETE)

Mode: HTTP

Encoding Type: application/json

Request Body:
{
    "code":"0032906789",
    "entries":[
        {
            "CEntryNumber":"",
            "orderEntry":{
            "entryNumber":"1",
            "quantity":"2"
            }
        }
       
    ]
}

Convert the Request body to the below loadrunner format (i.e) place '\' before every '"'.

{\"code\":\"0032906789\",\"entries\":[{\"CEntryNumber\":\"\",\"orderEntry\":{\"entryNumber\":\"1\",\"quantity\":\"2\"}}]}

save the formatted request body in a character pointer.

char *JsonReqInit;

char *JsonReqFinal;

JsonReqInit = "{\"code\":\"0032906789\",\"entries\":[{\"CEntryNumber\":\"\",\"orderEntry\":{\"entryNumber\":\"1\",\"quantity\":\"2\"}}]}";   //save the string in a C variable.

lr_save_string(lr_eval_string(JsonReqInit),"JsonReqFinal");

Goto Design --> Insert in Script --> New Step

Then Steps Tool search window will open in the right pane, search for web_custom_request and open it.



Enter the details in each field and click ok. Below request will get generated in the script.

web_custom_request("Rest_API",
"URL=https://10.36.133.105:8104/belocc/v2/belea/dealer/B1234/rep/B1234/smsession/10/consignments?access_token=800004e-c7ca-400b-9004-48c8aef00000",
"Method=POST",
"TargetFrame=",
"Resource=0",
"Referer=",
"Mode=HTTP",
"EncType=application/json",
"Body={JsonReqFinal}",      //Pass the variable in the body of the web_custom_request.
LAST);