Monday 29 January 2018

How to handle JSON request in LoadRunner(LR 12.53)

JSON request can be handled using lr_eval_json function in LR 12.53.
lr_eval_json parses a JSON string, creates a JSON object, and stores the handle of the object in a parameter.
This function is not recorded. You can insert it manually into your script.

Step1: Capture JSON request from the response body using web_reg_save_param.

Step2: Input captured parameter of json parameter and save it in a string using below lr_save_sting function.
        lr_save_string(json_input, "JSON_Input_Param");
Step3: Create a Json object using above json string.
 //Create a Json object from a string.
 lr_eval_json("Buffer={JSON_Input_Param}",
              "JsonObject=json_obj_1", LAST);
 //Create a Json object from a file.
 lr_eval_json("Buffer/File=store.json", 
              "JsonObject=json_obj_2", LAST);

Step4: Using below function values can be fetched from json object and used in the scritps.
      lr_json_get_values("JsonObject=json_obj", 
                         "ValueParam=val", 
                         "QueryString=$.val", 
                         "SelectAll=Yes", 
                         LAST);
Values will be stored in parameter like val_1, val_2, val_3.........

Alternate way:
Using web_reg_save_param_json function.
The web_reg_save_param_json function supports array type parameters. When you specify SelectAll=Yes, all the occurrences of the match are saved in an array. 
Each element of the array is represented by the ParamName_index.
In the following example, the parameter name is A:
web_reg_save_param_json("ParamName= A", "QueryString=$..arguments.additional_context[0].name", "SelectAll=Yes", LAST );
The first match is saved as A_1, the second match is saved as A_2, and so forth. You can retrieve the total number of matches by using the following term: ParamName_count.
For example, to retrieve the total number of matches saved to the parameter array,
use: TotalNumberOfMatches=atoi(lr_eval_string("{A_count}"));
web_reg_save_param_json is not recorded. You can add it manually to a script.
Argument Description
ParamName The name of the parameter to store the returned value. If the parameter does not exist, it is created.
QueryString The path of the value to save. For the syntax of the query string, see Json Path on GitHub.
SelectAll Optional: If SelectAll=Yes, all the occurrences of the match are saved in an array. See Saving Multiple Matches below in this topic.
List of Attributes For details of each attribute, see Attributes for Save Parameter Registration Functions. Attribute value strings (e.g., "Search=body") are not case-sensitive. See the Restrictions.
SEARCH FILTERS Specifies the sections of the buffer to search for the string in. See Search Filters for Save Parameter Registration Functions. See the Restrictions.
LAST A marker that indicates the end of the argument list.

1 comment: