How to choose a value randomly from the list

How to choose a value randomly from the list

For demonstrating the example, we will use the sample application (HP Web Tours Application).  This application shows a sample where we can book flight tickets.
The options in depart and arrive are shown as below:
web_submit_data("reservations.pl",
        "Action=http://127.0.0.1:1080/cgi-bin/reservations.pl",
        "Method=POST",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl?page=welcome",
        "Snapshot=t4.inf",
        "Mode=HTML",
        ITEMDATA,
        "Name=advanceDiscount""Value=0"ENDITEM,
        "Name=depart""Value=London"ENDITEM,
        "Name=departDate""Value=03/28/2014"ENDITEM,
        "Name=arrive""Value=Paris"ENDITEM,
        "Name=returnDate""Value=03/29/2014"ENDITEM,
        "Name=numPassengers""Value=1"ENDITEM,
        "Name=roundtrip""Value=on"ENDITEM,
        "Name=seatPref""Value=Aisle"ENDITEM,
        "Name=seatType""Value=Coach"ENDITEM,
        "Name=.cgifields""Value=roundtrip"ENDITEM,
        "Name=.cgifields""Value=seatType"ENDITEM,
        "Name=.cgifields""Value=seatPref"ENDITEM,
        "Name=findFlights.x""Value=45"ENDITEM,
        "Name=findFlights.y""Value=6"ENDITEM,
        LAST);

Loadrunner recorded the script as above when depart is selected as “London” and arrive is selected “Paris”:
Now we want to provide a random value in depart and arrive from the list of values available.

Solution:
Simple Solution is we capture the values and perform a parameterization.
But lets do using it capturing the values using correlation at runtime and select a random value programmatically.

1.     Capture the list of values.
2.     Get a random value from array.
3.     In next web_sumbit_form or web_url use the value.

If we check the Code generation Log we have the below values for depart and arrive.



Instead of discussing theoretically, lets go through below action.
Statements are followed as comments where and when required.
Solution:

Action()
{

    int place_count,i;
    char Place[100];
    web_reg_save_param("places","LB=<option value=\"","RB=\">","ORD=ALL",LAST);
/*web_reg_save_param should be placed just above the request
Here we want to exclude the double quotes. So we used \” in both LB and RB.
Also we have used ORD=ALL to capture all the options
*/
 
    lr_start_transaction("Flights");

    web_url("welcome.pl",
        "URL=http://127.0.0.1:1080/cgi-bin/welcome.pl?page=search",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?page=menu&in=home",
        "Snapshot=t3.inf",
        "Mode=HTML",
        LAST);

    lr_end_transaction("Flights",LR_AUTO);
  

//Capturing the Number of places found using correlation

    place_count=atoi(lr_eval_string("{places_count}"));
    lr_output_message("Number of places= %d",place_count);
    
// output: Action.c(47): Number of places= 18
//Here I have used lr_output_message, the output in the Replay Log is shown along with the Line number.

    for(i=1;i<=place_count;i++)
    {
        sprintf (Place,"{places_%d}",i );
      //save Place to  String city
      lr_save_stringlr_eval_string (Place),"city" );
      lr_messagelr_eval_string("{city}") );

    }

/*
Output obtained from above For Loop:

Frankfurt
London
Los Angeles
Paris
Portland
San Francisco
Seattle
Sydney
Zurich
Frankfurt
London
Los Angeles
Paris
Portland
San Francisco
Seattle
Sydney
Zurich
*/
   
/*  As we check the above output, we have duplication of values. In total there are 9 cities in the List. But we have captured 18 cities, 9 from depart and 9 from arrive. Since the List are same we will select only half list as below.
*/
    //code to select random value
    //(place_count)/2 will select only 9 out of 18.

    sprintf (Place,"{places_%d}",1 + rand() % (place_count/2) );

      //save Place to  String depart
      lr_save_stringlr_eval_string (Place),"depart" );
      lr_message"City Selected for Depart : %s" , lr_eval_string("{depart}") );

      //Output: City Selected for Depart : Seattle
