您的回复很复杂,而不仅仅是xml 使用属性转移来提取所需的值。因为,它包含cdata,它内部又包含json,你需要它里面的值。
因此,为了获得该值,需要使用Groovy Script 测试步骤而不是Property Transfer 测试步骤。话虽如此,您删除了 Property Transfer 并将 Groovy Script 步骤放在同一个地方。
Groovy 脚本的内容在这里:
/**
* 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)
如何在其他测试用例中使用export_id 值?
- 如果您想在
(REST / SOAP) Test Request 测试步骤中使用该值,则可以通过属性扩展来简单地使用它,即 ${#TestSuite#EXPORT_ID}。
- 如果您想在
Groovy Script 测试步骤中使用该值,则需要使用log.info context.expand('${#TestSuite#EXPORT_ID}') 获取该值
如何在 Groovy Script 中使用动态响应?
正如上面脚本中的嵌入式 cmets 所述,我已经展示了如何使用您的示例数据获取所需的值。
但您可能很难每次都替换脚本中变量 soapRespone 的值,或者您可能还想自动化这些东西。
在这种情况下,您只需将def soapResponse 语句替换为以下代码:
//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}')