1

I want to use a random number from a list to select the particular element of a parameter array and use it elsewhere in the script as a parameter e.g.

Array is sspaidlist My random integer from a parameter list is {GenRandomSSPAID}, which i want to use as the element of the sspaidlist array and save to is RandomSSPAID

lr_save_string(lr_eval_string("sspaidlist_{GenRandomSSPAID}"),"RandomSSPAID");

This just gets me the actual value "sspaidlist" and not the array.

I've also tried

sprintf(RandomSSPAID, "{sspaidlist_%d}", lr_eval_string("{GenRandomSSPAID}"));

but this seems to set RandomSSPAID to 0

The idea is to get 3 unique values - so 3 different array elements, I can't get the same value twice. I've offloaded the randomness to the loadrunner parameter functions, so I will always get a unique number with {GenRandomSSPAID}.

jquerynewbie
  • 27
  • 3
  • 12

2 Answers2

1

First convert your "GenRandomSSPAID" to integer as below:

i = atoi(lr_eval_string("{GenRandomSSPAID}"));

Now use sprintf to save it into RandomSSPAID as below:

sprintf(RandomSSPAID, "{sspaidlist_%d}", i);

You should be able to see value now.

S.ai
  • 61
  • 3
  • 17
  • Can I then use RandomSSPAID later on with just the curly brackets? Or do I need to use lr_save_string – jquerynewbie Jun 05 '17 at 09:58
  • Still getting zero for the RandomSSPAID value above, GenRandomSSPAID returns correctly. Get a warning 'Invalid parameter detected in function'. Will this work as a one-liner (and eliminating the need to declare anything in C?) `lr_save_int((sprintf(RandomSSPAID, "{sspaidlist_%d}", (atoi(lr_eval_string("{GenRandomSSPAID}"))))),"RandomSSPAID");` – jquerynewbie Jun 05 '17 at 10:15
  • You can use RandomSSPAID as parameter. Just cross check whether you declared all the variables. – S.ai Jun 06 '17 at 03:30
  • So i should declare as follows, right? `int RandomSSPAID; int i;` Do I need to do anything with sspaidlist? – jquerynewbie Jun 08 '17 at 10:42
  • I just get an output like this: `Message 96 Add_List.c(96): String value of Random Values are : {RandomSSPAID}` when I do `lr_output_message("String value of Random Values are : %s", ,lr_eval_string("{RandomSSPAID}"));` – jquerynewbie Jun 08 '17 at 10:47
  • Declare as below: int RandomSSPAID[100], int i; – S.ai Jul 19 '17 at 07:15
0

I resolved this with the following code:

//declare c variables
Add_List()
{
....
char *RandomSSPAID;
char *SecondRandomSSPAID;
char *ThirdRandomSSPAID;

RandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(RandomSSPAID),"RandomSSPAID");

SecondRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(SecondRandomSSPAID),"SecondRandomSSPAID");

ThirdRandomSSPAID = lr_paramarr_idx("sspaidlist",atoi(lr_eval_string("{GenRandomSSPAID}")));
lr_save_string(lr_eval_string(ThirdRandomSSPAID),"ThirdRandomSSPAID");

lr_error_message("Random Values  for iteration %s are : %s_%s_%s",lr_eval_string("{IterationNumber}"),lr_eval_string("{RandomSSPAID}"),lr_eval_string("{SecondRandomSSPAID}"),lr_eval_string("{ThirdRandomSSPAID}"));
....
}

Note that I offloaded the randomness to Loadrunner to generate a random number with {GenRandomSSPAID}, which is a parameter type of File, with a list of numbers and setting to select new row 'Random', Update value on 'Each occurance'

jquerynewbie
  • 27
  • 3
  • 12