2016-09-28 105 views
-1

我需要解析來自請求1的響應並將響應中的特定數值傳遞給請求2.以下是JSON響應。SoapUI REST響應會話令牌


HTTP/1.1 200 OK 日期:星期三,2016年9月28日19點42分35秒GMT 的Content-Length:61

{ "response":"New session Created with session Id 123456789101213" }


我應該怎麼用 - 常規或物業轉移? if groovy - 請讓我知道我應該使用什麼代碼 如果屬性傳遞 - 我應該使用什麼樣的解析消息?

回答

0

是的,它可以以任何方式完成,這是一個額外的步驟。

也可以在當前的測試步驟中使用Script Assertion而沒有額外的步驟。那裏的迴應也可以斷言。

僞指令:

  • 檢索響應並解析它
  • 斷言如果所需的值是存在於所述響應
  • 設定在測試情況下電平的值
  • 使用使用檢索到的值在測試用例的其他測試步驟中進行屬性擴展。

腳本斷言

/** 
* This is a script assertion 
* which reads the response and asserts if there is response 
* reads response property and sets at test case level property SESSION_ID 
*/ 
def jsonString = context.response 
def json = new groovy.json.JsonSlurper().parseText(jsonString) 

//Check if the response is not empty or null 
assert json, "Response received is empty or null" 
def sessionId = json.response as String 

//Check if there is reponse property exists in response json 
assert sessionId, "response property is empty or null" 
log.info "Session id : ${sessionId}" 

//To set the value at test case level 
context.testCase.setPropertyValue('SESSION_ID', sessionId) 

如何使用檢索到的會話ID的測試用例的其他測試步驟?

  • 如果步驟類型是Groovy腳本,請使用以下兩種方法之一:context.expand('${#TestCase#SESSION_ID}')
    context.testCase.getPropertyValue('SESSION_ID')

  • 如果步驟類型是其他類型的,比如SOAP,REST,JDBC, HTTP等,然後使用:
    ${#TestCase#SESSION_ID}

注:當然,它也可以在浴室o設定值r項目級別,可以在套件或項目級別分別重用檢索值