1

This is the response(as XML) I get from TestCase1 when I run my REST request:

<data contentType="text/plain" contentLength="88">
    <![CDATA[{"message":"success","data":{"export_id":"064c1948fe238d892fcda0f87e361400_1467676599"}}]]>
</data>

I want to use the value of export_id(dynamic) which is 064c1948fe238d892fcda0f87e361400_1467676599 in the Test Case 2.

How Can I achieve this? I tried using property transfer but I am not sure how to configure.

Rao
  • 20,781
  • 11
  • 57
  • 77
user5653362
  • 161
  • 2
  • 15
  • Have you tried to follow the [tutorial on the property transfer](https://www.soapui.org/functional-testing/properties/transferring-properties.html) yet? I'm not sure though if you can extract the `export_id` from the embedded JSON easily with this though. – Roman Vottner Jul 06 '16 at 00:10

1 Answers1

3

The response you have is complex, not just an xml to extract the required value using property transfer. Because, it contains cdata and it internally again contains json and you need the value which is inside the of it.

So, in order to get that value, need to use Groovy Script test step instead of Property Transfer test step. Having said that, you remove the Property Transfer and have Groovy Script step in the same place.

And the contents of the Groovy Script goes here:

enter image description here

/**
* This script reads a response xml string, extracts cdata at the specified xpath
* Then parse that string as Json and extract the required value
**/
import com.eviware.soapui.support.XmlHolder
import net.sf.json.groovy.JsonSlurper
//For testing using fixed value as you mentioned in the question
//However, you can use the dynamic response as well which is coming from the 
//previous step as well if you want
def soapResponse = '''
<data contentType="text/plain" contentLength="88"><![CDATA[{
           "message":"success",
           "data": 
           {
               "export_id":"064c1948fe238d892fcda0f87e361400_1467676599"
           }
      }]]>
</data>'''
def holder = new XmlHolder(soapResponse)
//Extract the data inside of CDATA section from the response
def data = holder.getNodeValue('//*:data')
//Parse the string with JsonSlurper
def json = new JsonSlurper().parseText(data)
log.info json
//Extract the export_id
def exportId =  json.data."export_id"
log.info "Export id : ${exportId}"
//Since you wanted to use the extracted value in another test case,
//Saving the value at test suite level custom property EXPORT_ID
//So that it can be used in any of the test case once the value is set
context.testCase.testSuite.setPropertyValue('EXPORT_ID', exportId)

How to use the export_id value in other test cases?

  • If you want to use the value in a (REST / SOAP) Test Request test step, then you can simple use it using property expansion i.e., ${#TestSuite#EXPORT_ID}.
  • If you want to use the value in a Groovy Script test step, then you need get the value using log.info context.expand('${#TestSuite#EXPORT_ID}')

How to use dynamic response in the Groovy Script?

As mentioned in the in-line comments in the script above, I have shown how to get the required value using your sample data.

But it may be difficult for you to each time replace the value for variable soapRespone in the script or you may also want to automate the stuff as well.

In such cases, you just need to replace def soapResponse statement with below code :


//Replace the previous step request test step name in place of "Test Request"
//Where you get the response which needs to be proceed in the groovy script
def soapResponse = context.expand('${Test Request#Response}')
Rao
  • 20,781
  • 11
  • 57
  • 77