Ask a Question

How can I use a variable in Property transfer?

SOLVED
Arjan
New Contributor

How can I use a variable in Property transfer?

I want to use a variable in the Property transfer screen. I use JSONPATH to extract an ID (see screenshot)

I use the variable: invoice_id = 1041

 

When I use the following JSONPATH expression I get the desired result in Property Transfer:

"$.[?(@.invoice_id==1041)]id[0]"

 

See Attachment: Desired Result

 

The problem is that '1041' is dynamic so I want to get value '1041' out of another property saved under the testcase:  

'${#TestCase#invoice_id}'

 

Therefore I use the following JSONPATH expression:

"$.[?(@.invoice_id==${#TestCase#invoice_id})]id[0]"

 

But now I get the error message: For input string: "${#TestCase#invoice_id}" See Attachment: Error

 

NOTE: When I use the same JSPATH expression in a assertion it works just fine.

 

Is there another way to get the same result? Or how is it possible to use a variable in Property Transfer.

4 REPLIES 4
Arjan
New Contributor

Nobody?

IvanB
Occasional Contributor

Hi,

 

I encountered same issue, when using test case property for a JSONPath query. It just stops working (returns nothing). Any ideas for a workaround?

 

For path like this $[*][?(@.displayName==ABCD)]['id'] - property transfer correctly finds the value an puts it to Test Case property, but when I use another test case property instead of explicit string ABCD like this:

 

$[*][?(@.displayName=="${#TestCase#AccountNumber}")]['id']

 

it stops working just returns null.

 

Ivan.

PaulMS
Super Contributor

A test case property can be used with JSONPath match assertions and XPath property transfers so JSONPath property transfer should work as well. It seems to be a defect because $ is a special character for JSONPath and property expansion.

 

Groovy script can be used as a workaround

(removed single quotes around id in JSONPath for this example)

 

def sourcename = 'source test step name'
def propname = 'test case property name'
def jsonpath = '$[*][?(@.displayName=="${#TestCase#AccountNumber}")].id'

transfervalue = context.expand("\${${sourcename}#Response#${jsonpath}}")
context.testCase.setPropertyValue(propname, transfervalue)
log.info transfervalue

 

IvanB
Occasional Contributor

I thought that double quotes around ReadyAPI parameter name are serving this purpose. Anyways, @PaulMS thanks for help, appreciate that.

cancel
Showing results for 
Search instead for 
Did you mean: