Actually, there are three methods to generate a random number in LoadRunner:
- Using rand() Function
- By Random Parameter
- By Date/Time Parameter
1. Using rand() Function
This is a pre-defined function in LoadRunner. rand() function generates a random value in the range 0 to 32767. As per the example, the age limit should be in between 1 to 100. Hence the following code will generate the required value:
Action()
{
int randNumber;
randNumber = rand()%100 + 1; //Generate Random Number between 1 to 100
lr_output_message("Random Number is %d", randNumber);
return 0;
}
Output:
Starting action Action.
Action.c(5): Random Number is 26
Ending action Action.
Pros:
The random number can be generated using the basic function of maths.
Ease of generating the number using offset. In the above code ‘1’ is an offset which gets added in the generated random numbers every time.
A negative number can also be generated by subtracting the offset.
Cons:
The scope of the generated random number is throughout the iteration. If a different random number needs to be passed within the same iteration then this function needs to be recalled, so that another random number can be generated.
The format cannot be specified.
2. By Random Parameter:
In this method, a parameter needs to be defined in the ‘Parameters’ by selecting ‘Parameter Type’ as ‘Random Number’.
Random Value Generation in LoadRunner using Random Parameter
{
lr_output_message("Random Number is %d", atoi(lr_eval_string("{randParam}")));
return 0;
}
Output:
Starting action Action.
Action.c(3): Random Number is 77
Ending action Action.
Pros:
The number can be generated by simply defining a parameter of type ‘Random Number’.
Ease of specifying the range.
No need to write mathematical logic.
Number format can be easily defined.
A negative number can also be generated by using a minus sign (-) in the ‘Number Format’ field.
The scope of the random number can be easily defined by using ‘Update value on’ setting. A new random value can be generated at each iteration, each occurrence and once.
Support to generate x digit(s) value.
No option for offset.
3. By Date/Time Parameter:
With the help of the combination of date and time, a random number can be generated. In this method, a parameter of type Date/Time needs to be defined and the number format is created by using the combination of year, month, day, hour, minute and second.
Random Value Generation in LoadRunner using Date and Time Parameter
Figure 02: Random Value Generation using Date/Time Parameter
The value can be fetched by using the parameter name as shown below:
Action()
{
lr_output_message("Random Number is %d", atoi(lr_eval_string("{randParam}")));
return 0;
}
Output:
Starting action Action.
Action.c(3): Random Number is 171007
Ending action Action.
Pros:
The random number can be generated easily, but needs to take care of special character like /, – etc. (do not include them)
No need to write mathematical logic
A negative number can be generated by using a minus sign (-) in the ‘Date/Time Format’ field
The scope of the random number can be easily defined by using ‘Update value on’ setting. A new random value can be generated at each iteration, each occurrence and once
Support to generate x digit(s) value
An offset can be defined
Cons:
The range cannot be specified.
The number format is defined using the tokens of date and time only.
It does not allow to specify the millisecond format (without a dot).
Not suitable to generate small digit numbers.
You can choose any of the above-mentioned methods to generate a random number in the VuGen script.
=====================================================
randomizing the date.
For example in a online booking of train ticket or air tickets, we may want to book a ticket for a post date which can be any where between tomorrow or 60 days from today. Usually if it is a date is constant, that can be parameterized in LoadRunner. If that is not a fixed date and needs to be randomized, the below code will be helpful.
rand() is a C function that is used to generate a random number.
rand()%30 will give us a random number any where between 0 -29.
randNumber = rand()%30 +1 //gives you a random number anywhere between 1 - 30
lr_save_datetime("%d/%m/%Y", DATE_NOW + ONE_DAY*randNumber, "JrnyDate");
lr_save_datetime() is the LoadRunner function to save a date of a specified format to a LoadRunner variable.
In the above example, the date in the format of Date/Month/Year (25/05/2012) is saved to a LoadRunner parameter called "JrnyDate". DATE_NOW gives us the current date and ONE_DAY*randnumber will give us a future random date.
How does it work?
The rand() function generates a random number in the range 0 to 59.
The offset 1 increases the range of generated value by 1. Thus the range of random number becomes 1 to 60.
The argument DATE_NOW stores the current date.
ONE_DAY*randNumber converts the random number into date format.
Random Date is added into the current date. (DATE_NOW + ONE_DAY*randNumber)
The lr_save_datetime method saves the random date in the randDate variable.
==========================
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): 8/22/2020 12:22:14 PM
Action.c(6): 8/23/2020 12:22:14 PM
Action.c(8): 8/22/2020 1:22:14 PM
Action.c(10): 8/22/2020 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 8/23/2020 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 no of a year with first Sunday in a year as 01
%w 5 Day of the week with Sunday as zero(0)
%W 20 week no the year with first Monday in a year as 01
%x 8/23/2020 It gives Today's Date
%X 10:55:35 AM It gives time now
%y 14 It gives year without century
%Y 2020 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: 8/23/2020 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 8/23/2020
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 2020
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%2020".
Notify: Parameter Substitution: parameter "TodayDate" = "Today is 23%2020"
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)
No comments:
Post a Comment