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 );

Lr_save_int Functions In Loadrunner With Examples.



Lr_save_int is same as lr_save_string it converts an integer to string and saves in a parameter.

Example 1:

In this example I have declared value=10 and save in the URL using lr_save_int.



Action()
{
    
int value=10;
    
lr_save_int(value, "URL");
    
lr_output_message("%s"lr_eval_string("{URL}"));
    
return 0;
 }

Output:

Starting action Action.
Action.c(7): Notify: Saving Parameter "URL = 10".
Action.c(10): Notify: Parameter Substitution: parameter "URL" =  "10"
Action.c(10): 10

Ending action Action.

Example 2:

In this example i have saved integer value directly to URL using lr_save_int.

Action()
{
    
lr_save_int(10"URL");
    
lr_output_message("%s"lr_eval_string("{URL}"));
    
return 0;
}

Starting action Action.
Action.c(5): Notify: Saving Parameter "URL = 10".
Action.c(7): Notify: Parameter Substitution: parameter "URL" =  "10"
Action.c(7): 10
Ending action Action.

Lr_eval_string Function In Loadrunner With Examples


It evaluates a parameter and after evaluating the parameter it replaces the parameter with its current value.

You cannot place lr_eval_string in the middle of the web functions or any other functions. You can directly use in the web functions like this
 {URL}. It will replace the parameter with its current value.


Example 1:

Action()
{
    
lr_save_string("http://www.google.com","URL");
    
lr_output_message("%s"lr_eval_string("{URL}"));

    
return 0;
}

Output:
Starting action Action.
Action.c(3): Notify: Saving Parameter "URL = http://www.google.com".
Action.c(5): Notify: Parameter Substitution: parameter "URL" =  "http://www.google.com"
Action.c(5): http://www.google.com
Ending action Action.

Example 2:

Action()
{
    
int a=10;
    
lr_save_int(a,"IntegerValue");
    
lr_output_message("%s"lr_eval_string("{IntegerValue}"));
    
lr_output_message("%d"atoi(lr_eval_string("{IntegerValue}")));

    
return 0;
}

Output:


Starting action Action.
Action.c(4): Notify: Saving Parameter "IntegerValue = 10".
Action.c(5): Notify: Parameter Substitution: parameter "IntegerValue" =  "10"
Action.c(5): 10
Action.c(10): Notify: Parameter Substitution: parameter "IntegerValue" =  "10"
Action.c(10): 10
Ending action Action.

Lr_save_datetime Function In Loadrunner With Examples


By using this function we can save the current date, time, day, hour, and minute into parameter. We have the following predefined constants that we can use with the function:

DATE_NOW, TIME_NOW, ONE_DAY, ONE_HOUR, ONE_MIN.


Example 1:
Action()
{
lr_save_datetime("%c",DATE_NOW,"Date");
lr_output_message(lr_eval_string("{Date}"));
lr_save_datetime("%c",DATE_NOW+ONE_DAY,"OneDayAdvanceDate");
lr_output_message(lr_eval_string("{OneDayAdvanceDate}"));
lr_save_datetime("%c",TIME_NOW,"TimeNow");
lr_output_message(lr_eval_string("{TimeNow}"));
lr_save_datetime("%c",TIME_NOW+ONE_HOUR,"TimeNowPlusOneHour");
lr_output_message(lr_eval_string("{TimeNowPlusOneHour}"));

   
return 0;
}

Output:
Starting action Action.
Action.c(4): 5/22/2014 12:22:14 PM
Action.c(6): 5/23/2014 12:22:14 PM
Action.c(8): 5/22/2014 1:22:14 PM
Action.c(10): 5/22/2014 1:22:14 PM
Ending action Action.

Different Date Formats Table:
Code
Date Format
Description
%a
Fri
It gives abbreviated name of a day
%A
Friday
It gives full name of a day
%b
Jun
It gives abbreviated name of month
%B
May
It gives full name of the month
%c
5/23/2014 10:55
Date and Time Stamp
%d
23
Today's Date
%H
10
It gives hour with 24 hours format
%I
10
It gives with 12 hour format
%j
143
It gives number of day in a year
%m
5
Month of the year in number format
%M
55
It gives present minute
%p
AM
It Gives AM/PM
%S
35
It gives seconds
%U
20
week number of a year with first Sunday in a year as 01
%w
5
Day of the week with Sunday as zero(0)
%W
20
It gives week no the year with first Monday in a year as 01
%x
5/23/2014
It gives Today's Date
%X
10:55:35 AM
It gives time now
%y
14
It gives year without century
%Y
2014
It gives year with century
%Z
India Standard Time
It gives time zone abbreviation
%%
%
it gives % output in your date format
Example 2:
lr_save_datetime("Today is (with abrreviated name) %a",DATE_NOW,"WeekDay");
lr_output_message(lr_eval_string("{WeekDay}"));
Output: Today is (with abbreviated name) Fri
Example 3:
lr_save_datetime("Today is (with full name) %A",DATE_NOW,"WeekDay");
lr_output_message(lr_eval_string("{WeekDay}"));
Output:
 Today is (with full name) Friday
