Thursday, May 2, 2013

Can we define our own header files and functions in Load runner? Yes, the art of reusing special codes in LR scripts.

Suppose, while working in Loadrunner, you create lots of scripts and you face a situation when your code is repeated in many scripts. In this case you have a choice to reuse the code and reusing is a better move. Write once use many times that’s how smart people work.

Click here to know more
How?

Create a header file with the function or code you have written. Let's write a function for calculating sum of three values:
my_sum(2, 6, -4)

Open your notepad, create new file and write this code:
#ifndef _MYSUM_H_
#define _MYSUM_H_
int my_sum(int nArg1, int nArg2, int nArg3)
{
return nArg1 + nArg2 + nArg3;
}
#endif // _MYSUM_H_

Now, save this file as "mysum.h" into "<Loadrunner installation directory>\include” folder. You can give any name to this header file here I have given it mysum as I have defined it and it is performing sum operation.
 
We have created the library file and to include it in script, use #include
preprocessing directive in Action part:
#include "mysum.h"

It’s over. I mean it’s done ;)

Write the below code in action and run the script:

int sum;
sum=my_sum(3,4,-5);
lr_output_message(“The sum is %d”,sum);

Now I will tell you why we should take pain to reuse the code, I have damn strong point to support my idea:

1.    If you use duplicated code, you have to edit each occurrence of this code.

2.    If you decide to change something as if an algorithm, implemented in a duplicated code, you are unwilling to find and edit it in each occurrence of this code.

3.    If you have tens or hundreds scripts, containing duplicate code. Rework will cost you lot of $s. So, avoid it as prevention is always better than cure. J

No comments:

Post a Comment