Sunday, January 26, 2014

File Operations In Loadrunner With Examples


In this post I will explain how to capture a value from server response and save to a text file using vugen.

How to capture and save to text file using loadrunner:

First I have recorded the script using bing.com and then i searched for IBM. We will get 10 results. I have captured all the 10 values and saved to a file.

Here is the code for that.


    long fp;---Here I have declared fp to store the buffer that captured.
    
int i;
    
char *SearchValue;
    
char ch[1000];

    
web_url("www.bing.com",
        
"URL=http://www.bing.com/",
        
"Resource=0",
        
"RecContentType=text/html", ----------
        
"Referer=",
        
"Snapshot=t1.inf",
        
"Mode=HTML",
        
LAST);

    
web_reg_save_param_ex(
        
"ParamName=c_search",
        
"LB/IC=< h2 >=\"",
        "
RB/IC=\"",
        "
Ordinal=all",
        
SEARCH_FILTERS,
        "
Scope=body",
        
LAST);

    
web_url("search",
        "
URL=http://www.bing.com/search?q=ibm&qs=n&form=QBLH&filt=all&pq=ibm&sc=8-3&sp=-1&sk=&cvid=7edfd3e3530644d79148f56dfad4aa0a",
        "
Resource=0",
        "
RecContentType=text/html",
        "
Referer=http://www.bing.com/",
        "
Snapshot=t7.inf",
        "
Mode=HTML",
        
EXTRARES,
        
LAST);
 
    fp=
fopen("Loadrunnerz.txt","w");
    
for(i=1;i<=lr_paramarr_len("c_search");i++)
    {
        SearchValue = 
lr_paramarr_idx("c_search", i);
        
fputs(SearchValue,fp);
        
fputs("\n",fp);
    }
fclose(fp);

Hope, most of the code is self explanatory. If you have any doubts feel free to leave a comment.



Lr_save_string In Loadrunner With Examples



we use lr_save_string mainly to convert a string to parameter and use that parameter wherever required.

Example 1:

In this example I have saved my website name in a string a[] and I converted the string to parameter using lr save string and I used URL in web_url function as shown below.



char a[]="http://loadrunnerz.blogspot.com/";
     
    
lr_save_string(a,"URL");
 
    
web_url("Home Page",
    
"URL={URL}",
    
"TargetFrame=",
    
"Resource=0",
    
"RecContentType=text/html",
    
"Mode=HTML",
    
LAST );

Example 2:

In this example I have saved a string directly into lr save string as shown below and I used URL parameter in web_url. Please note that if you are using string directly you need to give double quotations. If you are declaring as string, as shown in the above example then there is no need to use double quotations.

lr_save_string("http://loadrunnerz.blogspot.com/","URL");
 
    
web_url("Home Page",
    
"URL={URL}",
    
"TargetFrame=",
    
"Resource=0",
    
"RecContentType=text/html",
    
"Mode=HTML",
    
LAST );