This resource is a tutorial on how can you define modular code blocks in your loadrunner script. This can have multiple uses like
In following example we are calling a function through loadrunner script, this function calls and loads SPEForums.com page via function.
Follow the below tutorial to understand how the functions work in loadrunner.
2. Add following code in Sample.h
3. To make loadrunner recognise the function written in point 2 , please add following lines in globals.h file
4. Call the newly created function from Action as shown below
Refer Screenshot below
Note that the 'test' variable has no significance in the example. It is just shown to demonstrate variable passing.
Example 2: Creating a file writing function in loadrunner
It is recommended to go through example 1 to understand how to call functions before going through this example.
Step 1: Define a function in extra file
Here we have created a file "WriteToFile.h" and included below code in the file
Step 2: Add an entry for "WriteToFile.h" in globals.h file
Step 3: Call this function from actions using this code
Output of example 2 -
- Writing common code blocks into functions which can be easily mananged
- Placing file writing code into a function, which can be called throughout the test script
In following example we are calling a function through loadrunner script, this function calls and loads SPEForums.com page via function.
Follow the below tutorial to understand how the functions work in loadrunner.
- Create a new file (Sample.h) in extra files (Solution Explorer) as shown in below figure
2. Add following code in Sample.h
Code:
callSpeforums(char* username)
{
//Call SPEForums.com homepage
web_url("Speforums Home",
"URL=http://speforums.com",
"Resource=0",
"RecContentType=text/html",
"Snapshot=t1.inf",
"Mode=HTML",
EXTRARES,
LAST);
return 0;
}
3. To make loadrunner recognise the function written in point 2 , please add following lines in globals.h file
Code:
#include "Sample.h"
4. Call the newly created function from Action as shown below
Code:
char * test;
test="SPEForums";
//Call Function
callSpeforums(test);
Refer Screenshot below
Note that the 'test' variable has no significance in the example. It is just shown to demonstrate variable passing.
Example 2: Creating a file writing function in loadrunner
It is recommended to go through example 1 to understand how to call functions before going through this example.
Step 1: Define a function in extra file
Here we have created a file "WriteToFile.h" and included below code in the file
Code:
writeFile(char* param1, char* param2)
{
int fp;
//Pre-requisite :: Create folder 'trail' in c drive
fp=fopen("c:\\trail\\example1.txt","a+");
fputs(param1,fp);
fputs(":",fp);
fputs(param2,fp);
fputs("\n",fp);
fclose(fp);
return 0;
}
Step 2: Add an entry for "WriteToFile.h" in globals.h file
Step 3: Call this function from actions using this code
Code:
Action()
{
char * test1;
char * test2;
test1="SPEForums";
test2="is Best";
//Call Function
writeFile(test1,test2);
return 0;
}
Output of example 2 -