1.解析Json數據腳本java
//groovy讀取json的方式很簡單,re.body.businessinfo.c2rate讀取c2rate對應的值 import groovy.json.JsonSlurper def xresponse = testRunner.testCase.testSteps["Request"].testRequest.response.contentAsString def slurper = new JsonSlurper() def re = slurper.parseText(xresponse) def num=re.body.businessinfo.size() log.info(re.body.businessinfo.c2rate)
例:Json格式
{
"body":{
"businessinfo":{
"c1rate":"0.00",
"c2rate":"12.00",
"c4rate":"21.00",
"c5rate":"0.00",
"c6rate":"0.00",
"c8rate":"0.00",
"d1rate":"0.00",
"d2rate":"0.00",
"riskcode":"0355",
"ruleid":"R32000213878_11-0"
}
},
"head":{
"error_code":"0000",
"error_message":"成功",
"request_type":"???",
"response_code":"0"
}
}json
2.獲取reponsecookie
//Get response def groovyUtils = new GroovyUtils( context ) def holder = groovyUtils.getXmlHolder('inquire#Response') //Parse return value def data = holder.getNodeValue("//return") log.info(data)
3. 導入須要用到的方法和腳本:例如MD5方法、groovy.json.JsonSlurper方法
例如:import groovy.json.JsonSlurper //導入groovy.json包中的JsonSlurper方法dom
4. 調用 Groovy,java 的jar包的方法ui
import pub.Text // 調用 Groovy 的jar包中方法, //此包必須與jar包中的包名一致 def text = new Text() text.writeFile("D:\\1.log", "test")
操做方法:選中工程->Export-> JARspa
這裏打jar包時候要注意.net
1)\WebContent\META-INF\MANIFEST.MF 這個文件打到jar包中去(這個文件描述了該Jar文件的不少信息)code
2)要打的是class文件,即要選擇Export generated class files and resourcesblog
5. 使用Groovy腳本執行接口請求教程
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext CURRENT_TESTCASE = testRunner.testCase TEST_SUITE = CURRENT_TESTCASE.parent def testStep = TEST_SUITE.getTestCaseByName('TestSuite').getTestStepByName('login') def testStepContext = new WsdlTestRunContext(testStep) def result = testStep.run(testRunner, testStepContext) log.info result.responseContent
調用另外一個TestSuite中的teststep腳本以下:
def testproject = testRunner.testCase.testSuite.project
def testcase = testproject.testSuites['PublicCase'].testCases['TestCase1']
testcase.testSteps["CreateService1"].run(testRunner, context)
調用本TestSuite中的TestStep腳本以下:
testRunner.runTestStepByName("Login")
testRunner.runTestStepByName("CreateAPI1-Scenario2")
6. 步驟跳轉腳本
testRunner.gotoStepByName("Step2") 跳轉到指定步驟
testRunner.runTestStepByName("Step2") 運行指定步驟或腳本
7. 獲得32位的UUID
import java.util.UUID
//獲得32位的UUID //格式爲:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12) def uuids = UUID.randomUUID().toString().toLowerCase() //獲得去除橫線的UUID def uuids = UUID.randomUUID().toString().replace("-", "").toLowerCase();
8. 獲取cookie並添加到對應的請求中
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) testRunner.testCase.getTestStepByName("filter").testRequest.setRequestHeaders(cookieMap);
其餘參考資料: