Forum Discussion

groovyguy's avatar
groovyguy
Champion Level 1
6 years ago

Share your groovy scripts

One of the areas I really try to focus on is working with groovy scripts, as I love being able to have granular control over what is happening in my tests. From actually creating tests and populating the requests to generating data, groovy can do so much for us. I'd like to start a thread where we can share some of our favorite groovy scripts. Big or small, complex or easy, share what you have. 

 

Here's one I have that can quickly change the WSDL operation for all of the test steps in a test case:

 

import com.eviware.soapui.support.XmlHolder
import groovy.util.*
import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory

//Get the testCase Reference
testCase = testRunner.testCase;
testSuite = testRunner.testCase.testSuite;
project = testSuite.project

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
// log.info (testCase.getTestStepCount());

def correctOp; 

// This loop assumes the first test step already has the desired operation. Must change manually if that is not the case. 
for (int i = 0; i < testCase.getTestStepCount() -  1; i++)
{
	// log.info("Index: " + i);
	// log.info(testCase.getTestStepAt(i).getLabel());
	if (i == 0)
	{
		correctOp = testCase.getTestStepAt(0).getOperation();	
	}
	else 
	{
		testCase.getTestStepAt(i).setOperation(correctOp);
	}
	// log.info("-----------------------------------------------------------------------------------------------------");
}

// This step will replace text in the requests and can be used if the change from old operation to new operation is easily managed by string replacements.
for (int i = 0; i< testCase.getTestStepCount() - 1; i++)
{
	
	def sourceRequest = testCase.getTestStepAt(i).testRequest;
	request = sourceRequest.getRequestContent();
	request = request.replaceAll("ReplacedTest","NewText");
	sourceRequest.requestContent = request;
}

3 Replies

  • Radford's avatar
    Radford
    Super Contributor

    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
                    }
                }
            }
        }
    }

     

    • groovyguy's avatar
      groovyguy
      Champion Level 1

      Both of those are super useful, Radford. Thanks for sharing! I try to do as much with ReadyAPI itself and the features it offers as well, but I like augmenting and supplementing with groovy when I can too. I still need to investigate events and how to tie into those, I think that'd be a huge next step for me.

      • Radford's avatar
        Radford
        Super Contributor

        groovyguy, I know for myself the trigger that made me realise that I needed to investigate and use events was when I found myself cutting and pasting the same bits of Groovy code into every new test case I was creating again and again. It sounds obvious, but it did take a while before I realised there was a better way.