10 April 2007 - 1.7 |
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" ))
Want some more examples? Mail the soapUI forum for a request and we will add them!