Example 4:
lr_save_datetime("This month is (with abbreviated name) %b",DATE_NOW+ONE_DAY*30,"Month");
lr_output_message(lr_eval_string("{Month}"));
Output:
 This month is (with abbreviated name) Jun

Example 5:
lr_save_datetime("This month is (with full name) %B",DATE_NOW+ONE_DAY*30,"Month");
lr_output_message(lr_eval_string("{Month}"));
Output: This month is (with full name) June
Example 6:
lr_save_datetime("%c Date with time stamp",DATE_NOW,"DateAndTimeStamp");
lr_output_message(lr_eval_string("{DateAndTimeStamp}"));
Output:
 5/23/2014 11:56:18 AM Date with time stamp
Example 7:
lr_save_datetime("Only Todays Date %d",DATE_NOW,"TodayDate");
lr_output_message(lr_eval_string("{TodayDate}"));
Output:
 Only Todays Date 23
Example 8:
lr_save_datetime("Gives 24 Hours %H",DATE_NOW,"24_Hours");
lr_output_message(lr_eval_string("{24_Hours}"));
Output: Gives 24 Hours 11
Example 9:
lr_save_datetime("Gives 12 Hours %I",DATE_NOW,"12_hours");
lr_output_message(lr_eval_string("{WeekDay}"));
Output: Today is (with full name) Friday
Example 10:
lr_save_datetime("It gives number of day in a year %j",DATE_NOW,"TodayNumber");
lr_output_message(lr_eval_string("{TodayNumber}"));
Output: It gives number of day in a year 143
Example 11:
lr_save_datetime("Month in number format %m",DATE_NOW,"MonthInNo");
lr_output_message(lr_eval_string("{MonthInNo}"));
Output:
 Month in number format 05
Example 12:
lr_save_datetime("Gives present minute %M",DATE_NOW,"Minute");
lr_output_message(lr_eval_string("{Minute}"));
Output: Gives present minute 56
Example 13:
lr_save_datetime("Gives AM/PM %p",DATE_NOW,"AM_PM");
lr_output_message(lr_eval_string("{AM_PM}"));
Output: Gives AM/PM AM
Example 14:
lr_save_datetime("Gives Seconds %S",DATE_NOW,"Seconds");
lr_output_message(lr_eval_string("{Seconds}"));
Output: Gives Seconds 18
Example 15:
lr_save_datetime("Gives week no of the year (with Sunday as 01) %U",DATE_NOW,"WeekNo");
lr_output_message(lr_eval_string("{WeekNo}"));
Output: Gives week no of the year (with Sunday as 01) 20
Example 16:
lr_save_datetime("Day of the week with sunday as(zero) %w",DATE_NOW,"DayNo");
lr_output_message(lr_eval_string("{DayNo}"));
Output: Day of the week with sunday as(zero) 5
Example 17:
lr_save_datetime("Week No of the Year (with Monday as 01) %W",DATE_NOW,"WeekNo");
lr_output_message(lr_eval_string("{WeekNo}"));
Output: Week No of the Year (with Monday as 01) 20
Example 18:
lr_save_datetime("It gives Todays Date %x",DATE_NOW,"TodaysDate");
lr_output_message(lr_eval_string("{TodaysDate}"));
Output: It gives Todays Date 5/23/2014
Example 19:
lr_save_datetime("It gives time now %X",DATE_NOW,"TimeNow");
lr_output_message(lr_eval_string("{TimeNow}"));
Output: It gives time now 11:56:18 AM
Example 20:
lr_save_datetime("Year without centuary %y",DATE_NOW,"YearWOCentuary");
lr_output_message(lr_eval_string("{YearWOCentuary}"));
Output: Year without centuary 14
Example 21:
lr_save_datetime("Year with centuary %Y",DATE_NOW,"YearWCentuary");
lr_output_message(lr_eval_string("{YearWCentuary}"));
Output: Year with centuary 2014
Example 22:
lr_save_datetime("Abbreviation of time zone %Z",DATE_NOW,"TimeZoneAbbr");
lr_output_message(lr_eval_string("{TimeZoneAbbr}"));
Output: Abbreviation of time zone India Standard Time
Example 23:
lr_save_datetime("Today is %d%%%Y"DATE_NOW"TodayDate");
lr_output_message(lr_eval_string("{TodayDate}"));

Output: (with parameter substitution in Extended Log)


Saving Parameter "TodayDate = Today is 23%2014".
Notify: Parameter Substitution: parameter "TodayDate" =  "Today is 23%2014"
Today is 23 
(Seems here the lr_eval_string is removing the embedded parameters - You can ignore it - Not Sure What Happening in the background)