SoapUI Groovy scripting for testers

SoapUI Groovy scripting for testers

[Back home]

SoapUI is a wonderful tool for testing API's. The open source version is sufficient if you know what you are doing.
By enhancing your tests with logic built in Groovy, you can do amazing things.
Here are some (in confusing order, I know) hints about the Groovy scripting capability inside SoapUI.

Misc stuff


The OSS version doesn't have intellisense, but that has never stopped me before. If you want to code in an IDE other than SoapUI:
groovy.grape.Grape.grab(group:'com.smartbear.soapui', module:'soapui', version:'5.4.0')
groovy.grape.Grape.grab(group:'org.apache.commons', module:'commons-io', version:'1.3.2')
groovy.grape.Grape.grab(group:'org.apache.ivy', module:'ivy', version:'2.4.0')
groovy.grape.Grape.grab(group:'commons-cli', module:'commons-cli', version:'1.3.1')
The first lesson is, when scripting Groovy inside SoapUI, println doesn't work. You have to use
You can simulate it if you want to test snippets outside of SoapUI
//overrides built-in log
class log {
    static String info(msg) {
	String smsg = msg.toString()
	println smsg;

String msg = "Hi from the log class" msg
It can be a bit overwhelming how many different ways there are of accessing elements in SoapUI. This is how you can address test cases:
def testCase = testRunner.testCase testCase

def testCaseName = "testCaseName: ${testCaseName}"
And test steps:
def testStep = testCase.getTestStepAt(0)
def testStepA = testCase.getTestStepAt(0)
def testStepB = testCase.getTestStepByName("testStep1")
def testStepC = testCase.testSteps["testStep1"] testStepA testStepB testStepC
def testStep1 = testCase.getTestStepAt(0)
def testStep2 = testCase.getTestStepByName("testStep1")
def testStep3 = testCase.testSteps["testStep1"] testStep1 testStep2 testStep3
By using context:
def groovyUtils = new
testCase = testRunner.testCase testCase

def stepNameVariable = context.expand('${#TestCase#SourceTestStep}') stepNameVariable

stepNameVariable = context.expand('${#TestCase#SourceTestStep}') stepNameVariable
Project name:
def projectName = "projectName: ${projectName}"
With log, context and testRunner built-in variables many things can be accessed:
groovyUtils = new
def projectPath = groovyUtils.projectPath "projectPath: ${projectPath}"

def projectPathFromGet = groovyUtils.getProjectPath() "projectPathFromGet: ${projectPathFromGet}"
Test step
def thisTestStep = context.getCurrentStep().getLabel() "thisTestStep: ${thisTestStep}"
//or get it from the context variable
def project = context.testCase.testSuite.project
def testStep4 = project.testSuites['TestSuite 1'].testCases['TestCase 1'].testSteps['SOAP Request1']
def testStep5 = project.getTestSuiteByName('TestSuite 1').getTestCaseByName('TestCase 1').getTestStepByName('SOAP Request1')
assert testStep4 == testStep5
Calling a web service and getting the result from the response XML
/* The web service method is as follows in Java:
 *  String callMe() {
 *  	String callMeReturn = "ping!";
 *      return callMeReturn;
 *  }

stepNameVariable = context.expand('${#TestCase 1#SOAP Request1}') "stepNameVariable: ${stepNameVariable.value}"

def soapTestStep = testRunner.testCase.getTestStepByName("SOAP Request1").name

def responseSOAP = context.expand('${SOAP Request1#Response}')
def responseSection = responseSOAP =~ /callMeReturn>(.*)<\/callMeReturn/
def response = responseSection[0][1] "response: ${response}"
Get a response value:
def xml = new XmlHolder(context.response)
//getting response with xpath
def responseValue = xml.getNodeValue("//*:callMeResponse/*:callMeReturn") "responseValue : ${responseValue}"
Display a dialog box
def alert =
if (response == 'ping!') {
    alert.showInfoMessage("Value is: ${response}", "Attention")
else {
    alert.showErrorMessage("Response doesn't match")
Get all SOAP calls
import com.eviware.soapui.impl.wsdl.teststeps.*

def totalTests=0
for( testCase6 in testRunner.testCase.testSuite.getTestCaseList() ) { "testCase.getName(): ${testCase6.getName()}"
    for( testStep in testCase6.getTestStepList() ) { "testStep.getName(): ${testStep.getName()}"
	if( testStep instanceof WsdlTestRequestStep ) { "instance of WsdlTestRequestStep"
	    if (testStep.isDisabled() == false) {
		totalTests ++
} "There are ${totalTests} WSDL request test steps"
More working with test steps
def testStepCount = testRunner.testCase.getTestStepCount() "There are ${testStepCount} test steps"

def testStep6 = testRunner.testCase.getTestStepAt(0)
def testStep7 = testRunner.testCase.getTestStepByName("Groovy Script")
def testStep8 = testRunner.testCase.testSteps["callMe"] " ${}" "testStep2: ${}" "testStep3: ${}" " ${}"


Setting properties
testRunner.testCase.setPropertyValue("CasePropName", "PropValue1")
testRunner.testCase.testSuite.setPropertyValue("SuitePropName", "PropValue2")
testRunner.testCase.testSuite.project.setPropertyValue("ProjectPropName", "PropValue3")
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue("GlobalPropName", "PropValue4")
Getting properties
assert testRunner.testCase.getPropertyValue("CasePropName") == "PropValue1"
assert testRunner.testCase.testSuite.getPropertyValue("SuitePropName") == "PropValue2"
assert testRunner.testCase.testSuite.project.getPropertyValue("ProjectPropName") == "PropValue3"
assert com.eviware.soapui.SoapUI.globalProperties.getPropertyValue("GlobalPropName") == "PropValue4"

String[] props = context.getPropertyNames() "All Properties: ${props.join(', ')}"
Delete properties

Proxy settings

import com.eviware.soapui.SoapUI
import com.eviware.soapui.settings.ProxySettings
SoapUI.settings.setString(ProxySettings.ENABLE_PROXY, "false" )


Set HTTP basic auth for all WSDL test requests in test case
import com.eviware.soapui.impl.wsdl.teststeps.*

for( testCase4 in testRunner.testCase.testSuite.getTestCaseList() ) {"Setting HTTP basic auth for all WSDL test requests in test case ["+ testCase4.getLabel()+"]")
    for( testStep in testRunner.testCase4.getTestStepList() ) {
	if( testStep instanceof WsdlTestRequestStep ) {
Programmatically create suite, case and groovy steps
import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory
suite = context.testCase.testSuite.project.addNewTestSuite("AutomatgicallyAddedSuite")
tc = suite.addNewTestCase("AutomatgicallyAddedCase")
gs1 = tc.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "AutomatgicallyAddedGroovyScript 1" )
gs2 = tc.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "AutomatgicallyAddedGroovyScript 2" )["script"].value = '\'Hi from AutomatgicallyAddedGroovyScript 1\')'
//delete it - useful for teardown
Programmatically create mock service
def mockService = project.addNewMockService("Automagix")
// you can add operations to the mock service, but it gets hairy
Extract all groovy code from project file to script files
import com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep
def testCases = context.testCase.testSuite.getTestCaseList()
testCases.each {
    for( testSteps in it.testStepList ) {
	if( testSteps instanceof WsdlGroovyScriptTestStep ) {
	    def outfile = new File("D:/temp/${}-${}.groovy")

© 2001-2023