標籤: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資料進行屬性值擷取與傳遞