Eventually, you face the situation when your code is duplicated in many scripts. This is a wrong way! Take my word for it :)
I will show - why it is and how to struggle against it.
Duplicated code is an evil, because:
- If you fix/modify a duplicated code, you have to edit each occurrence of this code.
- It needs add debug messages into duplicated code, you have to add debug functions into each occurrence of this code.
- If you decide to change the algorithm, implemented in a duplicated code, you are reluctant to find and edit it in each occurrence of this code.
Well, what to do?
You have create a user-defined library. It needs place your code into separate h-file.
Let's assume, we will use the following function:
sum(1, 4, -3)
which calculates a sum of arguments. It's easy :)
In LoadRunner VuGen, create new file and write this code:
Save it as "ar.h" into "Mercury\LoadRunner\include" folder. Actually, the name of file can be any. Out library will perform arithmetic operations (sum :) ), that's why I named the file as
Your library file is created! To include it, use #include preprocessing directive in Action:
Hint: If your file is not located in a default include folder ("Mercury\LoadRunner\include"), you have to specify full path, for example:
Hint: Since you deal with C language, you have to use double backslash inside of path.
That's all! Run your Action and enjoy:
As you see, our function Sum works correctly.
Now, I will answer ans show the following question:
How to process global variables in LoadRunner VuGen script?
Modify our "ar.h" from "Mercury\LoadRunner\include" folder:
Then, modify Action like this:
And add a second action ("Action2") to our script:
Note, that we added #include too.
Hint: By the way, the script will work correctly even if you didn't add #include for second action. Nevertheless, I recommend to add this preprocessing.
Execute our updated VuGen script, containing two actions:
As you can see, variable, located in h-file, is shared between actions. It saves his values between actions.
In other words, variable, located in h-file, is a global variable. It can be used for LoadRunner actions communicating.
I hope, library of user-defined functions will simplify your scripts and decrease efforts for their maintenance. As a result, it will save your time and increase your performance :)