SoapUI AddHeaders for API Testing

import com.eviware.soapui.support.types.StringToStringMapapp


//Put ApiKey,ProductId,UserId to a StringMap
def headerMap = new StringToStringMap()
headerMap.put("ApiKey",'${#Project#ApiKey}')
headerMap.put("ProductId",'${#Project#ProductId}')
headerMap.put("UserId",'${#Project#UserId}')
headerMap.put("Accept-Encoding", "deflate")
headerMap.put("debug_request", "1")oop

def testSuiteList =  testRunner.testCase.testSuite.project.getTestSuiteList()
def testCaseList, testStepList
for(testSuite in testSuiteList){
    if(testSuite.getName() != "PerpareData"){
        testCaseList =  testSuite.getTestCaseList()
        for(testCase in testCaseList){
            //If the testCase has only one user id(the parameter's name should be userId),so use the loop1 
            if(testCase.getProperty("userId")!=null){
                headerMap.put("UserId", testCase.getProperty("userId").getValue())
            }else {
                headerMap.put("UserId",'${#Project#UserId}')
            }

            testStepList = testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep.class)
            for (testStep in testStepList){
                testStep.testRequest.setRequestHeaders(headerMap)
            }
        }
    }
}ui

上面的腳本爲每個REST type的TestStep添加了headers。spa

可是有些TestStep須要添加特殊的headers,由於Project含有的test case比較多,因此總共的TestStep也不少,就用下方的script添加headers:debug

import com.eviware.soapui.support.types.StringToStringMapxml

String headername2 = "Content-Type"
String headervalue2 = "application/xml"ip

addSpecialHeaders("RegionExposuresDataAPI","AccountsRegionExposures","GetRegionExposures_PMS",headername2,headervalue2)ci

def addSpecialHeaders(String testsuite, String testcase, String teststep, String headername, String headervalue){
    def project = testRunner.testCase.testSuite.project
    def theStep = project.getTestSuiteByName(testsuite).getTestCaseByName(testcase).getTestStepByName(teststep)
    def oldHeaders = theStep.getHttpRequest().getRequestHeaders()
//    log.info oldHeaders
//    log.info "old : " + oldHeaders[headername]
    if(oldHeaders[headername] != null){
        oldHeaders[headername] = headervalue
    }else{
        oldHeaders.put(headername, headervalue)
    }
//    log.info "new : " + oldHeaders.get(headername)
    theStep.testRequest.setRequestHeaders(oldHeaders)
}
 get

相關文章
相關標籤/搜索