Forum Discussion

shales's avatar
shales
Occasional Contributor
6 years ago

Getting and Setting a property based on Sibling value

I am looking to set a test case level property based on the id field in the response.  I only care about the ID that contains a specific value in the 'node' field. 

 

Response from myTest Case named "GET/Nodes"

[{"id":53,"node":"TestA"},{"id":54,"node":"TestB"},{"id":136,"node":"TestC"},{"id":55,"node":"TestD"}]

For instance I want a test case level property called 'nodeId' that uses the id where the 'node' field contains "TestB". I am not sure what the best way to do this is. 

  • HI Olga,
    Yes, the issue appeared to be that the request contained too much data and the jsonslurper was running out of memory.  We changed the call to a batching call which seems to have resolved our issues.

  • Hi shales ,

     

    Didn't get it clearly but if you want to fetch some property based on some specific condition then you can use

     

    1. If, else if

    2. switch condition

     

    by writing your own custom groovy script.

     

    • shales's avatar
      shales
      Occasional Contributor

      Thanks HimanshuTayal for your reply!  Sorry this is all pretty new to me. 

       

      I am still unsure how to set the ID field as a property.  To be clear, I know the groovy command for setting the property, I am just not sure how to tell the command that I want the property value to equal the ID field that has a sibling 'node' that is equal to TestB.  What gives me trouble is the indexing.  Once I find the desired value in the node field how do I take that index and apply it to the ID field so that I get the desired ID?  Am I going down the wrong path with indexing?  Is there a way to do it via wildcards?  I'm not sure if I should be using JsonSlurper to parse the Json then do my comparison in the groovy script. 

       

      This is the response for the test case named: "GET/Nodes".

       

      [
        {
          "id": 9661,
          "node": "TestA"
        },
        {
          "id": 9662,
          "node": "TestB"
        }
      ]

       For instance how would I write it so that it would check all id fields and not just the one specified with an index?

      Is this correct?

      def x = 0
      def response = context.expand( '${GET/Nodes#Response#$[x][\'node\']}' )
      def nodeId = context.expand( '${GET/Nodes#Response#$[x][\'id\']}' )
      
      if (response != 'TestB') {
      x++;
      }
      else{
      testRunner.testCase.testSuite.setPropertyValue('nodeId', nodeId)
      }
      

        

    • shales's avatar
      shales
      Occasional Contributor

      Thanks for the reply!  I'm not sure what happened to mine but this is all very new to me.  Should it look like this below:

      def x = 0
      def response = context.expand( '${GET/Nodes#Response#$[x][\'node\']}' )
      def nodeId = context.expand( '${GET/Nodes#Response#$[x][\'id\']}' )
      
      if (response != 'TestB'){
      x++
      }
      else{
      testRunner.testCase.testSuite.setPropertyValue('nodeId', nodeId)
      }
      

      How do I tell it that I want it to use the variable x as the index (response and nodeId lines).  I get an error with the above example.  Should I even add an index if I want to look at all of them? 

      Do I need to convert to string?  Should I be using JsonSlurper to parse the Json first?  Is there a wildcard I could use instead of an index that would achieve the same result with less code?

      • nmrao's avatar
        nmrao
        Champion Level 3

        shales ,

         

        If I understand you right, you want to extract the value of "id" field where it matches "node" value that you provide and then set its value at test case level.

         

        Also understand you know all the stuff except getting id value.

         

        Below line helps to achieve that.

        //assign response, hope you know
        def response =
        
        
        //Extract id value
        def nodeValue = 'TestB'
        def id = new groovy.json.JsonSlurper().parseText(response).find {it.node == nodeValue}.id
        
        
        //Set Id value at test case level, hope you know