Monday, November 11, 2019


This article expains about how to test API’s using web_rest function and also how to modify JSON resonses




1.      Web_rest() function will be used for creating GET/POST/DELETE/PUT/etc… , so insert this function and add end points including headers and body

Example:
web_rest("OneEmployeeDetails",
        "URL=http://dummy.restapiexample.com/api/v1/employee/1",
        "Method=GET",
        "Snapshot=t20084.inf",
        LAST);

2.      Get the response from the result of web_rest and play with response based on your needs , some of the useful examples are given below
3.      Before checking the scripts , few online tools needed to making JSON path and also beatification

http://jsonpathfinder.com/   -- This helps to make JSON path , which can be used directly in QueryString in lr_json_XXXX functions.
https://jsonpath.com/  -- This helps to check the json expression is correctly retrieving the outputs so that that expression can be used in functions
https://jsonformatter.curiousconcept.com/  -- This helps to validate JSON




Action()
{
    // This script helps for handling JSON data in HP Load runner
    // Get, Insert, delete , Post , etc ... 
    // Web_rest is the function we use for testing REST API's / Micro services / web services
   
   
    // Example1: 
    int i=0;
   
 web_reg_save_param_ex(
    "ParamName=Output",
    "LB=",
    "RB=",
    SEARCH_FILTERS,
    "Scope=Body",
    LAST);
   
   
    web_rest("OneEmployeeDetails",
        "URL=http://dummy.restapiexample.com/api/v1/employee/1",
        "Method=GET",
        "Snapshot=t20084.inf",
        LAST);

    lr_eval_json ("Buffer={Output}""JsonObject=JSON_Param");
   
       //indented
    lr_json_stringify("JsonObject=JSON_Param","Format=indented","OutputParam=Result",LAST );
    lr_output_message("Indented JSON is : %s"lr_eval_string("{Result}"));
   
   
    //lr_output_message("OUTPUT is::::: %s",lr_eval_string("{Output}"));
   
   
        // Load JSON
    lr_eval_json("Buffer={Output}","JsonObject=json_obj",LAST);   
   
   
       // Get values from the json
    lr_json_get_values("JsonObject=json_obj",
                       "ValueParam=sal_BeforeUpdate",
                       "QueryString=$.employee_salary",
                       //"SelectAll=Yes",
                       LAST);
      
       lr_json_stringify("JsonObject=json_obj","Format=indented","OutputParam=Result",LAST );
      lr_output_message("Indented JSON is : %s"lr_eval_string("{Result}"));
   
      
    // Print the output of QueryString
   //lr_output_message("Employee Sal :::: %s",lr_eval_string("{sal_BeforeUpdate}"));
  
  
   lr_save_string("\"isbn\": \"0-048-08048-9\"""i_new_isbn");
  
   lr_eval_json("Buffer={i_new_isbn}",
                 "JsonObject=i_json_obj2"LAST);
   
    i = lr_json_insert("JsonObject=json_obj",
                       "JsonNode=i_json_obj2",
                       "QueryString=$.*",
                       LAST);
   // Json Insert Operation
  
//   // 1 match
//    i = lr_json_insert("JsonObject=json_obj",
//                    "Value=",
//                    "QueryString=$.",
//                     LAST);    
    if (i != 1)
        lr_error_message("lr_json_insert should return %d, but returns %d"1, i);
    else
        lr_error_message("lr_json_insert should return %d, it returns correctly %d"1, i);
   
         lr_json_get_values("JsonObject=json_obj",
                       "ValueParam=store_val_afterJAYA",
                       "QueryString=$.*",
                       //"SelectAll=Yes",
                       LAST);
  
   lr_output_message("Store List After Jaya krishna insert:::: %s",lr_eval_string("{store_val_afterJAYA}"));
  

  
     
  
  
  
  
   

    
    Example2:
    
    int i=0,a=0;
    char* json_input = "{\n    \"store\": {\n        \"book\": [\n            {\n                \"category\": \"reference\",\n                \"author\": \"Nigel Rees\",\n                \"title\": \"Sayings of the Century\",\n                \"price\": 8.95\n            },\n            {\n                \"category\": \"fiction\",\n                \"author\": \"Evelyn Waugh\",\n                \"title\": \"Sword of Honour\",\n                \"price\": 12.99\n            },\n            {\n                \"category\": \"fiction\",\n                \"author\": \"Herman Melville\",\n                \"title\": \"Moby **bleep**\",\n                \"isbn\": \"0-553-21311-3\",\n                \"price\": 8.99\n            },\n            {\n                \"category\": \"fiction\",\n                \"author\": \"J. R. R. Tolkien\",\n                \"title\": \"The Lord of the Rings\",\n                \"isbn\": \"0-395-19395-8\",\n                \"price\": 22.99\n            }\n        ],\n        \"bicycle\": {\n            \"color\": \"red\",\n            \"price\": 19.95\n        }\n    }\n}";
    //char* json_output;
    lr_save_string(json_input, "JSON_Input_Param");
    
    lr_output_message("some text is: %s",json_input);
    
    
    
   
    
    // Load JSON
    lr_eval_json("Buffer={JSON_Input_Param}","JsonObject=json_obj",LAST);    
    
    lr_json_get_values("JsonObject=json_obj",
                       "ValueParam=store_val_Before",
                       "QueryString=$.store",
                       //"SelectAll=Yes",
                       LAST);
   
   lr_output_message("Store List Before:::: %s",lr_eval_string("{store_val_Before}"));

    // Save result in parameter "title"
  lr_json_get_values("JsonObject=json_obj",
                       "ValueParam=title_val",
                       "QueryString=$.store.book[0].title",
                       //"SelectAll=Yes",
                       LAST);

   // lr_save_string(json_output, "title_val");
    
   lr_output_message("Output of JsonExperssions %s",lr_eval_string("{title_val}"));
   
   lr_json_delete("JsonObject=json_obj",
               "QueryString=$.store.bicycle",
               LAST);
   
   
    lr_json_get_values("JsonObject=json_obj",
                       "ValueParam=store_val_after",
                       "QueryString=$.store",
                       //"SelectAll=Yes",
                       LAST);
   
   lr_output_message("Store List After:::: %s",lr_eval_string("{store_val_after}"));
   



   
   
   // 1 match
    i = lr_json_insert("JsonObject=json_obj",
                    "Value={\"owner\":\"Mr Black\"}",
                    "QueryString=$.store",
                     LAST);    
    if (i != 1)
        lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);
    else 
        lr_error_message("lr_json_insert should return %d, it returns correctly %d", 1, i);
    
         lr_json_get_values("JsonObject=json_obj",
                       "ValueParam=store_val_afterJAYA",
                       "QueryString=$.store",
                       //"SelectAll=Yes",
                       LAST);
   
   lr_output_message("Store List After Jaya krishna insert:::: %s",lr_eval_string("{store_val_afterJAYA}"));
    
   
   // 2 match with ValueParam and SelectAll=Yes
    lr_save_string("{\"discount\":\"20 percent\"}", "i_new_discount");
    
    a = lr_json_insert("JsonObject=json_obj",
                       "ValueParam=i_new_discount",
                       "QueryString=$.store.book[?(@.price > 10)]",
                       "SelectAll=Yes",
                       LAST);
    
     lr_json_get_values("JsonObject=json_obj",
                       "ValueParam=store_val_afterJAYA1",
                       "QueryString=$.store",
                       //"SelectAll=Yes",
                       LAST);
   
   lr_output_message("Store List After Jaya krishna insert:::: %s",lr_eval_string("{store_val_afterJAYA1}"));
   
   
   
    return 0;
}

No comments:

Post a Comment