Wednesday 26 August 2015

Parametrization in loadrunner

Replacing hard coded values in the script is called Parametrization.

Parametrization helps in :

1. Reducing script size
2. Avoiding cache effect

I've reread LoadRunner VuGen User's Guide v9, trying to find the exact definition of LoadRunner parameter. That's strange, but LoadRunner Help does not contain the exact answer on this question.

In this article, I will answer basic questions, connected to LoadRunner parameters.
I think, LR beginners should study this article carefully :)

So, I recommend to read it, if you want to know:
  • the definition of LoadRunner parameter
  • LoadRunner parameter functions
  • how to create parameter in LoadRunner VuGen script
  • which types of LoadRunner parameter are available
  • how LoadRunner processes parameters - gets and sets their values
  • other key concepts, connected to LR parameters
OK, let's start.

I've recorded Web (HTTP/HTML) simple script on Web Tours demo web application. There are two first steps:

Please, notice that we use the following hard-coded username & password - jojo and jojo:
  1. "Name=username", "Value=jojo"ENDITEM,
  2. "Name=password", "Value=bean"ENDITEM,
They are hard-coded in the script! This is important.


What should we do, if we want to execute our script sequentially for different users, for example for jojo/bean, then for bob/lcdmdm/psswrd, and so on?

There are several solutions:
  • The simplest one is to record our script for each user.
    In this case, our pseudocode will look like:
  1. Login as jojo/bean
  2. Perform other actions under jojo/bean

  3. Login as bob/lcdm
  4. Perform other actions under bob/lcdm

  5. Login as dm/psswrd
  6. Perform other actions under dm/psswrd
  7.  
  8. so on...
As you can see, our code is duplicated. Actually, this is not good idea (read this article to find out why it is).
  • We can remake our code:
  1. Loop - perform next actions for each user name and password
  2.     Read user name and password into {UserName} and {Password} variables
  3.     Login as {UserName} and {Password}
  4.     Perform other actions under {UserName} and {Password}
So, what have I done?
I do not use hard-coded values (jojo/beanbob/lcdmdm/psswrd, and so on). Instead, we use special variables - {UserName} and {Password}. They are parameters.
The source code will look like:

So, now we can answer:

Question: What is LoadRunner parameter?

The simple answer is:
LoadRunner parameter is a 
special variable.
Note: We will investigate parameters and I will provide more exact definition later.

Since LoadRunner parameter is a variable, then I compare it with standard stack-based variable from C language (for example - int i).

  • Declaration
    To declare stack variable you can just write:

    1. int i;
    This means, that name of variable is 'i', and its type is int. It's easy, isn't it?

    To declare LoadRunner parameter you have to:
    1. select string, to be replaced with a parameter
    2. right-click on the selected text
    3. 'Replace with a parameter'
      'Select or Create Parameter' dialog opens.
    4. Here, you can enter name of your parameter:
      Note, that you can see an original value as well ('jojo' in our case).
    5. Specify type of parameter
      Parameter type is determined by the source of parameter data:
      Once you specified name and type of parameter, you declared LoadRunner parameter.

      The above declaration of parameter uses GUI ('Select or Create Parameter' dialog).
      This is not the only approach. We can declare parameter and assign value with lr_save_ functions (lr_save_stringlr_save_int,lr_save_searched_stringlr_save_var, and lr_save_datetime).

      See these examples:
    1. lr_save_string function

      1. lr_save_string("abCDEfgID", "prmProductID");
      2. lr_output_message("ID is: %s", lr_eval_string("{prmProductID}"));

      Here, I define new parameter ("prmProductID") and assign a value ("abCDEfgID") to it. So, the parameter contains this value.
      To make sure that the parameter contains correct value, please see the result of execution:

      Please, note that we can write analogous code using a stack variable:

      1. char *pszProductID = "abCDEfgID";
      2. lr_output_message("ID is: %s", pszProductID);

      The result will be the same:
      So, as you see, LR parameter is similar to standard variables.
    2. lr_save_int function

      1. lr_save_int(144, "prmCounter");
      2. lr_output_message("Counter: %s", lr_eval_string("{prmCounter}"));

      3. lr_save_int(-18, "prmCounter");
      4. lr_output_message("Counter: %s", lr_eval_string("{prmCounter}"));

      Result is:

      Again, we can write analogous code using stack variables:

      1. int i;
      2. char pszCounter[12];

      3. i = 144;
      4. itoa(ipszCounter10);
      5. lr_output_message("Counter: %s", pszCounter);

      6. i = -18;
      7. itoa(ipszCounter10);
      8. lr_output_message("Counter: %s", pszCounter);

      The result will be the same too:
I think, you've received evidence, that LR parameter is similar to a variable. So,
So, now you can ask:

Question: What's the difference between LR parameter and stack-based variables? 
// That is the question (C) W. Shakespeare :)

OK, I will answer - the LoadRunner parameters simplify:
  • Working with memory
    That is, you don't have to know, how many bytes allocate for a current value of parameter.
    You just use lr_save_string (for example) function, and LR allocates required number automatically.
  • Working with data sources
    For example, you can indicate, that values of parameters will be stored in a file:
    In this case, you don't have to write additional code for file processing. LoadRunner takes all routine procedures upon himself. For example - when and how update values from file, what is the current delimiter, and so on.

    Actually, these features (working with memory and data sources) are very comfortable from user's side. That's why LoadRunner parameter is used so extensively.

My article, describing LoadRunner parameter and parameterization, is up.
I hope, it was helpful :) Please, let me know...

In the next part, I will describe operations you can perform with parameters.
After that, I will explain - how LoadRunner parameterization is connected with correlation (this question was asked by Anonymous in his comment).

And, as usual, I'm waiting for your opinions.
If you have some questions or doubts - feel free to ask me

No comments:

Post a Comment