My approach to using Groovy scripts is to always try to use the out of the box functionality first, only resorting to Groovy if needed. Saying that I do rely on Groovy for numerous tasks.
Here is a script that I usually always set as the "TestRunListener.afterRun" event, when a test case fails this script logs the details of the failed tests steps in a concise and convenient form:
def logPrefix = testRunner.getTestCase().getName() + ': '
for(result in testRunner.getResults()){
if(result.getStatus().toString() != 'OK' ){
def failedTestStepName = result.getTestStep().getName()
def logPrefixStep = logPrefix.trim() + failedTestStepName + ': '
log.error(logPrefixStep + 'TestStep "' + failedTestStepName + '" finished with the status ' + result.getStatus().toString())
for(testProperty in testRunner.getTestCase().getTestStepByName(failedTestStepName).getPropertyList()){
if(testProperty.isReadOnly()){
log.info(logPrefixStep + 'Output property: ' + testProperty.getName() + ' = ' + testProperty.getValue())
}else{
log.info(logPrefixStep + 'Input property: ' + testProperty.getName() + ' = ' + testProperty.getValue())
}
}
for(message in result.getMessages()){
log.error(logPrefixStep + 'Error message: ' + message)
}
}
}
Here is another script that I usually always set as the "TestRunListener.beforeRun" event, this checks all of my test step assertions for common mistakes I always seem to make, on finding an issue it just logs a warning message. This could easily be expanded as required:
import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.*
import com.eviware.soapui.impl.wsdl.teststeps.assertions.http.*
import com.eviware.soapui.impl.wsdl.teststeps.assertions.jdbc.*
import com.eviware.soapui.impl.wsdl.teststeps.assertions.jms.*
import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.*
import com.eviware.soapui.impl.wsdl.teststeps.assertions.GroupAssertion
import com.eviware.soapui.impl.wsdl.teststeps.assertions.MessageContentAssertion
import com.eviware.soapui.impl.wsdl.teststeps.assertions.ProXPathContainsAssertion
import com.eviware.soapui.impl.wsdl.teststeps.assertions.ProGroovyScriptAssertion
import com.eviware.soapui.impl.wsdl.teststeps.ProJdbcRequestTestStep
def logPrefix = 'TestRunListener.beforeRun:' + testRunner.getTestCase().getName() + ": "
testRunner.getTestCase().getTestSteps().each{testStepName, testStep ->
logPrefix = 'TestRunListener.beforeRun:' + testRunner.getTestCase().getName() + ':' + testStepName + ": "
// Only check enabled test steps.
if(!testStep.isDisabled()){
// Test step specific checks.
if(testStep in ProJdbcRequestTestStep){
if(testStep.getAssertionList().find{ jdbcAssertion -> jdbcAssertion in JdbcStatusAssertion } == null){
log.warn(logPrefix + 'This is a JDBC Test Step, but does not have a JDBC Status assertion.')
}
}
if (testStep in com.eviware.soapui.model.testsuite.Assertable){
testStep.getAssertionList().each{ testAssertion ->
// Generic assertion checks
if(testAssertion.isDisabled()){
log.warn(logPrefix + 'Assertion "' + testAssertion.getName() + '" is disabled.')
}
// Specific assertion type checks
switch (testAssertion) {
case ProXPathContainsAssertion:
case XPathContainsAssertion:
if(!testAssertion.getPath().trim()){
log.warn(logPrefix + 'Assertion "' + testAssertion.getName() + '" XPath Expression is blank.')
}
if(!testAssertion.getExpectedContent().trim()){
log.warn(logPrefix + 'Assertion "' + testAssertion.getName() + '" Expected Result is blank.')
}
if(testAssertion.getName().equals('XPath Match')){
log.warn(logPrefix + 'Assertion "' + testAssertion.getName() + '" still has the default name.')
}
break
case ProGroovyScriptAssertion:
case GroovyScriptAssertion:
if(!testAssertion.getScriptText().trim()){
log.warn(logPrefix + 'Assertion "' + testAssertion.getName() + '" Script is blank.')
}
if(testAssertion.getName().equals('Script Assertion')){
log.warn(logPrefix + 'Assertion "' + testAssertion.getName() + '" still has the default name.')
}
break
}
}
}
}
}