08 May 2007 - 1.7.1 |
The Groovy Script step allows you to specify an arbitrary Groovy script during the execution of a TestCase. The script has full access to the soapUI object model and can thus perform a variety of tasks, for example:
If you need to add external libraries to the soapUI classpath for your groovy scripts (for example jdbc drivers), you need to modify the soapUI.bat/.sh file manually and add the respective .jar files there (not currently possible in the Java WebStart version of soapUI).
The Groovy Script editor is a straight forward text-editor without any bells-and-whistles except undo/redo functionality;
The editor has three components (from top to bottom):
log
objectWhen a groovy script is executed, the following context variables are set for the script to use:
testRunner
: the TestRunner
running the current TestCase, this will for now always be an instance of
WsdlTestCaseRunnercontext
: the TestRunContext
running the current TestCase, this will for now always be an instance of
WsdlTestRunContextlog
: a standard log4j Logger
object available for logging arbitrary informationWhen the script is run from inside the editor using the toolbars "Run" button, the first 2 objects will be set to
mock implementations allowing limited access to their actual functionality. The log
object will in
this case write to the log window in the editor, when running from inside a TestCase the log will write to a
"groovy log" tab in the main soapUI log.
If you want to fail the script due to some internal error throw an Exception from the script containing the error message (see example below)
The following properties are available from within a groovy-script:
Getting started with Groovy is not as easy as it may seem... here are some examples to get you started:
// get request property def request = testRunner.testCase.getTestStepByName( "Request 1" ); def property = request.getProperty( "request" ); // parse out textnodes to modify def node = new groovy.util.XmlParser(false,false).parseText(property.value); def textNodes = node["soapenv:Body"]["sam:getContactInfo"]["String_1"][0].children() // modify textNodes.clear(); textNodes.add( "test" + System.currentTimeMillis() ); // write back to string def writer = new java.io.StringWriter(); def printer = new groovy.util.XmlNodePrinter( new PrintWriter( writer )); printer.print( node ); // set property property.setValue( writer.toString() )
// check if time is set startTime = context.getProperty( "startTime" ) if( startTime == null ) { startTime = System.currentTimeMillis() context.setProperty( "startTime", startTime ) } timePassed = System.currentTimeMillis() - startTime if( timePassed < 60000 ) { // countdown and set name context.currentStep.name = "Groovy Step - " + (60000-timePassed) + "ms left" log.info "timePassed = " + timePassed Thread.sleep( 1000 ) testRunner.gotoStep( 0 ) } else { // reset name and pass on context.currentStep.name = "Groovy Step" log.info "timePassed = " + timePassed + ", exiting.." }
// read the file def properties = new java.util.Properties(); properties.load( new java.io.FileInputStream( "testprops.txt" )); def targetStep = testRunner.testCase.getTestStepByName( "Properties" ); // assign single property targetStep.setPropertyValue( "myproperty", properties.getProperty( "myproperty" )); // assign all properties def enum = properties.propertyNames(); while( enum.hasMoreElements() ) { def name = enum.nextElement(); targetStep.setPropertyValue( name, properties.getProperty( name )); }
// add a properties step testRunner.testCase.addTestStep( "properties", "another step" );
// fail randomly if( Math.random() > 0.5 ) throw new Exception( "A random error has occured!" );
If you select the "Maintain HTTP Session" option in the TestCase Options Dialog, an internal HttpState
object
will be maintained for the entire TestCase, which inludes any cookies set by the target server. The following script reads all
cookies currently set in the HttpState and writes them to the groovy log:
def state = context.getProperty( com.eviware.soapui.model.testsuite.TestRunContext.HTTP_STATE_PROPERTY ) assert state != null : "Missing HttpState.." def cookies = state.cookies assert cookies.length > 0 : "Missing cookies.." for( c in 0..cookies.length-1 ) log.info cookies[c].name + " = " + cookies[c].value
If you on the other hand want to set some cookie before making a request, do as follows:
def state = context.getProperty( com.eviware.soapui.model.testsuite.TestRunContext.HTTP_STATE_PROPERTY ) assert state != null : "Missing HttpState.." state.addCookie( new org.apache.commons.httpclient.Cookie( "http://www.mydomain.com", "SessionID", "1234" ))
soapUI Pro includes a GroovyUtils class that simplifies several common scripting tasks. Instantiate GroovyUtils from within any Groovy Script in soapUI (MockResponse, Script Assertion, etc) using:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
GroovyUtils currently includes the following (few) methods:
projectPath
: a property holding the path to the containing project, usefull for accessing
data files in same foldersetPropertyValue( String testStepName, String propertyName, String value )
: sets the specified
property valuegetXmlHolder( String xmlPropertyOrString )
: Creates an XmlHodler object (see below) for
easily accessing/modifying contents of an XML document using XPath expressions. The argument must either be
a TestStep property in the TestStepName#PropertyName
format or a valid XML stringThe above mentioned XmlHolder object has the following methods:
getNodeValue( String xpath )
: returns the value of the first node pointed to by the specified
XPath expression.getNodeValues( String xpath )
: returns a String array containing the values of all nodes
pointed to by the specified XPath expresion.setNodeValue( String xpath, String value )
: sets the content of the first node pointed to by
the specified XPath expression to the specified value.xml
: property containing the updated xml stringprettyXml
: property containing the pretty-printed updated xml stringIf any of the specified xpath expressions contains no namespace declarations, those present in the XML Message will be used in the same way as in Property Expansion.
Some examples of both the GroovyUtils and XmlHolder:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) log.info( groovyUtils.projectPath ) // create holder for last response and log requestId def holder = groovyUtils.getXmlHolder( "Book Author Search#Response" ) log.info( holder.getNodeValue( "//ns1:RequestId" )) // create holder for request and set id holder = groovyUtils.getXmlHolder( "Book Author Search#Request" ) holder.setNodeValue( "//ns:SubscriptionId", ""+Math.random() ) // update request with updated xml groovyUtils.setPropertyValue( "Book Author Search", "Request", holder.prettyXml )
If you are a licensed soapUI Pro user, please dont hesitate to suggest additional methods to add to GrooyUtils and XmlHolder!