Forum Discussion

mrarkapravo's avatar
mrarkapravo
Contributor
4 years ago

how to store multiple value retrieve in property as object

I am using groovy to retrieve some value from a JSON attribute and after retrieval of those attribute value wants to store them in a variable in property as an Json Object, can someone help me out that using groovy how can I do that?

 

So, I want to store the value in property step as below -

 

 

trying with below but not working -

testRunner.testCase.testSteps["Properties"].setPropertyValue("Hash",new JsonBuilder(Hash[i]).toPrettyString())

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

    The property value pairs are both strings.  I guess the issue are the multiple strings in the JSON string.

    If you're storing the property value to be used in a later step in your test case, then you don't need to store it as a property, you can use Groovy steps to 'pull' the JSON string into the step.

     

    If this is the use case, then reply and try and describe the steps

  • TNeuschwanger's avatar
    TNeuschwanger
    Champion Level 1

    Hello mrarkapravo

     

    Just because your attribute values came from JSON, doesn't mean putting them together is still JSON. Like ChrisAdams said, you are just showing a list of strings with curly brackets around them which does not make it JSON.

     

    I would propose just a list of strings that you can store as a property value. When you need that list in some other step you will need to manipulate it slightly to form a listObject since anything stored as a property is only a string value. listObj -> string (property) -> listObj.

     

    Some sample code that might meet your need. It is just a way to pass list object around your test steps by keeping content in properties. You could also just put the list object into the context of the running testcase and it would be available to other test steps (but only available during the run of the test case as a whole)...

    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" start...';
    log.info "";
    
    // example... not json
    def notValidJSONStr = '{"123", "234"}';
    log.info "notValidJSONStr=$notValidJSONStr";
    
    //  ====== Set a list to properties
    def strList = [];
    strList.add("123");
    strList.add("234");
    log.info "strList=$strList";
    strList.each {
       log.info it.trim();
    };
    
    context.conList = strList;
    
    // set list to string for storage in properties...
    testRunner.testCase.testSteps['Properties'].setPropertyValue("strList", strList.toString());
    log.info "-------------- transform ------------------";
    
    //  ====== Get a list from properties
    // get content of string property and restore it to list object...
    def differentStr = context.expand( '${Properties#strList}' );
    log.info "differentStr=$differentStr";
    
    // lose the brackets surrounding the strings
    // if using ReadyAPI...
    //differentStr = differentStr.takeBetween('[', ']');
    // if using SoapUI or ReadyAPI...
    differentStr = differentStr.substring(1, differentStr.length() - 1);
    log.info "differentStr=$differentStr";
    
    // now get back to a list
    def differentStrList = differentStr.tokenize(',');
    differentStrList.each {
       log.info it.trim();
    };
    
    log.info "";
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';

     

    In the event you want the context from another groovy test step in the same test case (take it or leave it).  I prefer just putting it in a property (illustrated above) since I can debug it better than running the whole test case to get context.

    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" start...';
    log.info "";
    
    
    strList = context.conList;
    strList.each {
       log.info it.trim();
    };
    
    
    log.info "";
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';

     

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      What a great thread, thank you ChrisAdams and TNeuschwanger!

       

      mrarkapravo did the suggestions help? Please mark the best reply as a solution.

  • nmrao's avatar
    nmrao
    Champion Level 3
    If the same has to be done, instead of processing the response multiple times, it would be better to read the required data from the response when needed. That way, no need to write multiple scripts.