Forum Discussion

Arjan's avatar
Arjan
New Contributor
8 years ago

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.

  • 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's avatar
      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's avatar
        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