SoapUI對於Json資料進行屬性值擷取與傳遞

來源:互聯網
上載者:User

標籤:cas   text   否則   change   assertion   str   ber   tostring   tor   

SoapUI的Property Transfer功能可以很好地對介面請求返回的資料進行參數屬性擷取與傳遞,但對於Json資料,SoapUI會把資料格式先轉換成XML格式,但實際情況卻是,轉換後的XML格式往往根本沒法用Xpath形式擷取到指定的資料了。因此只能用Script指令碼形式了。

{"addUsersToDirectoryResponse":{"addUserToDirectoryResponse":[{"ack":"Success","userId":"2745379"}]}}

例如要獲得userId的資料,有兩種方式

一、建立一個script步驟,然後再建一個Property Transfer步驟,將script步驟的結果傳遞到指定的properties中的property中就可以了

import groovy.json.JsonSlurper

def xresponse = testRunner.testCase.testSteps["addUserToDirectory"].testRequest.response.contentAsString
def slurper = new JsonSlurper()

def result = slurper.parseText(xresponse)

def userId = result.addUsersToDirectoryResponse.addUserToDirectoryResponse[0].userId

二、直接在script步驟中賦值

 

import groovy.json.JsonSlurper

def xresponse = testRunner.testCase.testSteps["addUserToDirectory"].testRequest.response.contentAsString
def slurper = new JsonSlurper()

def result = slurper.parseText(xresponse)

def userId = result.addUsersToDirectoryResponse.addUserToDirectoryResponse[0].userId.toString()

//testSteps[]中括弧中指定當前testCase中的哪個Properties步驟,setPropertyValue()方法中第一個userId參數指Properties步驟中名為userId的property,

第二個參數設定它的值,即上面從返回結果中獲得的userId

testRunner.testCase.testSteps["Properties"].setPropertyValue("userId",userId)

 

另外,在斷言中使用指令碼斷言時,如果也要獲得testCase步驟中的property的話,不能直接使用

testRunner.testCase.testSteps["Properties"].getPropertyValue("userId")這樣的形式,

否則會報"No such property: testRunner for clases: Script1"的錯誤

在Script Assertion中想要獲得屬性值的話可以用以下形式替代

def number = messageExchange.modelItem.testCase.testSteps["Properties"].getPropertyValue("userId")

 

對於項目中的屬性可分為這麼幾個層級Global, Project,TestSuite, TestCase

即全域變數、項目層級、用例集層級、單個用例層級

要獲得如項目層級的屬性變數的話,可以用以下方法

def time_num=context.expand(‘${#Project#time_num}‘)        //##號內為定義哪個層級的屬性變數,後面為屬性名

執行個體:

擷取登入時的介面cookies值和登入相關的JsessionID

1 def setCookie = testRunner.testCase.testSteps["開啟cas頁面"].testRequest.response.responseHeaders["Set-Cookie"]2 3 4 5 def  JSessionId6 if ((m = setCookie =~ /JSESSIONID=(\w*);/)) {7   JSessionId = m.group(1)8   testRunner.testCase.setPropertyValue( "JSESSIONID", JSessionId )9 }

 驗證登入資訊:

1 def setCookie = testRunner.testCase.testSteps["登入請求"].testRequest.response.responseHeaders["Set-Cookie"]2 def match3 if ((m = setCookie =~ /CASTGC=(.*);/)) {4   match = m.group(1)5   6 }7 def Castgc =match8 9 testRunner.testCase.setPropertyValue( "CASTGC", Castgc )

 

SoapUI對於Json資料進行屬性值擷取與傳遞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.