简介 TestRunner: a TestCaseRunner object, which is the entry-point to the SoapUI API for accessing project items,results, and so on. context: a TestCaseRunContext object holding context-related properties. messageExchange: 当前交互 re
简介 TestRunner: a TestCaseRunner object, which is the entry-point to the SoapUI API for accessing project items,results, and so on. context: a TestCaseRunContext object holding context-related properties. messageExchange: 当前交互 request/response 的 MessageExchange,可以用来直接访问 message content, HTTP Headers,Attachment 等对象: http://www.soapui.org/apidocs/com/eviware/soapui/model/iface/MessageExchange.html log: 一个标准的 Log4j Logger 对象 SoapUI官网: https://www.soapui.org/ http://readyapi.smartbear.com/ http://readyapi.smartbear.com/structure/steps/script/groovy/start API Document: http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/teststeps/RestResponseMessageExchange.html http://www.soapui.org/apidocs/com/eviware/soapui/model/testsuite/TestSuite.html http://www.soapui.org/apidocs/com/eviware/soapui/model/testsuite/TestCaseRunner.html http://www.soapui.org/apidocs/com/eviware/soapui/model/testsuite/TestStep.html http://docs.groovy-lang.org/latest/html/gapi/ https://smartbear-cc.force.com/portal/KbHome?utm_source=doc_notif_popup&utm_medium=rapi&utm_campaign=Support TestCase Memory Usage: http://www.soapui.org/working-with-soapui/improving-memory-usage.html By default, SoapUI automatically saves the entire request-response message exchange for each request so it can be viewed by double-clicking the corresponding entry in the TestCase log. Obviously this will fill up memory over time no matter how much you have allocated, but fortunately there are ways to discard old results from memory if not needed. 1. 属性表达式Property Expansion 1.1 获取响应数据: ${Search Request#Response#//ns1:Item[1]/n1:Author[1]/text()} 1.1.1获取json属性 response = context.expand(‘${GetDataPoints#Response)---Json format response = context.expand(‘${GetDataPoints#Response#$.params[2].options[0].id}‘) ---Json format ‘.‘表示当前json对象 1.1.1获取XML属性 /** * */ responseAsXml = context.expand( ‘${JDBC Request#ResponseAsXml‘)---Xml format responseAsXml = context.expand(‘${JDBC Request#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/ID[1]}‘) --Xml format 1.2 获取property属性值: 1.2.1 获取Project、TestSuite、TestCase中的属性值 #Project# - references a Project property(Reference properties across a particular SoapUI project) #TestSuite# - references a TestSuite property in the containing TestSuite #TestCase# - references a TestCase property in the containing TestCase Example: context.expand("${#Project#FilePath}") context.expand("${#TestSuite#FilePath}") context.expand("${#TestCase#FilePath}") #MockService# - references a MockService property in the containing MockService 1.2.2 获取全局属性值 #Global# - references a global property. Found in File>Preferences>Global Properties tab. Reference properties across all projects Example: Preferences -> Global Properties: test = global variables assert context.expand("${test}") == "global variables" 1.2.3 获取系统属性值 #System# - references a system property. Found in Help>System properties. Example: assert context.expand(‘${#System#file.encoding}‘) == ‘GBK‘ 1.2.4 获取环境变量值 #Env# - references an environment variable Example: context.expand(‘${#Env#JAVA_HOME}‘) 1.2.5 获取TestStep中属性值 #[TestStep name]# - references a TestStep property Example: Property step 获取属性值 Property step: context.expand(‘${Properties#variable}‘) 脚本step获取return的返回值 Groovy Step: context.expand(‘${getSql2#result}‘) 请求step获取响应值 Request Step: context.expand(‘${Request 1#Response}‘) 1.3 Dynamic Properties: 动态属性 ${=(int)(Math.random()*1000)} ${=request.name} ${=request.operation.interface.project.name} ${=import java.text.SimpleDateFormat ; new SimpleDateFormat("YYYY-MM-DDT00:00:00").format(new Date())} context.expand(‘${=request.name}‘) Test library: http://www.soapui.org/apidocs/com/eviware/soapui/impl/rest/RestRequest.html 1.4 Nested Properties: 嵌套属性 testxml = "hello" testxpath = "//value[@id=${id}]/text()" id = "123" -> "${#testxml#${testxpath}}" evaluates to "hello" 2. SoapUI自定义变量: 2.1 messageExchange: testLibrary: http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/teststeps/RestResponseMessageExchange.html 2.1.1 获取响应Get response content: response = messageExchange.response.responseContent response = messageExchange.responseContent String response = messageExchange.getResponseContent() String response = messageExchange.getResponseContentAsXml() 2.1.2 获取请求URL Get the request URL: String url = messageExchange.getEndpoint() 2.1.3 获取响应头 Get the request headers: HashMap map = messageExchange.getResponseHeaders() 2.1.4 获取响应状态码 Get http code: int status = messageExchange.getResponseStatusCode() 2.1.5 获取耗时 Get time taken: long time = messageExchange.getTimeTaken() 2.2 testRunner: com.eviware.soapui.impl.wsdl.panels.support.MockTestRunner testLibarary: http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/panels/support/MockTestRunner.html testRunner.testCase //可以访问和操作项目中的所有对象 testRunner.fail(....) testRunner.cancel,结束测试执行 testRunner.gotoTestStepByname("Groovy Script") // goto 表示跳转到哪一步开始执行,会等待当前脚本执行完成再跳转到对应步骤执行 testRunner.runTestStepByname("Groovy Script") //脚本会先去执行这个步骤,然后继续执行当前脚本 2.3 context: com.eviware.soapui.impl.wsdl.panels.support.MockTestRunContext testLibarary: http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/panels/support/MockTestRunContext.html 2.3.1 获取当前testStep,Get current test step: TestStep step = context.getCurrentStep() 2.3.2 获取当前TestStep所在index Get the index of current step: in index = context.getCurrentStepIndex() 2.3.3 获取属性 Get property: context.getProperty(String testStep, String propertyName) 2.3.4 获取用例 TestCase tc = context.getTestCase() 2.3.5 获取TestCaseRunner tcr = context.getTestRunner() 2.3.6 context.expand(‘${#TestCase#expectValue}‘) 2.3.7 使用Via XPath expressions def responseAsXml = context.expand( ‘${HTTP Request#ResponseAsXml#//table[4]/tr[2]/td[3]/table[1]/tr[1]/td[2]/span[4]/text()}‘) log.info responseAsXml xptah specs: http://www.w3school.com.cn/xpath/xpath_syntax.asp 2.3.8 其他方法 other methods: getProperties, getProperty, getPropertyNames, hasProperty, removeProperty, setProperty 2.4 log: testLibarary: org.apache.log4j.Logger log.info "Test information" 2.5 project: http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/WsdlProject.html 1> project.name 2> project.getPropertyValue(name) 3> project.setPropertyValue(name, value) 4> project.testSuiteList.each{log.info it.name} 5> project.testSuites.each{key,value-> log.info key //case name } 2.6 testSuite: com.eviware.soapui.impl.wsdl.WsdlTestSuite http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/WsdlTestSuite.html 1> testSuite.name 2> testSuite.getProject().getPropertyValue(name) 3> testSuite.getProject().setPropertyValue(key,value) 4> testSuite.getPropertyValue(name) 5> testSuite.setPropertyValue(name,value) 6> testSuite.testCaseList.each{log.info it.name} 7> testSuite.testCases.each{key,value-> log.info key //case name } 8> testSuite.getTestCaseByName(arg0) 2.7 testCase: com.eviware.soapui.impl.wsdl.WsdlTestCasePro http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/testcase/WsdlTestCase.html 1> testCase.name 2> testCase.getPropertyValue(name) 3> testCase.setPropertyValue(name, value) 4> testCase.testStepList.each{log.info it.name} 5> testCase.getTestSteps().each{key, value-> log.info key } 6> testCase.getTestStepByName(arg0) 8> testCase.project 9> testCase.testSuite 10> testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep.class) 通过类型过滤查找得到 testSteps列表。 3. 获取响应Get response of testStep: 3.1 获取当前 testStep的响应 1> response = messageExchange.response.responseContent 2> response = messageExchange.responseContent 3.2 获取其他testStep的响应. 1> context.expand(‘${testStepNM#response}‘) 2> testRunner.testCase.testSteps["testStepNM"].testRequest.response.contentAsString 4. Json解析和构建 4.1 jsonSlurper解析 testLibarary: http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html 如json为: {"calendar": [ {"calendar_id":"1705","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, {"calendar_id":"1706","showtime":"1288933200","endshowtime":"1288936800","allDay":false}, {"calendar_id":"1709","showtime":"1288935600","endshowtime":"1288938900","allDay":false} ] } import groovy.json.jsonSlurper response = messageExchange.response.responseContent // response = testRunner.testCase.testSteps["getCalendarListByCoid"].testRequest.response.contentAsString def slurper = new JsonSlurper() def re = slurper.parseText(response) //访问顶级元素,返回的是一个列表使用size 获得长度 re.calendar re.calendar.size() //访问元素模式 re.calendar.calendar_id re.calendar.calendar_id[index] //另一种访问形式 re[‘calendar‘][‘calendar_id‘][index] //查找过滤 re.calendar.calendar_id.find{it==‘1706‘} //获得的结果是 calendar_id 1706 re.calendar.find{it.calendar_id==‘1706‘} //获得的是calendar元素 4.2 JSONObject解析 def jsonMeta = net.sf.json.JSONSerializer.toJSON(messageExchange.responseContent); meta = jsonMeta.get("_meta").get("response_status") 4.3 json builder构建 def createAuthorId = context.expand( ‘${查询空间详情#Response#$.createAuthorId}‘ ) def flag = "1" import groovy.json.JsonBuilder def json = new JsonBuilder() json { userID createAuthorId fileflag flag} return json 5. xml解析 5.1 XmlParser testLibarary: http://docs.groovy-lang.org/latest/html/api/groovy/util/XmlParser.html def langs = new XmlParser().parse("languages.xml") def xml = "" def langs2 = new XmlParser().parseText(xml) log.info langs.lang.attribute("text") //注意与xmlSlurper的区别 5.2 XmlSlurper testLibarary: http://docs.groovy-lang.org/latest/html/api/groovy/util/XmlSlurper.html def langs = new XmlSlurper().parse("languages.xml") def xml = "*******" def langs2 = new XmlSlurper().parseText(xml) log.info
[email protected] Example 1: xmlStr = ‘‘‘ ‘‘‘ def result = new XmlSlurper().parseText(xmlStr) def rows = result.ResultSet.Row log.info rows.DESCRIPTION.join(‘,‘)//output: Information1,Information2,Information3 示例:http://renjie120.iteye.com/blog/1504827 6. 常用method-testRunner 6.1 Get response: testRunner.testCase.testSteps["ColumnSetsAllSummary"].testRequest.response.contentAsString context.expand(‘${Groovy2#result}‘) context.expand(‘${Request 1#Response}‘) 6.2 Add propery: testRunner.testCase.getTestStepByName("stepNM").addProperty("propertyNM") testRunner.testCase.addProperty("propertyNM") testRunner.testCase.testSuite.addProperty("propertyNM") 6.3 Set property value: testRunner.testCase.getTestStepByName("stepNM").setPropertyValue("propertyNM","value") testRunner.testCase.setPropertyValue("propertyNM","value") testRunner.testCase.testSuite.setPropertyValue("propertyNM","value") 6.4 Delete property: testRunner.testCase.getTestStepByName("stepNM").removeProperty("properNM") testRunner.testCase.removeProperty("properNM")) testRunner.testCase.testSuite.removeProperty("properNM") 6.5 Get properties: HashMap map = testRunner.testCase.getTestStepByName("failedDataPoints").getProperties() 6.6 Get a property value: testRunner.testCase.testSteps["Properties"].getPropertyValue("properNM") testRunner.testCase.getTestStepByName("Properties").getPropertyValue("properNM") testRunner.testCase.getPropertyValue("propertyNM") testRunner.testCase.testSuite.getPropertyValue("propertyNM") 6.7 Get all property names: testRunner.testCase.getTestStepByName("stepNM").getPropertyNames() 6.8 Get cookie: testRunner.testCase.getTestStepByName("login").testRequest.response.responseHeaders["Set-Cookie"] 6.9 向下访问 testRunner.testCase.testSteps[testStepName] testRunner.testCase.getTestStepByName("新增一个空间") 6.10 向上访问,用于访问同一项目中的其他testSuites 和 testCase下的元素 testRunner.testCase.testSuite.project.testSuites[testSuiteName].testCases[testCaseName].testSteps[testStepName] 6.11 几乎所有元件都有get 和set属性的方法 setPropertyValue getPropertyValue //在下面的4,5,6点中,几乎所有的属性值都可以和对应的get方法互相替换等价 6.12 获取响应体 myResponse=testRunner.testCase.getTestStepByName("新增一个空间").testRequest.response myRequest=testRunner.testCase.getTestStepByName("新增一个空间").testRequest 可以替换使用:getTestRequest(),testSteps[testStepName],getResponse()等。 context.expand(‘${Groovy2#result}‘) context.expand(‘${Request 1#Response}‘) 比较特殊的2个 获取响应作为String myResponse.contentAsString 或者 myRequest.getResponseContentAsString() 获取响应作为Xml myResponse.contentAsXml 或者myRequest.getResponseContentAsXml() 6.13 响应头和请求头 响应头: testRunner.testCase.getTestStepByName("新增一个空间").testRequest.response.responseHeaders["Location"] 或者testRunner.testCase.getTestStepByName("查询空间详情").testRequest.getResponse().getResponseHeaders("Location") 请求头: testRunner.testCase.getTestStepByName("查询空间详情").testRequest.requestHeaders testRunner.testCase.getTestStepByName("查询空间详情").testRequest.getRequestHeaders("Location") 设置请求头 def headerMap = new StringToStringMap() // headerMap.put("headerKeyName","HeaderKeyWord") //添加到map testRunner.testCase.getTestStepByName("查询空间详情").testRequest.setRequestHeaders(headerMap) 6.14 获取请求地址 testRunner.testCase.getTestStepByName("查询空间详情").testRequest.path 6.15 getAt(String) 方法,传入property名字,检索出值 针对testRunner下的所有对象,都可以通过此方法直接获取到 属性,例如: testRunner.testCase.getTestStepByName("查询空间详情").testRequest.getAt("path") 对应:testRunner.testCase.getTestStepByName("查询空间详情").testRequest.path testRunner.testCase.getTestStepByName("查询空间详情").testRequest.response.getAt("responseHeaders") 对应:testRunner.testCase.getTestStepByName("查询空间详情").testRequest.requestHeaders 6.16 获取全部用例并循环 def testCaseList = testRunner.testCase.testSuite.getTestCaseList() testCaseList.each{ if(it.testSteps["Input"]) it.testSteps["Input"].setPropertyValue("spaceid",uid) } 6.17 相对的获取用例数,getTestCaseCount() for(int i=0; i