SoapUI之cookie設置

1、測試背景:服務器

1)接口測試須要完成註冊-->登陸-->充值,使用soapui構建好測試用例、設置斷言後,運行結果以下:cookie

2)recharge接口運行失敗,繼續查看該接口具體發送的請求及返回結果有無錯誤:session

 

 3)這裏解釋下JSESSIONID是幹嗎用的。用戶登陸(login)向服務器發起請求,服務器會建立session會話保存用戶的信息,並返回一個JSESSIONID值放到響應頭set-cookie中。而後用戶繼續發起充值(recharge)請求,請求頭cookie中會帶上同一個JSESSIONID值提交到服務器,從而肯定是同一個登陸用戶發出的請求,服務器就會放行資源,充值成功。測試

TestCase運行結果中,雙擊login運行step查看Respouse Message,切換到Raw視圖,能看到JSESSIONID相關信息。那咱們如今就是要從登陸這個響應頭中將JSESSSIONID獲取到並賦給充值的請求頭中。ui

 

2、如何實現cookie設置spa

1)右鍵Test Step -->Add Step -->Groovy Script,並命名爲Setcookie。code

 

 2)在Setcookie中貼入如下代碼:blog

import com.eviware.soapui.support.types.StringToStringMap

def cookiesList = testRunner.testCase.getTestStepByName("login").testRequest.response.responseHeaders["Set-Cookie"]
log.info cookiesList
 
//Get the cookie 
String cookieNew = cookiesList.get(0)
log.info "cookie : "+cookieNew

//Put cookie to a StringMap 
def cookieMap = new StringToStringMap()
cookieMap.put("Cookie",cookieNew)

//Pass cookie to testStep "recharge" of testSuite 
//testRunner.testCase.getTestStepByName("recharge").testRequest.setRequestHeaders(cookieMap);

//Pass cookie to all testSteps of the project 
def time_num= context.expand ('${#TestCase#cookieMap}') 

def testSuiteList =  testRunner.testCase.testSuite.project.getTestSuiteList()
def testCaseList
def testStepList
for(testSuite in testSuiteList){
    testCaseList =  testSuite.getTestCaseList()
    for(testCase in testCaseList){
        testStepList = testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep.class)
        for (testStep in testStepList){
             testStep.testRequest.setRequestHeaders(cookieMap)
        }
    }
}

3)從新運行TestCase,運行成功。查看recharge(充值)請求詳細信息,顯示充值成功。接口

相關文章
相關標籤/搜索