Ask a Question

Getting and Setting a property based on Sibling value

SOLVED
shales
Occasional Contributor

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
shales
Occasional Contributor

Re: Getting and Setting a property based on Sibling value

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.

View solution in original post

8 REPLIES 8
HimanshuTayal
Community Hero

Re: Getting and Setting a property based on Sibling value

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.

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
shales
Occasional Contributor

Re: Getting and Setting a property based on Sibling value

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
Occasional Contributor

Re: Getting and Setting a property based on Sibling value

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?

HimanshuTayal
Community Hero

Re: Getting and Setting a property based on Sibling value

HI @shales ,

 

Can you please share any screenshot so that it will be more clear to me, what you wanted to achieve.

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Olga_T
Moderator

Re: Getting and Setting a property based on Sibling value

Thanks for your replies, @HimanshuTayal!

 

@shales, what stage are you on now? Was the issue solved?


Olga Terentieva
SmartBear Assistant Community Manager

TanyaYatskovska
Community Manager

Re: Getting and Setting a property based on Sibling value

Hi @shales,

 

Could you please help us understand if the issue still persists? If you need any more assistance, please share the requested screenshot with us.

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager


nmrao
Community Hero

Re: Getting and Setting a property based on Sibling value

@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


Regards,
Rao.
shales
Occasional Contributor

Re: Getting and Setting a property based on Sibling value

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.

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: