您的回覆非常複雜,不僅僅是一個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腳本動態響應的價值?
正如上面腳本的在線評論中所述,我已經展示瞭如何使用您的示例數據獲取所需的值。
但是,您可能很難每次在腳本中替換變量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}')
來源
2016-07-06 01:49:06
Rao
你試過跟隨[教程財產轉移(https://www.soapui.org/functional -testing/properties/transfers-properties.html)呢?我不確定,但是如果你可以很容易地從嵌入的JSON中提取'export_id'。 –