2016-07-05 28 views
1

這是響應(如XML),我從TestCase1得到,當我跑我的REST請求:從REST響應代碼(了SoapUI)獲取變量值

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

我想用export_id的值(動態)這是測試用例2中的064c1948fe238d892fcda0f87e361400_1467676599

我該如何做到這一點?我嘗試使用財產轉讓,但我不知道如何配置。

+0

你試過跟隨[教程財產轉移(https://www.soapui.org/functional -testing/properties/transfers-properties.html)呢?我不確定,但是如果你可以很容易地從嵌入的JSON中提取'export_id'。 –

回答

3

您的回覆非常複雜,不僅僅是一個xml使用屬性轉移提取所需的值。因爲它包含cdata並且它在內部再次包含json並且您需要其中的值。

所以,爲了獲得該值,需要使用Groovy Script測試步驟代替Property Transfer測試步驟。話雖如此,您可以刪除Property Transfer並在同一地點使用Groovy Script步驟。

而且Groovy腳本的內容放在這裏:

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) 

如何使用在其他的測試用例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}') 
+0

非常感謝你。它確實有效 – user5653362

+0

很高興知道它有幫助。 – Rao

+0

和一個很好的解釋。欣賞它 – user5653362