SOAPUI中文教程---使用腳本進行斷言

Script-Assertion容許對消息進行任意驗證,包括消息內容,HTTP頭等。 Script-Assertion使用腳本語言(Groovy或JavaScript)編寫。使用標準的「 Add Assertion」按鈕將 Script-Assertion添加到TestStep中;

 

Add script assertion dialog

這將顯示如下配置對話框

Empty script assertion configuration

頂部區域是標準的soapUI腳本編輯器,具備相應的彈出菜單和操做。運行按鈕針對上次接收到的響應執行斷言腳本,並將結果顯示在彈出窗口中。底部顯示腳本中可用的日誌變量的輸出。

腳本中的messageExchange對象暴露了與最後一個請求/響應消息相關的一堆屬性,咱們來看一些簡單的例子:

1. 在響應中驗證HTTP頭的內容session

// check for the amazon id header 
assert messageExchange.responseHeaders["Server"] != null

2. 驗證必定的響應時間less

// check that response time is less than 400 ms 
assert messageExchange.timeTaken < 400

3. 驗證響應中是否存在附件編輯器

// check that we received 2 attachments 
assert messageExchange.responseAttachments.length == 2

4. 驗證響應中是否存在特定的XML元素spa

// check for RequestId element in response 
def holder = new XmlHolder( messageExchange.responseContentAsXml )  
assert holder["//ns1:RequestId"] != null

這個最後一個例子在soapUI Pro中大大簡化,它將一個相應的嚮導添加到Outline Editor中;

 

Add script assertion wizard

例如,選擇上面的sessionid節點將爲您自動建立如下斷言:

 

Generated script assertion

相關文章
相關標籤/搜索