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.
The first lesson is, when scripting Groovy inside SoapUI, println doesn't work. You have to use log.info
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"
log.info 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:
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() ) {
log.info("Setting HTTP basic auth for all WSDL test requests in test case ["+ testCase4.getLabel()+"]")
for( testStep in testRunner.testCase4.getTestStepList() ) {
if( testStep instanceof WsdlTestRequestStep ) {
testStep.getTestRequest().setUsername(testSuite.getPropertyValue("Authorization"))
testStep.getTestRequest().setPassword(testSuite.getPropertyValue("HeaderValue"))
}
}
}
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" )
gs1.properties["script"].value = 'log.info(\'Hi from AutomatgicallyAddedGroovyScript 1\')'
//delete it - useful for teardown
testRunner.testCase.testSuite.project.removeTestSuite(testRunner.testCase.testSuite.project.getTestSuiteByName("AutomatgicallyAddedSuite"))
Programmatically create mock service
def mockService = project.addNewMockService("Automagix")
// you can add operations to the mock service, but it gets hairy
project.removeMockService(mockService)
Extract all groovy code from project file to script files