1.1. Get and Set properties
Getting properties from groovy is straight-forward; all objects that contain properties have a getPropertyValue / setPropertyValue methods. The following examples are from inside a Groovy Script step:
//get properties from testCase, testSuite and project
def testCaseProperty = testRunner.testCase.getPropertyValue( "MyProp" )
def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "MyProp" )
def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "MyProp" )
def globalProperty = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "MyProp" )
//setting values is equally straigh forward
testRunner.testCase.setPropertyValue( "MyProp", someValue )
testRunner.testCase.testSuite.setPropertyValue( "MyProp", someValue )
testRunner.testCase.testSuite.project.setPropertyValue( "MyProp", someValue )
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "MyProp", someValue )
testRunner.testCase.getTestStepByName("TestStepName").setPropertyValue("SoNumber", SoNumber)
//This is how to access the Project name from a Groovy Script TestStep:
testRunner.testCase.testSuite.project.name
//(almost all items have a name property)
testRunner.testCase.testSuite.name
testRunner.testCase.name
testRunner.testCase.getTestStepByName("TestStepName").getLabel()
1.2. Get and Set Request
testRequestStep = context.testCase.getTestStepAt(1) //Get request content testRequest = testRequestStep.getTestRequest() content = testRequest.getRequestContent() //Set request contentcontent = content.replaceAll("20503960","20503980") testRequest.setRequestContent(content) def request = context.expand( '${TestStepName#Request}' ) //The following script you can change requestHolder's request contet, But you can't change request's request. def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) def requestHolder = groovyUtils.getXmlHolder("TestStepName#Request") //change Node Value using XPath requestHolder.setNodeValue("//NodeName", "20503980" ) requestHolder["//NodeName"] = "20503980" //write updated request back to teststep requestHolder.updateProperty() def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) // Get request data def request = context.expand( '${TestStepName#RawRequest}' ) //Ordef request = context.expand( '${TestStepName#Request}' ) def requestHolder = groovyUtils.getXmlHolder(request) def requestNodeValue = requestHolder.getDomNode("//NodeName").toString() log.info requestNodeValue //Get the node value of requst <Markin xmlns="http://Mrakin.Li.Qun.Xuan"> <Li>1111</Li> <Qun>2222</Qun> <Xuan>3333</Xuan> </Markin> def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) def holder = groovyUtils.getXmlHolder("MarkinTest#Request") holder.declareNamespace( 'ns', 'http://Mrakin.Li.Qun.Xuan') def Qun = holder.getNodeValue("//ns:Qun").toString().trim() log.info Qun result:2222
1.3. XmlParser and XmlSlurper
http://www.ibm.com/developerworks/cn/java/j-pg05199/index.html
1.4 fire a request which does a query using another request's response
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
//Run the first request and get the response
def firstResponse = testRunner.runTestStepByName("Test Case 1")
def holder = groovyUtils.getXmlHolder(firstResponse.responseContent)
def txnNumber = holder["//NS1:InsertionResponse/NS1:TransactionNumber"]
// Modify the second request using the first response
def secondRequest = testRunner.testCase.getTestStepByName("Test Case 2")
def property = secondRequest.getProperty("request")
holder = groovyUtils.getXmlHolder(property.value)
holder.put("//NS1:QueryRequest/NS1:TransactionNumber", txnNumber)
property.setValue(holder.prettyXml)
// Run the second request and get the response
def secondResponse = testRunner.runTestStepByName("Test Case 2")
holder = groovyUtils.getXmlHolder(secondResponse.responseContent)
1.5 Test Automation Tools and Scripting
http://testautomationnoob.blogspot.tw/2013/01/soapui-scripting-objects_17.html