//Here I have used lr_message, so in the Replay log the message is displayed without the Line number
   
  
    sprintf (Place,"{places_%d}",1 + rand() % (place_count/2) );
      //save Place to  String arrive
      lr_save_stringlr_eval_string (Place),"arrive" );
      lr_message"City Selected for Arrival : %s" , lr_eval_string("{arrive}") );

      // Output: City Selected for Arrival : Portland
      
//Parameterizing  the Depart Date as Todays date
    lr_save_datetime("%m/%d/%Y"DATE_NOW"departDate");
    lr_output_message("Depart Date is %s",lr_eval_string("{departDate}"));

    //Output: Action.c(103): Depart Date is 03/29/2014

// Parameterizing  the return Date as todays date+3

    lr_save_datetime("%m/%d/%Y"DATE_NOW+ONE_DAY*3"returnDate");
    lr_output_message("Return Date is %s",lr_eval_string("{returnDate}"));
   
    //Output: Action.c(106): Return Date is 04/01/2014
    
//Parameterizing and passing the Values of depart, departdate, arrive and arrivedate
    lr_start_transaction("Find Flight");

    web_submit_data("reservations.pl",
        "Action=http://127.0.0.1:1080/cgi-bin/reservations.pl",
        "Method=POST",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl?page=welcome",
        "Snapshot=t4.inf",
        "Mode=HTML",
        ITEMDATA,
        "Name=advanceDiscount""Value=0"ENDITEM,
        "Name=depart""Value={depart}"ENDITEM,
        "Name=departDate""Value={departDate}"ENDITEM,
        "Name=arrive""Value={arrive}"ENDITEM,
        "Name=returnDate""Value={returnDate}"ENDITEM,
        "Name=numPassengers""Value=1"ENDITEM,
        "Name=roundtrip""Value=on"ENDITEM,
        "Name=seatPref""Value=Aisle"ENDITEM,
        "Name=seatType""Value=Coach"ENDITEM,
        "Name=.cgifields""Value=roundtrip"ENDITEM,
        "Name=.cgifields""Value=seatType"ENDITEM,
        "Name=.cgifields""Value=seatPref"ENDITEM,
        "Name=findFlights.x""Value=45"ENDITEM,
        "Name=findFlights.y""Value=6"ENDITEM,
        LAST);

    lr_end_transaction("Find Flight",LR_AUTO);
    return 0;
}
In the above we can do a check for random values that depart and arrive cities are not same.
In next post lets check if the ticket was booked with the random values selected.


Comments

  1. Very well explained..

    ReplyDelete
  2. Hi, Above correlation does not cover denver in both departure and arrival city.
    Any best possible solutions?

    ReplyDelete
    Replies
    1. Hi,

      Were you able to find any approach for covering denver value in both departure and arrival city?

      Delete
  3. Hi,

    I am new to Load Runner and i tried the above mentioned code, but I am getting the ordinal count always as zero. Please help me on this.

    ReplyDelete
    Replies
    1. Hi....

      Answer for your qusn is we need to perform array merging

      Delete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. Where did the {places_count} came from? Sorry I'm new to LoadRunner.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. The Depart and the arrival city can be same sometime as we are using random function to select them.So how to make them different everytime while booking a flight?

    ReplyDelete
  9. Excellent idea!!! I really enjoyed reading your post. Thank you for your efforts. Share more like this.
    Software Testing Courses Online Certification

    ReplyDelete
  10. This post is so interactive and informative.keep updating more information...
    Career In .NET
    About Dot NET

    ReplyDelete

Post a Comment

Popular posts from this blog

Steps to Analyze AWR Report in Oracle

Vmstat Output explained

Verifications and Error Handling in LoadRunner *Web_reg_find and Web_reg_save_param*