Resource icon

Defining and Calling functions in loadrunner Vugen

  • perf-test.com need your contributions to build up a strong repository of performance engineering resources.

This resource is a tutorial on how can you define modular code blocks in your loadrunner script. This can have multiple uses like

  • 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.
  1. Create a new file (Sample.h) in extra files (Solution Explorer) as shown in below figure

upload_2016-10-7_0-21-48.png


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"

upload_2016-10-7_0-28-41.png


4. Call the newly created function from Action as shown below

Code:
char * test;
  
    test="SPEForums";
  
    //Call Function
  
    callSpeforums(test);

Refer Screenshot below
upload_2016-10-7_0-33-32.png


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 -

upload_2016-10-7_1-5-35.png

Author
anmoldubey
Views
119
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from anmoldubey